0

If I'm animating a UIButton and use the .allowUserInteraction option, the area of the final frame at the end of the animation can be tapped to interact with the button, even before the button has made it to that frame. Tapping where the button is visible does not trigger the action if it is outside of the final frame the button is animating toward:

UIView.animate(withDuration: 9.0, delay: 0.0, options: [.curveLinear, .allowUserInteraction], animations: {
            self.theButton.frame = CGRect(x: (self.view.frame.width * 0.1), y: self.theButton.frame.origin.y, width: self.theButton.frame.width, height: self.theButton.frame.height)

        }, completion: nil)

Any help would be greatly appreciated. Thanks!

RanLearns
  • 4,086
  • 5
  • 44
  • 81
  • Possible Duplicate of https://stackoverflow.com/questions/6346046/uibutton-not-interacting-during-animation – Karthick Ramesh May 16 '18 at 23:07
  • What you are seeing is the expected (if annoying) behavior. A view/layer does not actually move during an animation. Instead, it gets moved in a presentationLayer. The view itself snaps to it's end position at the beginning of the animation. RanLearns gave you the correct solution in his answer. – Duncan C May 17 '18 at 00:04
  • [Here](https://stackoverflow.com/a/66824606/885189) is an answer that might work for you. – JaredH Mar 26 '21 at 21:46

1 Answers1

2

Doesn't matter that it's a UIButton. Whether it's a button or an imageview, the only way to tap it where it is showing during the animation seems to be to use touchesBegan and test whether the object's layer.presentation frame contains the touch location:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let touchLocation = touch.location(in: self.view)

        let buttonFrame = theButton.layer.presentation()!.frame

        if buttonFrame.contains(touchLocation) {
            print("Tapped the button here!")
        }
    }
RanLearns
  • 4,086
  • 5
  • 44
  • 81