我正在尝试实现一个可以从其父 View 中拖出的 UIView。
我尝试将 UIPanGestureRecognizer
添加到我希望能够拖动的 View 中。然而,从其父 View 中删除 UIView 并将其添加到另一个 View 似乎破坏了手势识别器。
注释掉 UIGestureRecognizerStateBegan
中的代码后,其他两个 block 中的代码可以正常运行,但是当我恢复它时,永远不会实现 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 只能是一个 View 的 subview 。
关于ios - 更改 super View 会破坏 UIPanGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12161047/