【发布时间】:2015-09-17 16:54:05
【问题描述】:
我有一个UIButton,我正在根据它后面的UICollectionView 的当前滚动位置设置动画。我正在执行的动画是将按钮图像从一个更改为另一个。
我正在使用以下代码来执行动画:
let buttonAnimation: CABasicAnimation = CABasicAnimation(keyPath: "contents")
buttonAnimation.fromValue = UIImage(named: "Search-Button")!.CGImage
buttonAnimation.toValue = UIImage(named: "Search-Button-Reverse")!.CGImage
buttonAnimation.duration = 1.0
self.searchButton.imageView!.layer.speed = 0.0; // Pause the animation
self.searchButton.imageView!.layer.addAnimation(buttonAnimation, forKey: "animateSearchButton")
然后我根据滚动视图内容偏移值scrollView.contentOffset.y 设置self.searchButton.imageView!.layer.timeOffset 来更新scrollViewDidScroll() { 中的动画状态。
这一切都很好,UIButton 的动画效果完全符合我的要求,但是当您按下它时通常显示的按钮的选定/突出显示状态不再起作用。我假设这是因为我通过设置layer.speed = 0.0 暂停了UIButton 内容上的任何动画,但是有什么办法可以解决这个问题吗?
我确实有过拥有 2 个UIButton's 的想法,每个图像一个,然后为每个图像的不透明度设置动画(一个淡出,另一个淡入),而不是为一个UIButton 的图像设置动画,但这看起来很混乱它还有一个问题,即使在不透明度为 0 之后,也不会通过顶部按钮将触摸传递到后面的按钮。
有人有什么建议吗?很高兴在 Objective-C 或 Swift 中查看解决方案,如果需要,我会自行转换。
【问题讨论】:
标签: ios objective-c swift cabasicanimation quartz-core