Add shadow to UIView

The usual way, set the layer property of the view

    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(50,50, 200, 300)];
    v.backgroundColor = [UIColor blueColor];
    v.layer.shadowColor = [UIColor redColor].CGColor; //opaque black
    v.layer.shadowRadius = 0;   //3
    v.layer.shadowOffset = CGSizeMake(20, 50);
    v.layer.shadowOpacity = 0.1;    //0

  

shadowColor: shadow color, default black
shadowOpacity: the transparency of the color, the default is 0
shadowOffset: cheap amount of shadow, (x, y)
shadowRadius: The shadow radius, the default value is 3, when it is equal to 0, the actual effect is that the shadow is a straight line. 

Sometimes it is necessary to customize the shadow effect on some edges. At this time, only using shadedowoffset obviously cannot meet the needs, so we can use the shadowpath property to solve it.
When shawdowoffset and shadowpath exist at the same time, both values ​​take effect, and the reference point of shawdowoffset offset changes from (0, 0) to the x and y coordinates of shadowpath.
1. Add shadows only on one side
    v.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, -10, 200, 20)].CGPath; //The height ensures that the shadow can seamlessly connect to the top of the view

 2. There are shadows all around at the same time

    v.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, 220, 320)].CGPath; 

 3. Shadows on the left and right sides

  v.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(-10, 0, 220, 300)].CGPath; 

  







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325945962&siteId=291194637