1. 程式人生 > >CALayer的mask屬性

CALayer的mask屬性

back spl ise tor DC prop gpa pos radi

可以對圖層按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

CALayer的mask屬性