To make a picture of the hexagon shadow

What does not say the first effect diagram

To make a picture of the hexagon shadow
Paste_Image.png

This effect is very simple to write, the main use of the following knowledge points

  • Layer mask: Layer Mask
  • Layer shadowPath: draw custom shape shadows
  • UIBezierPath: draw a hexagonal route to finish the following knowledge of the code
  • Hexagon drawn line – (CGPathRef) getCGPath: (CGFloat) viewWidth{UIBezierPath * path [UIBezierPath = bezierPath]; path.lineWidth = 2; [[UIColor whiteColor] setStroke]; [path moveToPoint:CGPointMake ((sin (M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 4)]; (addLineToPoint:CGPointMake) [path (viewWidth / 2) 0, [path (addLineToPoint:CGPointMake)]; viewWidth ((sin (M_1_PI / 180 * 60)) * (viewWidth / 2)), (viewWidth / 4)] [path (addLineToPoint:CGPointMake); viewWidth ((sin (M_1_PI / 180 * 60)) * (viewWidth / 2) (viewWidth).) / 2 + (viewWidth / 4)]; (addLineToPoint:CGPointMake) [path (viewWidth / 2), viewWidth [path (CGPointMake)]; addLineToPoint: (sin (M_1_PI / 180 * 60)) * (viewWidth / 2), (viewWidth / 2) + (viewWidth / 4))]; [path closePath]; return path.CGPath;}
  • Draw a hexagon layer, and assign image to contents CGRect hexagnoRect = self.bounds; / / draw a hexagonal layer, and copy a image to his contents CALayer *hexagonLayer [CALayer = layer]; hexagonLayer.frame = hexagnoRect; CAShapeLayer * shapLayer = [CAShapeLayer layer]; shapLayer.lineWidth = 1; shapLayer.strokeColor = [UIColor whiteColor].CGColor; shapLayer.path = [self getCGPath:hexagnoRect.size.width-20]; hexagonLayer.mask = shapLayer; hexagonLayer.contents = (__bridge ID _Nullable) (self.image.CGImage);
  • Create a CALayer, the hexagonal layer added to the CALayer, and draw the CALayer *completeLayer = [CALayer layer] fuzzy shadow; completeLayer.frame = CGRectMake (10, 10, self.bounds.size.width-10, self.bounds.size.height-10); [completeLayer addSublayer:hexagonLayer]; completeLayer.shadowOpacity = 1.0F; completeLayer.shadowPath = [self getCGPath:hexagnoRect.size.width]; completeLayer.shadowOffset = CGSizeMake (-10, -10); completeLayer.shadowColor = self.hg_shadowColor.CGColor; [self.layer addSublayer:completeLayer]; this completed only To make a picture of the hexagon shadow
    .Gif
    Demo I see shadow stamp
    feel good for a star.