更改superview会破坏UIPanGestureRecognizer

Ric*_*ich 4 uiview superview ios uipangesturerecognizer

我正在尝试实现一个可以拖出超级视图的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.
}
Run Code Online (Sandbox Code Playgroud)

paw*_*opa 5

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