What's the trick to pass an event to the next responder in the responder chain?

前端 未结 8 989
悲&欢浪女
悲&欢浪女 2020-12-05 14:44

Apple is really funny. I mean, they say that this works:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* touch = [touches any         


        
8条回答
  •  独厮守ぢ
    2020-12-05 15:19

    use this

        [super.nextResponder touchesBegan:touches withEvent:event];
    

    before this line in your code

        [self.nextResponder touchesBegan:touches withEvent:event];
    

    it means

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
         UITouch* touch = [touches anyObject];
         NSUInteger numTaps = [touch tapCount];
         if (numTaps < 2) {
             [super.nextResponder touchesBegan:touches withEvent:event];
             [self.nextResponder touchesBegan:touches withEvent:event];
         } else {
             [self handleDoubleTap:touch];
         }
    }
    

    **Swift 4 Version **

    func touchesBegan(_ touches: Set, with event: UIEvent) {
        let touch = touches?.first as? UITouch
        let numTaps: Int? = touch?.tapCount
        if (numTaps ?? 0) < 2 {
            if let aTouches = touches as? Set {
                super.next?.touchesBegan(aTouches, with: event)
                next?.touchesBegan(aTouches, with: event)
            }
        } else {
            handleDoubleTap(touch)
        }
    }
    

提交回复
热议问题