iOS 利用mask layer 使view中扣掉一块露出下边的view

[objc] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width  
  2. #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height  
  3.   
  4. - (void)addMask{  
  5.     UIButton * _maskButton = [[UIButton alloc] init];  
  6.     [_maskButton setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];  
  7.     [_maskButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];  
  8.     [self.view addSubview:_maskButton];  
  9.       
  10.     //create path  
  11.     UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];  
  12.       
  13.     // MARK: circlePath  
  14.     [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(SCREEN_WIDTH / 2, 200) radius:100 startAngle:0 endAngle:2*M_PI clockwise:NO]];  
  15.       
  16.     // MARK: roundRectanglePath  
  17.     [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 400, SCREEN_WIDTH - 22 * 20, 100) cornerRadius:15] bezierPathByReversingPath]];  
  18.       
  19.     CAShapeLayer *shapeLayer = [CAShapeLayer layer];  
  20.       
  21.     shapeLayer.path = path.CGPath;  
  22.       
  23.     [_maskButton.layer setMask:shapeLayer];  
  24. }  
原文地址:https://www.cnblogs.com/nngh/p/5577364.html