这是我用来在单个单元格中显示 3 个不同视图的 Swift 代码。
func animateBothObjects() {
weak var weakSelf = self
UIView.animate(withDuration: 1.0, delay: 2.0, options: [.curveEaseIn, .allowUserInteraction], animations: {() -> Void in
self.infoView.alpha = 0.0
self.offerImage.alpha = 1.0
self.businessOwnerImage.alpha = 0.0
}, completion: {(finished: Bool) -> Void in
if !self.isAccountFeed && !finished {
return
}
UIView.animate(withDuration: 1.0, delay: 2.0, options: [.curveEaseIn, .allowUserInteraction], animations: {() -> Void in
self.infoView.alpha = 0.0
self.offerImage.alpha = 0.0
self.businessOwnerImage.alpha = 1.0
}, completion: {(finished: Bool) -> Void in
if !self.isAccountFeed && !finished {
return
}
UIView.animate(withDuration: 1.0, delay: 2.0, options: [.curveEaseIn, .allowUserInteraction], animations: {() -> Void in
self.infoView.alpha = 1.0
self.offerImage.alpha = 0.0
self.businessOwnerImage.alpha = 0.0
}, completion: {(finished: Bool) -> Void in
if !self.isAccountFeed && !finished {
return
}
weakSelf!.animateBothObjects()
})
})
})
}
此功能工作正常,但每秒帧数和 CPU 使用率非常高。我想知道我们是否可以使用.repeat
和优化这个动画.autoreverse
当我尝试这样做时,动画无法同步工作。我需要一个又一个视图动画1>视图1 alpha 0到1
2> 查看 2 alpha 0 到 1
3> 查看 3 alpha 0 到 1
然后重复这个循环。
以下代码在 CPU % 和每秒帧数方面效果更好,但动画不同步。
self.infoView.alpha = 0.0
self.offerImage.alpha = 0.0
self.businessOwnerImage.alpha = 0.0
UIView.animate(withDuration: 2.0, delay: 0.0, options: [.transitionCrossDissolve,.repeat,.autoreverse], animations: {
self.infoView.alpha = 1.0
}) { (success) in
//self.infoView.alpha = 0.0
print("animation completed as per Xcode")
}
UIView.animate(withDuration: 2.0, delay: 2.0, options: [.transitionCrossDissolve, .repeat,.autoreverse], animations: {
self.offerImage.alpha = 1.0
}) { (success) in
//self.offerImage.alpha = 0.0
}
UIView.animate(withDuration: 2.0, delay: 4.0, options: [.transitionCrossDissolve, .repeat,.autoreverse], animations: {
self.businessOwnerImage.alpha = 1.0
}) { (success) in
//self.businessOwnerImage.alpha = 0.0
}