【问题标题】:Dragging a button from UITableView从 UITableView 拖动一个按钮
【发布时间】:2011-05-24 11:57:29
【问题描述】:

我有一个由自定义单元格组成的 TableView。此单元格包含一个标签和一个按钮。现在我有一个 panGestureRecognizer 添加到单元格中的按钮。 我的问题是当我尝试开始移动/拖动按钮时,按钮移动正确但不幸的是按钮得到隐藏在 TableView 后面。我怎样才能让它出现在它面前。自定义单元格是通过编程方式创建的,TableView 也是如此。我的手势处理方法如下:


-(void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer withEvent:(id) event {
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

//*******************Dragging Ended*************************************

if([gestureRecognizer state]==UIGestureRecognizerStateEnded)
{
    [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

}

//*******************Dragging Started andchanged*************************************

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] ==UIGestureRecognizerStateChanged) 
{

    CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
    [piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
    [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
    CGPoint moveLocation=[gestureRecognizer locationInView:piece];
    NSIndexPath *indexPath=[tableView indexPathForRowAtPoint:moveLocation];

}

-(void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer { if (gestureRecognizer.state == UIGestureRecognizerStateBegan || (gestureRecognizer.state == UIGestureRecognizerStateChanged)) { UIView *piece = gestureRecognizer.view; CGPoint locationInView = [gestureRecognizer locationInView:piece]; CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview]; piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height); piece.center = locationInSuperview; } }


代码已根据苹果代码here建模

【问题讨论】:

    标签: ipad ios4 uitableview uigesturerecognizer


    【解决方案1】:

    这里的解决方案:Drag an UIImageView from UIscrollView to another view

    希望对你有帮助

    【讨论】:

    • 感谢您的回复。我已经在图像视图的帮助下实现了这一点。但是这些要求迫使我拖动按钮而不是它的图像。当我添加图像时,它被添加到主视图中,即 self.view 和 ergo 它工作正常。但是如果按钮嵌入在 tableview 中,它会变得相当复杂。除了将按钮添加到 tableview 单元格的内容视图之外,还有其他方法可以添加按钮吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多