这是我的Swift Code,我用它来显示单个单元格中的3个不同视图。
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 GT;查看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
}