UIView.animate 使用 allowUserInteraction 仅在动画的最后一帧启用 UIButton

UIView.animate with allowUserInteraction enables the UIButton only in the animation's final frame

如果我正在为 UIButton 设置动画并使用 .allowUserInteraction 选项,则可以点击动画结束时最后一帧的区域与按钮进行交互,甚至在按钮到达该帧之前.如果按钮位于动画指向的最终帧之外,则点击按钮可见的位置不会触发操作:

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)

如有任何帮助,我们将不胜感激。谢谢!

它是一个 UIButton 并不重要。无论是按钮还是图像视图,在动画期间显示的位置点击它的唯一方法似乎是使用 touchesBegan 并测试对象的 layer.presentation 帧是否包含触摸位置:

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!")
        }
    }