zoukankan      html  css  js  c++  java
  • CALayer的mask属性

    可以对图层按path进行指定裁剪

    //#import "ViewController.h"
    //
    //@interface ViewController ()
    //
    //@end
    //
    //@implementation ViewController
    //
    //- (void)viewDidLoad {
    //
    //    [super viewDidLoad];
    //
    //    // 创建一个蓝色的Layer
    //    CALayer *foregroundLayer        = [CALayer layer];
    //    foregroundLayer.bounds          = CGRectMake(0, 0, 100, 100);
    //    foregroundLayer.backgroundColor = [UIColor redColor].CGColor;
    //
    //    // 创建一个路径
    //    UIBezierPath *apath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 60, 60)];
    //
    //    // 创建maskLayer
    //    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    //    maskLayer.path = apath.CGPath;
    //    maskLayer.fillColor = [UIColor greenColor].CGColor;
    //    maskLayer.fillRule = kCAFillRuleEvenOdd;
    //
    //    // 设置位置
    //    foregroundLayer.position = self.view.center;
    //    // 设置mask
    //    foregroundLayer.mask = maskLayer;
    //
    //    [self.view.layer addSublayer:foregroundLayer];
    //
    //}
    //
    //@end
    
    
    #import "ViewController.h"
    
    static CGFloat num;
    
    @interface ViewController ()
    
    @property (nonatomic, strong) CAShapeLayer *circle;
    @property (nonatomic, strong) CADisplayLink *link;
    
    @end
    
    @implementation ViewController
    
    @synthesize circle;
    
    - (void)viewDidLoad {
        
        [super viewDidLoad];
        
    //    //创建一个CAShape
    //    CALayer *bgLayer = [CALayer layer];
    //
    //    //设置大小颜色和位置
    //    bgLayer.bounds          = CGRectMake(0, 0, 200, 200);
    //    bgLayer.backgroundColor = [UIColor redColor].CGColor;
    //    bgLayer.position        = self.view.center;
        
        CAGradientLayer *bgLayer = [CAGradientLayer layer];
        bgLayer.bounds = CGRectMake(0, 0, 200, 200);
        bgLayer.position        = self.view.center;
    
        bgLayer.colors = [NSArray arrayWithObjects:
                           (id)[UIColor colorWithRed:0 green:143/255.0 blue:234/255.0 alpha:1.0].CGColor,
                           (id)[UIColor colorWithRed:0 green:173/255.0 blue:234/255.0 alpha:1.0].CGColor,
                           (id)[UIColor whiteColor].CGColor, nil];
        
        
        //创建一个CAShapeLayer作为MaskLayer
        circle = [CAShapeLayer layer];
        
        //设置路径
        circle.path      = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)
                                                          radius:20
                                                      startAngle:0
                                                        endAngle:2 * M_PI
                                                       clockwise:YES].CGPath;
    //    circle.lineWidth = 5;
    //    circle.fillColor = [UIColor greenColor].CGColor;
    //    circle.fillRule  = kCAFillRuleEvenOdd;
        
        //设置maskLayer
        bgLayer.mask = circle;
        
        [self.view.layer addSublayer:bgLayer];
        
        //添加计时器 这个只是一个附加动画
        //self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(action)];
        //[self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    }
    
    - (void)action {
        
        num ++;
        
        circle.path      = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100)
                                                          radius:20 + num
                                                      startAngle:0
                                                        endAngle:2 * M_PI
                                                       clockwise:YES].CGPath;
        
        if (num > 1000) {
            [self.link invalidate];
        }
    }
    
    @end
    此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935. 我的gitHub: (学习代码都在gitHub) https://github.com/nwgdegitHub/
  • 相关阅读:
    错误与异常_2-11选择题
    错误与异常_2-10选择题
    错误与异常_2-8选择题
    错误与异常_2-7选择题
    C#定时器
    C#动态webservice调用接口
    C# webservice返回Xml格式文件
    C#创建简单的Xml文件
    获取数据库中指定类型的数据库名称
    C#生成Xml以UTF-8无BOM格式编码
  • 原文地址:https://www.cnblogs.com/liuw-flexi/p/8918582.html
Copyright © 2011-2022 走看看