Tap Gesture on animating UIView not working

后端 未结 6 1674
南笙
南笙 2020-12-19 18:26

I have a tap gesture on a UILabel who\'s translation is being animated. Whenever you tap on the label during the animation there\'s no response from the tap gesture.

<
相关标签:
6条回答
  • 2020-12-19 18:44

    You will not be able to accomplish what you are after using a tapgesture for 1 huge reason. The tapgesture is associated with the frame of the label. The labels final frame is changed instantly when kicking off the animation and you are just watching a fake movie(animation). If you were able to touch (0,900) on the screen it would fire as normal while the animation is occuring. There is a way to do this a little bit different though. The best would be to uses touchesBegan. Here is an extension I just wrote to test my theory but could be adapted to fit your needs.For example you could use your actual subclass and access the label properties without the need for loops.

    extension UIViewController{
    
    public override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        guard let touch = touches.first else{return}
        let touchLocation = touch.locationInView(self.view)
    
        for subs in self.view.subviews{
            guard let ourLabel = subs as? UILabel else{return}
            print(ourLabel.layer.presentationLayer())
    
            if ourLabel.layer.presentationLayer()!.hitTest(touchLocation) != nil{
                print("Touching")
                UIView.animateWithDuration(0.4, animations: {
                    self.view.backgroundColor = UIColor.redColor()
                    }, completion: {
                        finished in
                        UIView.animateWithDuration(0.4, animations: {
                            self.view.backgroundColor = UIColor.whiteColor()
                            }, completion: {
                                finished in
                        })
                    })
                }
            }
    
        }
    }
    

    You can see that it is testing the coordinates of the CALayer.presentationLayer()..That's what I was calling the movies. To be honest, I have still not wrapped my head completely around the presentation layer and how it works.

    0 讨论(0)
  • 2020-12-19 18:44

    For your tap gesture to work, you have to set the number of taps. Add this line:

    tapGesture.numberOfTapsRequired = 1

    (I'm assuming that tapGesture is the same one you call label.addGestureRecognizer(tapGesture) with)

    0 讨论(0)
  • 2020-12-19 18:53

    I was stuck on this problem for hours, and could not understand why the tapping did not work on an animated label which slides out of screen after 3 seconds delay.

    Well said agibson007, about the animation works like a fake movie's playback, the 3-second delay controls the payback of the movie, yet the label's frame is changed as soon as the animation begins without a delay. So the tapping (which depends on the label's frame at its original position) would not work.

    My solution was changing the 3-second delay to a timeout function like -

    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
       [weak self] in
       self?.hideLabel()           
    }
    

    So that keeps the tapping works during the delay, and allow animation runs inside the hideLabel() call after the delay.

    0 讨论(0)
  • 2020-12-19 18:54

    How to detect touches in a view which is moving

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    
        let pf = layer.presentation()!.frame
        // note, that is in the space of our superview
    
        let p = self.convert(point, to: superview!)
    
        if pf.contains(p) { return self }
        return nil
    }
    

    It's that easy

    Related tip -

    Don't forget that in most cases if an animation is running, you will, of course, almost certainly want to cancel it. So, say there's a "moving target" and you want to be able to grab it with your finger and slide it somewhere else, naturally in that use case your code in your view controller will look something like ..

       func sliderTouched() {
           if alreadyMoving {
              yourPropertyAnimator?.stopAnimation(true)
              yourPropertyAnimator = nil
           }
           etc ...
       }
    
    0 讨论(0)
  • 2020-12-19 18:56

    If you want to see the animation, you need to put it in the onTap handler.

    let gesture = UITapGestureRecognizer(target: self, action: "onTap:")
    gesture.numberOfTapsRequired = 1
    label.addGestureRecognizer(gesture)
    label.userInteractionEnabled = true
    
    label.transform = CGAffineTransformMakeTranslation(0, 0)
    
    UIView.animateWithDuration(12, delay: 3, options: [.AllowUserInteraction], animations: { () -> Void in
          label.transform = CGAffineTransformMakeTranslation(0, 900)
          }, completion: nil)
    
    
    func onTap(sender : AnyObject)
    {
    
        print("Tapped")
    }
    
    0 讨论(0)
  • 2020-12-19 18:57

    Below is a more generic answer based on the answer from @agibson007 in Swift 3.

    This didn't solve my issue immediately, because I had additional subviews covering my view. If you have trouble, try changing the extension type and writing print statements for touchLocation to find out when the function is firing. The description in the accepted answer explains the issue well.

    extension UIViewController {
    
        open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    
            guard let touch = touches.first else { return }
            let touchLocation = touch.location(in: self.view)
    
            for subview in self.view.subviews {
    
                if subview.tag == VIEW_TAG_HERE && subview.layer.presentation()?.hitTest(touchLocation) != nil {
    
                    print("[UIViewController] View Touched!")
                    // Handle Action Here
    
                }
    
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题