【问题标题】:What really happens when call setCancelsTouchesInView?调用 set CancelsTouchesInView 时会发生什么?
【发布时间】:2012-10-24 03:34:05
【问题描述】:

想知道当我调用 setCancelsTouchesInView 时真正发生了什么。官方文档中没有介绍http://developer.apple.com/library/ios/#documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html

谢谢

【问题讨论】:

    标签: iphone objective-c ios uigesturerecognizer


    【解决方案1】:

    ACB 引用了UIGestureRecognizer 参考。为了更具体一点,假设您有一个附加了平移手势识别器的视图,并且您的视图控制器中有这些方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesBegan");
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesMoved");
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesEnded");
    }
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesCancelled");
    }
    
    - (IBAction)panGestureRecognizerDidUpdate:(UIPanGestureRecognizer *)sender {
        NSLog(@"panGesture");
    }
    

    当然,平移手势识别器配置为发送panGestureRecognizerDidUpdate: 消息。

    现在假设您触摸视图,移动手指足以识别平移手势,然后抬起手指。应用打印什么?

    如果手势识别器将cancelsTouchesInView 设置为YES应用将记录这些消息:

    touchesBegan
    touchesMoved
    touchesCancelled
    panGesture
    panGesture
    (etc.)
    

    在取消之前您可能会收到多个touchesMoved

    因此,如果您将cancelsTouchesInView 设置为YES(默认值),系统将在从手势识别器发送第一条消息之前取消触摸,您将不会再收到任何与触摸相关的消息那种触感。

    如果手势识别器将cancelsTouchesInView 设置为NO应用将记录这些消息:

    touchesBegan
    touchesMoved
    panGesture
    touchesMoved
    panGesture
    touchesMoved
    panGesture
    (etc.)
    panGesture
    touchesEnded
    

    因此,如果您将cancelsTouchesInView 设置为NO,系统将继续发送与手势触摸相关的消息,并与手势识别器的消息交错。触摸将正常结束而不是被取消(除非系统出于其他原因取消触摸,例如在触摸期间按下主页按钮)。

    【讨论】:

    • 谢谢!当您将cancelsTouchesInView 设置为No 时,该事件将被取消弹出对吗?
    • 我不明白“取消弹出”是什么意思。
    • 这是一个旧线程,所以苹果现在可能已经改变了行为,但如果cancelTouchesInView 设置为true,我会得到不同的调用顺序:touchesBegantochesMoved , panGesture (sender.state .began), touchesCancelled, panGesture (sender.state .changed), panGesture (sender.state .changed), ... .所以基本上区别在于touchesCancelled 在第一次调用panGesture 之后被调用,至少在我的情况下,根据我的逻辑,这不是我所期望和想要的。我在这里错过了什么吗?
    • 听起来他们在我调查后的 9 年里改变了它。
    【解决方案2】:

    来自苹果开发者门户link

    cancelsTouchesInView — 如果手势识别器识别出它的手势, 它会从他们的视野中解除该手势的剩余触摸(所以 窗口不会提供它们)。窗口取消之前的 使用 (touchesCancelled:withEvent:) 消息传递触摸。如果一个 手势识别器无法识别其手势,视图接收到 多点触摸序列中的所有触摸。

    cancelsTouchesInView:

    影响是否触摸的布尔值 当手势被识别时传递到视图。

    @property(nonatomic) BOOL cancelsTouchesInView

    讨论

    当这个 属性为 YES(默认值)并且接收器识别其手势, 该手势的未决触摸不会传递给 视图和先前传递的触摸被取消通过 touchesCancelled:withEvent: 消息发送到视图。如果一个手势 识别器无法识别它的手势,或者如果 this 的值 属性为 NO,视图接收多点触控中的所有触控 顺序。

    【讨论】:

    • “那个手势的触摸是 PENDING..”是什么意思?
    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 2013-09-17
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多