利用mask layer 勾View

时间:2024-10-14 20:37:44
  1. #define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
  2. #define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
  3. - (void)addMask{
  4. UIButton * _maskButton = [[UIButton alloc] init];
  5. [_maskButton setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  6. [_maskButton setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.7]];
  7. [self.view addSubview:_maskButton];
  1. //把下面代码加入到view的-drawRect'中便可以实现
  2. //create path
  3. UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  4. // MARK: circlePath
  5. [path appendPath:[UIBezierPath bezierPathWithArcCenter:CGPointMake(SCREEN_WIDTH / 2, 200) radius:100 startAngle:0 endAngle:2*M_PI clockwise:NO]];
  6. // MARK: roundRectanglePath
  7. [path appendPath:[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 400, SCREEN_WIDTH - 200, 100) cornerRadius:15] bezierPathByReversingPath]];
  8. CAShapeLayer *shapeLayer = [CAShapeLayer layer];
  9. shapeLayer.path = path.CGPath;
  10. [_maskButton.layer setMask:shapeLayer];
  11. }