Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIGestureRecognitzerState.已取消与UIGestureRecognitzerState.失败_Ios_Objective C_Swift_Uigesturerecognizer_Gesture Recognition - Fatal编程技术网

Ios UIGestureRecognitzerState.已取消与UIGestureRecognitzerState.失败

Ios UIGestureRecognitzerState.已取消与UIGestureRecognitzerState.失败,ios,objective-c,swift,uigesturerecognizer,gesture-recognition,Ios,Objective C,Swift,Uigesturerecognizer,Gesture Recognition,.Cancelled和Failed状态之间有什么区别 将手势识别器的状态设置为.Cancelled或.Failed如何影响手势识别器本身 手势识别器的状态何时变为.Cancelled和Failed 手势识别器在哪一点标记为“已识别”?转换到后,是否开始 如果是,手势的状态是否也可以设置为触摸移动中的开始 例如,UIPinchGestureRecognitzer在哪个阶段识别挤压手势?我想只有在触摸时才会移动,因为捏是一种持续的动作。以下是两种姿势的区别 。已取消 手势识别器已接收到导致取消连续手

.Cancelled
Failed
状态之间有什么区别

将手势识别器的状态设置为
.Cancelled
.Failed
如何影响手势识别器本身

手势识别器的状态何时变为
.Cancelled
Failed

手势识别器在哪一点标记为“已识别”?转换到
后,是否开始

如果是,手势的状态是否也可以设置为
触摸移动
中的
开始


例如,UIPinchGestureRecognitzer在哪个阶段识别挤压手势?我想只有在触摸时才会移动,因为捏是一种持续的动作。

以下是两种姿势的区别

。已取消

手势识别器已接收到导致取消连续手势的触摸。它在运行循环的下一个周期发送其操作消息(或多条消息),并将其状态重置为UIgestureRecognizerStateEnable

。失败

手势识别器接收到无法识别为其手势的多点触摸序列。未发送任何动作消息,手势识别器将重置为UIgestureRecognitizerStateEnabled


换言之,当连续手势被中断时,调用
.Cancelled
<代码>。失败的在手势未被识别为特定类型的手势时被调用。

以下是它们之间的区别

。已取消

手势识别器已接收到导致取消连续手势的触摸。它在运行循环的下一个周期发送其操作消息(或多条消息),并将其状态重置为UIgestureRecognizerStateEnable

。失败

手势识别器接收到无法识别为其手势的多点触摸序列。未发送任何动作消息,手势识别器将重置为UIgestureRecognitizerStateEnabled


换言之,当连续手势被中断时,调用
.Cancelled
.Failed
在手势未被识别为某种类型的手势时被调用。

实际上,.Cancelled和.Failed状态之间没有区别。两者都会导致手势识别器无法处理手势。我想这只是一种命名惯例

尽管如此,区别也在于这两种状态如何影响手势处理

这取决于手势识别器的先前状态

  • 如果手势识别器在
    触摸开始(触摸:设置,withEvent事件:UIEvent)
    UITouchPhase.Start
    触摸阶段)中从
    可能的
    转换为
    开始的
    ,而不是以相同的方法转换为
    。失败的
    取消的
    ,则队列中的下一个手势识别器(附在视图上)将有机会处理该手势。不会发送任何动作消息
  • 但是,如果手势识别器在触摸开始时(触摸:设置,withEvent事件:UIEvent)(
    UITouchPhase.Begined
    触摸阶段)从.mable转换为.begind,则在触摸移动时(触摸:设置,withEvent事件:UIEvent)方法到
    。失败
    。取消
    手势识别将失败,不会发生任何事情。但仍将发送动作消息
  • 如果在第8行注释掉代码,则手势识别将失败,连接到视图的下一个手势识别器将有机会处理该手势
  • 这里是视图控制器:

    class ViewController: UIViewController {
    
        func panHandler(sender: UIPanGestureRecognizer) {
             print("panHandler")
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
    
            let customRecognizer = CustomGestureRecognizer(target: self, action: #selector(ViewController.customHandler(_:)))
            view.addGestureRecognizer(customRecognizer)
    
            let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ViewController.panHandler(_:)))
            view.addGestureRecognizer(panRecognizer)
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    
        func customHandler(c: CustomGestureRecognizer) {
            print("customHandler")
        }
    }
    
    这里是一个自定义手势识别器:

    import UIKit
    import UIKit.UIGestureRecognizerSubclass
    
    class CustomGestureRecognizer: UIGestureRecognizer {
    
        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) {
            super.touchesBegan(touches, withEvent: event)
            state = .Began
            if touches.count == 1 {
                //state = .Failed
            }
            print("CustomGestureRecognizer.touchesBegan")
        }
    
        override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) {
            super.touchesMoved(touches, withEvent: event)
    
            if state == .Failed {
                return
            }
    
            if touches.count == 1 {
                state = .Failed //.Cancelled
            }
    
            state = .Changed
            print("CustomGestureRecognizer.touchesMoved")
        }
    
        override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) {
            super.touchesEnded(touches, withEvent: event)
            state = .Ended
            print("CustomGestureRecognizer.touchesEnded")
        }
    }
    
    导入UIKit
    导入UIKit.UIgestureRecognitizerSubclass
    类CustomGestureRecognitor:UIGestureRecognitor{
    覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
    super.touchsbegind(touchs,withEvent:event)
    状态=。开始
    if.count==1{
    //状态=。失败
    }
    打印(“CustomGestureRecognitor.touchesStart”)
    }
    覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
    super.touchsmoved(touchs,with event:event)
    如果状态==。失败{
    返回
    }
    if.count==1{
    状态=.Failed/.Cancelled
    }
    状态=.已更改
    打印(“CustomGestureRecognitor.touchesMoved”)
    }
    覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
    super.touchesend(touchs,withEvent:event)
    状态=。结束
    打印(“CustomGestureRecognitor.touchesEnded”)
    }
    }
    

    只需注释/取消注释第8行、第10行和第23行上的代码即可查看差异。

    实际上,.Cancelled和.Failed状态之间没有区别。这两种状态都会导致手势识别器无法处理手势。我想这只是一种命名约定

    尽管如此,区别也在于这两种状态如何影响手势处理

    这取决于手势识别器的先前状态

  • 如果手势识别器在
    触摸开始(触摸:设置,withEvent事件:UIEvent)
    UITouchPhase.Start
    触摸阶段)中从
    可能的
    转换为
    开始的
    ,而不是以相同的方法转换为
    。失败的
    取消的
    ,则队列中的下一个手势识别器(附在视图上)将有机会处理该手势。不会发送任何动作消息
  • 但是,如果手势识别器从.mable转换为.begind in
    touchsbegin(touchs:Set,withEvent-event:UIEvent)
    uitouchsphase.begind
    触摸阶段),则在
    touchsmoved(touchs:Set,withEvent:UIEvent)
    方法中转换为
    。失败
    取消
    手势记录