Here is my Swift Code which i am using to show 3 different view in single cell.
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()
})
})
})
}
this function works fine but frame per second and CPU usage is very high.
I was wondering if somehow we can optimize this animation using .repeat
and .autoreverse
When i try to do that the animation doesn't work in sync. I need to animate one view after another 1> view 1 alpha 0 to 1
2> view 2 alpha 0 to 1
3> view 3 alpha 0 to 1
and than repeat the cycle.
Following Code works better in terms of CPU % and Frame per seconds but the animations are not in sync.
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
}