更改superview会破坏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. } 

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