CALayer's mask property

// #import "ViewController.h"
 // 
// @interface ViewController ()
 // 
// @end
 // 
// @implementation ViewController
 // 
// - (void)viewDidLoad {
 // 
//     [super viewDidLoad];
 // 
//     // Create a blue
 layer //     CALayer *foregroundLayer = [CALayer layer];
 //     foregroundLayer.bounds = CGRectMake(0, 0, 100, 100);
 //     foregroundLayer.backgroundColor = [UIColor redColor] .CGColor;
 // 
//     // Create a path
 //    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];
    
//     // Create a CAShape
 //     CALayer *bgLayer = [CALayer layer];
 // 
//     // Set size, color and position
 //     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];
    
    
    // Create a CAShapeLayer as MaskLayer 
    circle = [CAShapeLayer layer];
    
    // Set the path 
    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;
    
    // Set maskLayer 
    bgLayer.mask = circle;
    
    [self.view.layer addSublayer:bgLayer];
    
    // Adding a timer this is just an additional animation
     // 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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324685193&siteId=291194637