更改超级视图会破坏 UIPanGestureRecognizer [英] Changing superview breaks UIPanGestureRecognizer

查看:22
本文介绍了更改超级视图会破坏 UIPanGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个可以拖出其父视图的 UIView.

I'm trying to implement a UIView that can be dragged out of its superview.

我尝试将 UIPanGestureRecognizer 添加到我希望能够拖动的视图中.然而,从其父视图中删除 UIView 并将其添加到另一个视图似乎正在破坏手势识别器.

I tried adding a UIPanGestureRecognizer to the view I want to be able to drag. It seems, however, that removing the UIView from its superview and adding it to another view, is breaking the gesture recognizer.

UIGestureRecognizerStateBegan 中的代码注释掉后,其他两个块中的代码运行正常,但是当我恢复它时,UIGestureRecognizerStateChanged 和 UIGestureRecognizerStateEnded 状态永远不会实现.

With the code within the UIGestureRecognizerStateBegan commented out, the code within the other two blocks functions correctly, but when I reinstate it, the UIGestureRecognizerStateChanged and UIGestureRecognizerStateEnded states are never achieved.

出了什么问题?

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只能是一个视图的子视图.

You deduce right, [self removeFromSuperview] breaks gesture recognizer. I have had the same problem. Comment this line [self removeFromSuperview] and should be ok, you don't have to remove it from superview, because UIView can only be a subview of one view.

这篇关于更改超级视图会破坏 UIPanGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆