iOS mask 层和 CAShapeLayer层中间挖去一部分的 源码分析

iOS mask 层和 CAShapeLayer层中间挖去一部分的 源码分析

//
//  ViewController.m
//  test_maskLayer_01
//
//  Created by admin on 3/6/16.
//  Copyright © 2016 jeffasd. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


//@property (nonatomic, weak) IBOutlet UIView *containerView;


@property(nonatomic, strong)CAShapeLayer *maskeLayer;

//@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


//    [self test0];
    
//    [self test2];
    
    [self test3];
}

/**方法3*/
- (void)test3{
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIBezierPath *bPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 100, 100)];
    UIBezierPath *bPath = [UIBezierPath bezierPathWithRect:self.view.frame];
    
    UIBezierPath *bsPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(120, 120, 60, 60)];
    [bPath appendPath:bsPath];//追加
    
    //在设置了shapeLayer的fillRule后这个设置就不起作用了
    bPath.usesEvenOddFillRule = YES;
//    bPath.usesEvenOddFillRule = NO;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor blueColor].CGColor;

//        shapeLayer.fillRule = kCAFillRuleNonZero;
    shapeLayer.fillRule = kCAFillRuleEvenOdd;
    
//    shapeLayer.lineWidth = 5;
//        shapeLayer.lineJoin = kCALineJoinBevel;
//        shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = bPath.CGPath;
    
    [self.view.layer addSublayer:shapeLayer];
    
}

- (void)test2{
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
//    [path addArcWithCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2) radius:50 startAngle:0 endAngle:M_PI *2 clockwise:YES];
    
//    [path moveToPoint:CGPointMake(CGRectGetWidth(self.view.bounds)/2+50, CGRectGetHeight(self.view.bounds)/2)];
//    [path addArcWithCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];
    
    UIBezierPath *appendPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];
    
    [path appendPath:appendPath];
    
//    path.usesEvenOddFillRule = YES;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = path.CGPath;
    shapeLayer.fillColor= [UIColor cyanColor].CGColor;  //其他颜色都可以,只要不是透明的
    shapeLayer.fillRule=kCAFillRuleEvenOdd;
    
    shapeLayer.strokeColor = [UIColor cyanColor].CGColor;
    
    [shapeLayer removeFromSuperlayer];
    
//    self.view.layer.mask = shapeLayer;
    
    [self.view.layer addSublayer:shapeLayer];
    

}

- (void)test0{
    //create path
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(200, 150)];
    [path addArcWithCenter:CGPointMake(150, 150) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];
    [path moveToPoint:CGPointMake(250, 150)];
    [path addArcWithCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:YES];
    
    //create shape layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor blueColor].CGColor;
//    shapeLayer.fillRule = kCAFillRuleNonZero;
    shapeLayer.fillRule = kCAFillRuleEvenOdd;
    
    shapeLayer.lineWidth = 5;
//    shapeLayer.lineJoin = kCALineJoinBevel;
//    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = path.CGPath;
    
    //add it to our view
    [self.view.layer addSublayer:shapeLayer];
}

- (void)test1{
    //create path
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(200, 150)];
    [path addArcWithCenter:CGPointMake(150, 150) radius:50 startAngle:0 endAngle:2*M_PI clockwise:YES];
    [path moveToPoint:CGPointMake(250, 150)];
    [path addArcWithCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2*M_PI clockwise:YES];
    [path moveToPoint:CGPointMake(300, 150)];
    [path addArcWithCenter:CGPointMake(150, 150) radius:150 startAngle:0 endAngle:2*M_PI clockwise:YES];
    
    //create shape layer
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    shapeLayer.fillColor = [UIColor blueColor].CGColor;
    //shapeLayer.fillRule = kCAFillRuleNonZero;
    shapeLayer.fillRule = kCAFillRuleEvenOdd;
    
    shapeLayer.lineWidth = 5;
    shapeLayer.lineJoin = kCALineJoinBevel;
    shapeLayer.lineCap = kCALineCapRound;
    shapeLayer.path = path.CGPath;
    
    //add it to our view
//    [self.containerView.layer addSublayer:shapeLayer];
    [self.view.layer addSublayer:shapeLayer];
}

- (void)testShapeLayerOne{
    
    
    
#if 0
    _maskeLayer = [CAShapeLayer layer];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.frame];
    
    [_maskeLayer removeFromSuperlayer];
    
    //    _maskeLayer.path = path.CGPath;
    self.view.layer.mask = _maskeLayer;
    
    
    
    UIBezierPath *bPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)];
    bPath.usesEvenOddFillRule = YES;
    UIBezierPath *bsPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 60, 60)];
    [bPath appendPath:bsPath];//裁剪
    [bPath addClip];//绘制图像
    
    _maskeLayer.path = bPath.CGPath;
    
    
    //    _maskeLayer.path = bsPath.CGPath;
    
    
    //    UIBezierPath *piePath = [UIBezierPath bezierPath];
    //    CGPoint center = self.view.center;
    //    [piePath moveToPoint:center];
    //    [piePath addArcWithCenter:center  radius:radius  startAngle:topAngle  endAngle:endAngle  clockwise:YES];
    //    [piePath closePath];
    //
    //    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:outCircleRect];
    //    UIBezierPath *innerCirclePath = [UIBezierPath bezierPathWithOvalInRect:innerCircleRect];
    //    [circlePath appendPath:innerCirclePath];
    //    [circlePath setUsesEvenOddFillRule:YES];  //后便会有说明
    //    [circlePath addClip];
    
    
    UIView *view = [UIView new];
    //    view.frame = CGRectMake(100, 100, 200, 500);
    
    view.frame = self.view.frame;
    
    [self.view addSubview:view];
    view.backgroundColor = [UIColor redColor];
    
#endif
    
#if 0
    
    //    self.view.backgroundColor = [UIColor redColor];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.view.bounds];
    [path addArcWithCenter:CGPointMake(CGRectGetWidth(self.view.bounds)/2, CGRectGetHeight(self.view.bounds)/2) radius:50 startAngle:0 endAngle:M_PI *2 clockwise:YES];
    path.usesEvenOddFillRule = YES;
    
    
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame         = self.view.frame;                // 与showView的frame一致
    shapeLayer.position = self.view.center;
    shapeLayer.fillColor= [UIColor blackColor].CGColor;  //其他颜色都可以,只要不是透明的
    
    //设置线条的宽度和颜色
    shapeLayer.lineWidth = 1.0f;
    shapeLayer.strokeColor = [UIColor redColor].CGColor;
    
    //创建出圆形贝塞尔曲线
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    circlePath.usesEvenOddFillRule = YES;
    
    
    shapeLayer.path = circlePath.CGPath;
    
    //    shapeLayer.path = path.CGPath;
    
    
    shapeLayer.fillRule=kCAFillRuleEvenOdd;
    
    
    
    
    
    [self.view.layer addSublayer:shapeLayer];
    
    //    UIView *translucentView = [UIView new];
        translucentView.frame = self.imaegView.bounds;
    //
    //    translucentView.frame = self.view.bounds;
    //
        translucentView.backgroundColor = [UIColor blackColor];
    //    translucentView.backgroundColor = [UIColor cyanColor];
        translucentView.alpha = 0.5;
        translucentView.layer.mask = shapeLayer;
    //
        [self.view addSubview:translucentView];
    
    
#endif
    
    //    //创建出CAShapeLayer
    //    self.shapeLayer = [CAShapeLayer layer];
    //    self.shapeLayer.frame = CGRectMake(0, 0, 200, 200);//设置shapeLayer的尺寸和位置
    //    self.shapeLayer.position = self.view.center;
    //    self.shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor
    //
    //    //设置线条的宽度和颜色
    //    self.shapeLayer.lineWidth = 1.0f;
    //    self.shapeLayer.strokeColor = [UIColor redColor].CGColor;
    //
    //    //创建出圆形贝塞尔曲线
    //    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 200, 200)];
    //
    //    //让贝塞尔曲线与CAShapeLayer产生联系
    //    self.shapeLayer.path = circlePath.CGPath;
    //
    //    //添加并显示
    //    [self.view.layer addSublayer:self.shapeLayer];
    
    CAShapeLayer *myLayer = (CAShapeLayer*) self.view.layer; //size: 320 X 480
    UIBezierPath *testPath = [UIBezierPath bezierPathWithOvalInRect:(CGRect){{100, 100}, 100, 100}]; //a simple circle
    myLayer.fillRule = kCAFillRuleNonZero; // have tried this as well: kCAFillRuleEvenOdd;
    myLayer.path = testPath.CGPath;
    myLayer.fillColor = [UIColor whiteColor].CGColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值