我的viewDidLoad中有一个计时器,该计时器每5秒重复播放一次动画。该动画调用另一个动画函数,该函数一旦点按即可淡出UIImageView。我遇到了结束淡出动画的问题,因为当计时器重复其动画时,UIImageView在以前的淡出功能中仍然不可见。
viewDidLoad,它要求动画开始并每5秒钟重复一次。
override func viewDidLoad() {
Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(ViewController.startRedBalloon), userInfo: nil, repeats: true)
}
在viewDidLoad中由计时器调用的动画。
@objc func startRedBalloon() {
let xOrigin = arc4random_uniform(208) + 1
let xEnding = arc4random_uniform(208) + 1
redBalloon.isHidden = false
redBalloon.image = UIImage(named: "redBalloon")
redBalloon.contentMode = .scaleAspectFit
redBalloon.frame = CGRect(x: Int(xOrigin), y: 667, width: Int(redBalloon.frame.size.width), height: Int(redBalloon.frame.size.height))
let animRed = UIViewPropertyAnimator(duration: 5, timingParameters: UICubicTimingParameters(animationCurve: .easeIn))
animRed.addAnimations {
self.redBalloon.frame = CGRect(x: Int(xEnding), y: -192, width: 100, height: 116)
}
animRed.addCompletion { _ in
self.endGame()
}
let imageTapRed = UITapGestureRecognizer(target: self, action: #selector(imageTappedRed))
redBalloon.isUserInteractionEnabled = true
redBalloon.addGestureRecognizer(imageTapRed)
animRed.startAnimation()
}
点击UIImage后就会调用动画。
@objc func imageTappedRed(_ sender: UITapGestureRecognizer) {
redBalloon.image = UIImage(named: "redShreds")
UIViewPropertyAnimator(duration: 2, curve: .easeOut, animations: {
self.redBalloon.alpha = 0.0
}).startAnimation()
score += 1
}