iOS开发-绘制OC学习项目

iOS实现遮罩镂空效果2种方式

2017-08-15  本文已影响660人  千年积木
【1】
- (void)addMask{
    /*
   mask不是遮罩,不是add到layer上的另一个layer,而是控制layer本身渲染的一个layer。
    效果是:比如imageLayer有一个maskLayer作为mask(注意maskLayer可以不跟imageLayer大小一样),
    那maskLayer透明的地方,imageLayer就不会渲染,而是变透明,显示出imageLayer之后的内容,
    maskLayer不透明的地方,imageLayer就会正常渲染,显示出imageLayer本来的内容
    如果maskLayer比imageLayer要小,那默认的maskLayer之外的地方都是透明的,都不会渲染。
    
    注意:作为mask的layer不能有superLayer或者subLayer!
    
    */
    
        UIView *backgroundView = [[UIView alloc] init];
        backgroundView.frame = self.bounds;
        
        //背景色不能为透明
        backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        [self addSubview:backgroundView];
    
        // 创建一个全屏大的path
         UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
         // 创建一个圆形path
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x/2, self.center.y *3/2)radius:SCREEN_WIDTH/5 startAngle:0 endAngle:2 * M_PI clockwise:NO];
    
    
    // UIBezierPath 有个原生的方法- (void)appendPath:(UIBezierPath *)bezierPath, 这个方法作用是俩个路径有叠加的部分则会镂空.
    //  这个方法实现原理应该是path的FillRule 默认是FillRuleEvenOdd(CALayer 有一个fillRule属性的规则就有kCAFillRuleEvenOdd), 而EvenOdd 是一个奇偶规则,奇数则显示,偶数则不显示.叠加则是偶数故不显示
    
         [path appendPath:circlePath];
    
         CAShapeLayer *shapeLayer = [CAShapeLayer layer];
         shapeLayer.path = path.CGPath;
    
         backgroundView.layer.mask = shapeLayer;
}
【2】
- (void)addView{

    UIView *backgroundView = [[UIView alloc] init];
    backgroundView.frame = self.bounds;
  
    //必须透明
    backgroundView.backgroundColor = [UIColor clearColor];
    [self addSubview:backgroundView];
    
    // 创建一个全屏大的path
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
    // 创建一个圆形path
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.center.x/2, self.center.y *3/2)radius:W/5 startAngle:0 endAngle:2 * M_PI clockwise:NO];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillRule = kCAFillRuleEvenOdd;
    shapeLayer.fillColor = [UIColor blackColor].CGColor;
    shapeLayer.opacity = 0.5;

    [backgroundView.layer addSublayer:shapeLayer];
}
上一篇下一篇

猜你喜欢

热点阅读