Shadow inside custom views causes flickering - ios

I have a custom popup view in my iOS application and inside that there are two views who need a small shadow below them. I'm animating this popup from the bottom to the top of the screen. While animating it flickers/stutters because of the shadows. Do you have any idea how to fix this?
Here is the code that I'm using for the shadows (Swift 3):
view.layer.masksToBounds = false
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: 1)
view.layer.shadowRadius = 1
view.layer.shadowOpacity = 0.5
And yes, it is definitely because of the shadows.

Related

How to add shadow to only top side of UIView without using shadow path and adding layers

How to add shadow to only topside of UIView without affecting other sides?. My view is based on constraints so using shadow path or inserting layer not worked properly.
view.layer.masksToBounds = false
view.layer.shadowColor = UIColor.gray.cgColor
view.layer.shadowOffset = CGSize(width: 0, height: -2)
view.layer.shadowOpacity = 0.5
view.layer.shadowRadius = 3.0
change view.layer.shadowRadius to 0 if you want the shadow to be only on the top
UPDATE:- to have shadow effect on top, add the shadowView (view on which shadow is needed) inside another view(containerView) and leave some spacing from the top (shadowView width is equal to containerView width), make sure that the containerView.clipsToBounds = true. Apply all your code to shadowView, you should see a shadow on the top.

Draw Shadow Only *Around* UIView Which Contains Transparency

Question I'm struggling on. I've searched SO for a fair good time by now, but couldn't find an answer.
I have a UIView which contains alpha value of 0.5, means - it transparent.
If I'm applying the usual code for UIView shadowing -
class func applyShadow(view : UIView)
{
view.layer.shadowColor = UIColor.blackColor().colorWithAlphaComponent(0.15).CGColor
view.layer.shadowOpacity = 1
view.layer.shadowOffset = CGSizeMake(0, 0.5)
view.layer.shadowRadius = 1.3
view.layer.masksToBounds = false
view.layer.shouldRasterize = true
view.layer.rasterizationScale = UIScreen.mainScreen().scale
}
The "fill" of the my UIView get shadowed as well.
How can I draw the shadow only on the "border" path of my UIView, excluding the UIView fill?
For 2022 this is now possible.
Essentially you do this:
shadowHole.fillRule = .evenOdd
shadowHole.path = p.cgPath
full details ...
https://stackoverflow.com/a/59092828/294884

IOS - When I rotate a UIView the edges become distorted

I tried rotating a UIView using:
view.transform = CGAffineTransform.init(rotationAngle: 45)
only to see the edges of the view looked very distorted.
Is this normal? And if it is, are there any workarounds to straighten the edges?
EDIT
I found that adding the piece of code below works the best
view.layer.allowsEdgeAntialiasing = true
refer this link,
https://stackoverflow.com/a/8313978/6742121,
After that implement this for swift 3.0,
view.layer.borderWidth = 3
view.layer.borderColor = UIColor.clear.cgColor
view.layer.shouldRasterize = true
view.layer.rasterizationScale = UIScreen.main.scale()

Shadows slow up UIScrollView

I have a UIScrollView that pretty much functions like a Facebook news feed. I thought my elements were slowing the scroll fps down. By process of elimination, I found out that the shadows slow down my app.
The UIView boxes inside the scroll view have such configuration:
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSizeMake(0, 2)
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 0.15
Like a news feed, my scroll view has many boxes, therefore having UIViews with their own shadows. How do I go with this without slowing down my app?
There's a bunch of stuff on speeding up UIScrollViews:
CALayer - Shadow causes a performance hit?
https://markpospesel.wordpress.com/2012/04/03/on-the-importance-of-setting-shadowpath/
If you use custom CALayer instances -- especially with shadows -- stuff that requires processing power, you should use
scrollView.layer.shouldRasterize = YES;
scrollView.layer.rasterizationScale = [UIScreen mainScreen].scale;
Also a shadowPath could speed up your scrollview as well something like this:
[scrollView.layer setShadowPath:[[UIBezierPath bezierPathWithRect:myView.bounds] CGPath]];
Setting a shadowPath allows iOS to not recalculate how it should draw the shadow every time it draws the view, thus improving performance.
In swift, something like this:
let shadowPath = UIBezierPath(rect: view.bounds);
view.layer.masksToBounds = false;
view.layer.shadowColor = UIColor.blackColor().CGColor;
view.layer.shadowOffset = CGSizeMake(0, 0.5);
view.layer.shadowOpacity = 0.2;
view.layer.shadowPath = shadowPath.CGPath;
Setting the shadowPath will improve performance and look the same so long as your views are opaque. You could just set it to the bounds of the view or layer like so:
CGPathRef shadowPath = CGPathCreateWithRect(self.bounds, NULL);
self.layer.shadowPath = shadowPath;
CGPathRelease(shadowPath);
In Swift:
layer.shadowPath = CGPathCreateWithRect(bounds, nil)
The memory management is handled for you (discussed here).

Shadow in UIView without gradient ?

Does any one know how to achieve the shadow effect with no gradient? Like the screenshot show below
Another concern is the sequence of subviews, i.e the view in front may hide the effect of the view in behind. How to overcome this?
For the first problem you can change the shadowRadius of the shadow, for example:
//You must include QuartzCore framework (#import <QuartzCore/QuartzCore.h>)
view.layer.cornerRadius = 5;
view.layer.shadowRadius = 0; //The shadow should be rendered as a solid shape
view.layer.shadowOffset = CGSizeMake(0, 2);
view.layer.shadowOpacity = 0.5;
view.layer.shadowColor = [UIColor blackColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.shadowPath = path.CGPath; //This is very important!
Remember to always set the shadowPath! If you don't the performance of rendering the shadow will decrease a lot.
For the second problem, sorry but I don't think there's a way to let the shadow of an object appear over another view that is over the original one.

Resources