按钮背景色动画禁用触摸 (IOS)



我正在开发一个读取QR码的Swift应用程序。当应用程序识别QR码时,它需要开始对按钮的背景颜色进行动画处理以吸引用户的注意力。动画按预期工作,但按钮似乎不再响应触摸事件。如果我禁用动画,按钮将正常工作。我应该如何继续解决这个问题?谢谢!

动画的代码如下:

var getCoordButtonAnimationRunning = false
let secondBackgroundColor = UIColor(red: 137.0/255.0, green: 205.0/255.0, blue: 237.0/255.0, alpha: 1.0)
func animateCoordButton() {
    if !getCoordButtonAnimationRunning && !initiatedAnimationStop {
        getCoordButtonAnimationRunning = true
        UIView.animateWithDuration(0.5, delay: 0.0, options: [.Autoreverse, .Repeat],
            animations: {
                self.getCoordinatesButton.backgroundColor = self.secondBackgroundColor
            },
            completion: { finished in
                UIView.animateWithDuration(0.5, delay: 0.0, options: [],
                    animations: {
                        self.getCoordinatesButton.backgroundColor = self.getCoordinatesButtonColor
                    },
                    completion: { finished in
                        self.getCoordButtonAnimationRunning = false
                        self.initiatedAnimationStop = false
                    })
            })
    }
}
var initiatedAnimationStop = false
func stopAnimatingCoordButton() {
    if !initiatedAnimationStop {
        initiatedAnimationStop = true
        getCoordinatesButton.layer.removeAllAnimations()
    }
}

您需要添加 .允许动画的用户交互选项。默认情况下,对视图进行动画处理会禁用用户交互。

最新更新