使用allowUserInteraction的UIView.animate仅在动画的最终帧中启用UIButton

时间:2018-05-16 22:42:54

标签: ios swift animation uibutton 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)

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

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