2

enter image description hereIf I'm using UIView.animate for 28 UIViewImages where them alpha changed from alpha = 0 to alpha = 0.5. Simulator loads my Macbook CPU upto 200%, but simulator in XCode debug navigator showing just 0-4% CPU load. If I run the app on iPhone X, all the same. iPhone CPU is 0-4%, but temperature of the device is high. If I'm comment animation function, app working good and iPhone have normal temperature. Is this a normal situation with simultaneous animation of 28 view? Or should it not be so?

Function for add View

func addView() { 
 for _ in 0...27 {     
    imageViews.append(UIImageView(image: UIImage(named: "logo_main")))
 }
 imageViews.forEach{ (view) in
    view.contentMode = .scaleAspectFill
    view.center = CGPoint(x: bounds.midX, y: bounds.maxY + view.bounds.size.height)
    addSubview(view)
 }
    layoutIfNeeded()
}

Animate Background Function

func animateBackground() {
        self.imageViews.forEach { view in
            view.alpha = 0
            let rand = TimeInterval(self.imageViews.count.arc4random)
            UIView.animate(withDuration: 3,
                           delay: rand,
                           options: [.repeat, .autoreverse, .curveEaseInOut],
                           animations: { view.alpha = 0,5 },
                           completion: nil)

    }
}

It's normal behavior of app? Maybe there is another way to make animated background blinking?

2 Answers2

1

I don't think you should create animations by stacking 28 views on top of each other

I think the main issue with your code is color blended layers. Final pixel you see on screen is a composition (i.e. blend) of top layer's pixel and pixels of all underlying layers. Therefore every underlying layer must be rendered to create final pixel you see on the screen. However if layer is opaque, composition process is optimized by not drawing underlying layers. For example if you stack 28 image views on each other it wouldn't harm visualization process very much because all image views except the top one just wouldn't be rendered.

Unfortunately in you source code there is no optimization because you constantly change alpha property of image views stacked upon each other. In worst case scenario the blending process have to be executed for all 28 image views. You can check if "color blending" is present in your animation by checking "Debug > Color Blended Layers" in Simulator.

The "color blended layers" can dramatically reduce performance in your app. I would recommend you to create animation by updating images in the single image view or at least reduce number of views in your animation.

P.S. I'm not sure I perfectly detected your performance problem. If not you can find more information about optimizing rendering performance in this thread: What triggers offscreen rendering, blending and layoutSubviews in iOS?

NShiny
  • 1,046
  • 1
  • 10
  • 19
  • I think this is exactly what you wrote. Initially, in each of the 28 views, one image was drawn in the vector, via the draw(_ rect :). And this led to a very strong heating of the iPhone, then I changed the vector drawings on the UIImageView and it began to heat up less. But still, it is not allowed for such a simple application. Still, changing the alpha properties of a large number of view at the same time is a very energy-intensive process. I just don’t understand why Debug Navigator shows the same value in the Energy Impact tab when the animation is on and when it’s off. I add screenshot – Alexander Ushakov Jun 16 '19 at 13:40
  • "I just don’t understand why Debug Navigator shows the same value in the Energy Impact tab when the animation is on and when it’s off." - I'm not sure I have an answer for this. My guess is that after animation is off your image views are still with alpha < 1 which in theory should still harm the performance. – NShiny Jun 16 '19 at 16:52
  • Thanks a lot for your help. Maybe there are ideas how to implement the smooth appearance and disappearance of images without the use of alpha? For now I think that I will have to draw several images of the background in Photoshop and make animation of their replacement one by one. – Alexander Ushakov Jun 17 '19 at 05:36
  • Unfortunately I don't think I know the right way to implement this kind of animation – NShiny Jun 18 '19 at 05:29
0

It depends on what your doing, bear in mind that the simulator shares the resources of your mac which is running alot of other stuff too, at the very least xcode (which isn't light on resource usage), the simulator itself and a bunch of other stuff.

You should never really test performance on a simulator. It's effectively a virtual machine.

Run the app from Xcode directly on a device and check the CPU and memory performance from there.

Scriptable
  • 19,402
  • 5
  • 56
  • 72