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

来源:互联网 发布:windows无法启动怎么办 编辑:程序博客网 时间:2024/04/30 09:17

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
原创粉丝点击