【问题标题】:Changing superview breaks UIPanGestureRecognizer更改超级视图会破坏 UIPanGestureRecognizer
【发布时间】:2023-03-20 17:13:02
【问题描述】:

我正在尝试实现一个可以拖出其父视图的 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.
}

【问题讨论】:

    标签: ios uiview uipangesturerecognizer superview


    【解决方案1】:

    你猜对了,[self removeFromSuperview] 破坏了手势识别器。我曾经也有过一样的问题。评论这行[self removeFromSuperview]应该没问题,你不必从superview中删除它,因为UIView只能是一个视图的子视图。

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 2012-04-01
      • 1970-01-01
      相关资源
      最近更新 更多