ios - 更改 super View 会破坏 UIPanGestureRecognizer

标签 ios uiview uipangesturerecognizer superview

我正在尝试实现一个可以从其父 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/

相关文章:

ios - 为什么 Xcode 创建名为 "XC Wildcard"的非通配符应用程序 ID?

iphone - 无法识别的选择器 -[UIView addSubView :]

iphone - UIView 上的圆角

iphone - 拖动时为 UIView 制作动画

ios - 无法删除手势

ios - Pan 手势识别器 : Location of touch crashes once the pan gesture is released

ios - 全面替换已提交至苹果应用商店的应用

ios - 在设备上将 GIF 转换为 MP4

iOS SDWebImage 交叉淡入淡出效果 UITableViewCell

iphone - 绘制 UIImage 的选定区域