2

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
}
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
  • I don't see anything in _this_ code that would result in a poor frame rate or a high CPU usage. Are you profiling this on a real device? Also, what is the CPU doing? Is it busy executing code in your application or a different process? – David Rönnqvist Jun 27 '17 at 09:55
  • Thanks. I have created a sample app where i am not doing anything other than animation. I am using Core Animation Tool to measure frame per second and CPU % – Mihir Mehta Jun 27 '17 at 10:13
  • if i use [.transitionCrossDissolve, .repeat,.autoreverse] or CABasicAnimation the Frame rates and CPU % increase drastically but animation mixes up – Mihir Mehta Jun 27 '17 at 10:15
  • It's possible that the animation you’re doing would work well with a repeating UIView key-frame animation with three key-frames. However, that doesn't explain the performance you're seeing. Are you profiling on a real device or on the simulator? Please include some of the profile data that shows what the CPU is doing. – David Rönnqvist Jun 27 '17 at 10:20
  • Yes real device , Core Animation template doesn't support simulator in any case. – Mihir Mehta Jun 27 '17 at 10:24

0 Answers0