更改superview会破坏UIPanGestureRecognizer

时间:2012-08-28 14:05:21

标签: ios uiview uipangesturerecognizer superview

我正在尝试实现一个可以拖出超级视图的UIView。

我尝试将UIPanGestureRecognizer添加到我希望能够拖动的视图中。但是,似乎将UIView从其超级视图中删除并将其添加到另一个视图中,正在破坏手势识别器。

UIGestureRecognizerStateBegan注释掉的代码中,其他两个块中的代码正常运行,但是当我恢复它时,永远不会实现UIGestureRecognizerStateChanged和UIGestureRecognizerStateEnded状态。

出了什么问题?

if ([gr state] == UIGestureRecognizerStateBegan)
{
    CGPoint newCenter = [endView convertPoint:[self center]
                                     fromView:startView];
    [self removeFromSuperview];
    [endView addSubview:self];
    [self setCenter:newCenter];

}

if ([gr state] == UIGestureRecognizerStateChanged)
{
    // Some code that successfully moves the view.
}

if ([gr state] == UIGestureRecognizerStateEnded)
{
    // Other code.
}

1 个答案:

答案 0 :(得分:5)

你推断右,[self removeFromSuperview]打破手势识别器。我曾经也有过一样的问题。注释这一行[self removeFromSuperview]并且应该没问题,你不必从superview中删除它,因为UIView只能是一个视图的子视图。