CALayer – Shadow causes a performance hit?
So I am doing some custom animations on my navigationcontroller and the way it pushes and pops the viewControllers.
Everything runs smooth. As soon as I add the following code (In a subclass of
UINavigationController), I face a huge performance hit. After adding a shadow all animations become very laggy. Is this expected or am I doing something wrong in the code?
// This code gets called once during NavigationController initialization. [self.view setClipsToBounds:NO]; [self.view.layer setCornerRadius:5]; [self.view.layer setShadowOffset:CGSizeMake(0, 20)]; [self.view.layer setShadowColor:[[UIColor yellowColor] CGColor]]; [self.view.layer setShadowRadius:20.0]; [self.view.layer setShadowOpacity:1];
Changed my shadow radius to 1 and it’s still slow
- What are the differences between a UIView and a CALayer?
- UIView animation block pause both the animation and the completion code
- Applying a CIFilter to a CALayer
- iOS CAlayer Orientation AVCaptureVideoPreviewLayer doesn't rotate
- Rotating a CALayer 90 degrees?
- Cropping a captured image exactly to how it looks in AVCaptureVideoPreviewLayer
4 Solutions Collect From Internet About “CALayer – Shadow causes a performance hit?”
You should expect a slowdown from adding a shadow. A
shadowRadius of 20 is very high and will be especially slow.
The other key to improve shadow rendering speed: set the
shadowPath property. It can help dramatically.
self.view.layer.shouldRasterize = YES; self.view.layer.rasterizationScale = UIScreen.mainScreen.scale;
I was recently having some issues with slow CALayer shadows, and that simple line of code fixed up everything for me!
shadowPath instead of
theView.layer.shadowPath = [UIBezierPath bezierPathWithRect:theView.bounds].CGPath;
Check this post: iphone – Animation's performance is very poor when view's shadow is on
Yes, shadow’s are very expensive (especially a shadow that big — play with the radius and you’ll notice it makes a huge difference in the degree of slowdown you experience). One way to improve performance is to render it once to a
CGImageContext and just display that image instead of having the layer re-render the shadow every time it redraws (but this doesn’t work if the shadow needs to animate or something).
- how to properly use insertRowsAtIndexPaths?
- Get button click inside UI table view cell
- UICollectionView Self Sizing Cells with Auto Layout
- Scratching Audio
- Sample code to create pdf programmatically
- iOS – Clipping union of two paths using UIBezierPath appendPath
- UIImagePickerController stuck at compressing Video
- How do I resize the UIImage to reduce upload image size
- Display the distance from user with mapKit Swift (miles/km)
- UILabel text to fill in the frame width
- Swift Google Directions API JSON – EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
- Facebook.com and the iOS7 Captive Portal Detection
- Why UIButton does not require to alloc and init?
- APN custom notification sound issue
- Getting data from the Info plist