In the project, I encountered a situation of setting shadows for a view or button with rounded corners, but found that adding shadows was invalid by using the conventional writing method. I tried adding a CALayer below the control that needed to add shadows. It was ok if the control remained still, but if the position of the control changed, the CALayer itself had animation properties. Will cause the shadow will slow the control one step, then you need to disable animation can do, but after the animation is disabled you do not want to control the movement of very sudden, that is very embarrassing, and so write down the code long, god annoying, so found the following method

If you subtract more than the main layer, the shadow will not show up. Therefore, set the code that cuts beyond the main layer to NO

view.clipsToBounds=NO;
Copy the code
// the color of the shadow
view.layer.shadowColor = [UIColor blackColor].CGColor;

// shadowOffset,x to the right by 1, y to the down by 1, default (0, -3), this is used with shadowRadius
view.layer.shadowOffset = CGSizeMake(1.1);

// Shadow radius, default 3
view.layer.shadowRadius = 4;

// Shadow transparency, default 0
view.layer.shadowOpacity = 0.5;
Copy the code

clipsToBounds(UIView)

MasksToBounds (CALayer) is a ounds layer on the layer of the View. If you are in the ounds View, it can be used to cut off any part of the View that is outside the parent layer. When clipsToBounds executes beyond the parent View layer, it calls its own layer maskToBounds method

ClipsToBounds =NO if you want to add rounded corners and shadows to a view, you can set masksToBounds =NO if you want to add rounded corners and shadows to a view.

  • The difference between CALayer and UIView