我
animateFinished方法中添加了几行,以查看发生了什么:
func animateFinished(textToDisplay: String, footerBtn: UIButton, footerImg: UIImageView) { //Should cancel any current animation print("Remove animations") footerBtn.layer.removeAllAnimations() print("Animations removed") footerBtn.alpha = 0 footerBtn.setTitle(textToDisplay, forState: UIControlState.Normal) footerBtn.titleLabel!.font = UIFont(name: "HelveticaNeue-Regular", size: 18) footerBtn.setTitleColor(UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0), forState: UIControlState.Normal) //footerBtn.backgroundColor = UIColor(red: 217/255.0, green: 217/255.0, blue: 217/255.0, alpha: 1.0) print("Initial animation setup completed") UIView.animateKeyframesWithDuration(3.0 , delay:0.0, options: UIViewKeyframeAnimationOptions.CalculationModeLinear, animations: { UIView.addKeyframeWithRelativeStartTime(0.0, relativeDuration:0.10, animations:{ footerImg.alpha = 0.01 //Img fades out footerBtn.backgroundColor = UIColor(red: 46/255.0, green: 103/255.0, blue: 00/255.0, alpha: 0.6) //Bg turns to green }) UIView.addKeyframeWithRelativeStartTime(0.10, relativeDuration:0.30, animations:{ footerBtn.alpha = 1 //Text and green bg fades in footerBtn.backgroundColor = UIColor(red: 46/255.0, green: 173/255.0, blue: 11/255.0, alpha: 0.6) //BG turns greener }) UIView.addKeyframeWithRelativeStartTime(0.40, relativeDuration:0.50, animations:{ footerBtn.alpha = 0.01 //Text fades out & bg fade out }) }, completion: { finished in print("Completion block started") footerImg.alpha = 1 footerBtn.alpha = 1 footerBtn.backgroundColor = UIColor.clearColor() footerBtn.setTitleColor(UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1.0), forState: UIControlState.Normal) footerBtn.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 18) footerBtn.setTitle("", forState: UIControlState.Normal) //Completion blocks sets values back to norm print("Completion block finished") } )}//End of 'Finished' animation
如果您允许动画运行完成,则将显示日志,如您所愿:
Remove animationsAnimations removedInitial animation setup completedCompletion block startedCompletion block finished
但是,如果在动画过程中点击按钮,则会看到以下内容:
Remove animationsAnimations removedInitial animation setup completedRemove animationsAnimations removedInitial animation setup completedCompletion block startedCompletion block finishedCompletion block startedCompletion block finished
发生了什么情况,
removeAllAnimations导致在完成第二个调用的初始设置 之后 ,但 在 执行第二个动画 之前
,执行了完成块(对于第一个调用)。因此,例如,在第二个动画期间,按钮标题为“”。
解决方法相对简单:如果动画尚未完成,请不要执行完成块:
completion: { finished in if (!finished) { return } print("Completion block started") footerImg.alpha = 1 footerBtn.alpha = 1 footerBtn.backgroundColor = UIColor.clearColor() footerBtn.setTitleColor(UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1.0), forState: UIControlState.Normal) footerBtn.titleLabel!.font = UIFont(name: "HelveticaNeue-Light", size: 18) footerBtn.setTitle("", forState: UIControlState.Normal) print("Completion block finished") //Completion blocks sets values back to norm }
另外,根据《 Shripada》,您将需要从footerImg和footerBtn中移除动画,并使用以下命令:
footerImg.layer.removeAllAnimations()
在方法开始时。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)