CAGradientLayer 绘制渐变
UIBezierPath 绘制图形路径
CAShapeLayer 根据路径绘图
//
// ViewController.m
// Fill
//
// Created by Mr.H on 2018/8/29.
// Copyright © 2018年 Mr.H. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
/** <#statements#> */
@property (nonatomic,strong) CAGradientLayer *layer;
/** <#statements#> */
@property (nonatomic,assign) CGFloat number;
/** <#statements#> */
@property (nonatomic,assign) BOOL isNo;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.number = 0.1;
self.layer = [[CAGradientLayer alloc] init];
self.layer.frame = (CGRect){CGPointZero, CGSizeMake(200, 200)};
self.layer.position = self.view.center;
[self.view.layer addSublayer:self.layer];;
self.layer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor yellowColor].CGColor];
self.layer.locations = @[@(0.0),@(self.number)];
self.layer.startPoint = CGPointMake(0, 0);
self.layer.endPoint = CGPointMake(1, 1);
// self.layer.cornerRadius = 10;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
// shapeLayer.frame = CGRectMake(0, 0, 50, 50);
shapeLayer.strokeColor = [UIColor greenColor].CGColor;
shapeLayer.lineWidth = 1;
// shapeLayer.fillColor = [UIColor clearColor].CGColor;
UIBezierPath *path = [UIBezierPath bezierPath];
[path addArcWithCenter:CGPointMake(100, 100) radius:100 startAngle:0 endAngle:180 clockwise:YES];
// [path moveToPoint:CGPointMake(0 , 200)];
// [path addLineToPoint:CGPointMake(0, 0)];
//
// [path addLineToPoint:CGPointMake(30, 30)];
//
// [path addLineToPoint:CGPointMake(60, 60)];
//
// [path addLineToPoint:CGPointMake(90, 30)];
//
// [path addLineToPoint:CGPointMake(100, 100)];
// [path addLineToPoint:CGPointMake(200, 200)];
//
//
// [path moveToPoint:CGPointMake(100 , 20)];
//
// [path addLineToPoint:CGPointMake(100 , 150)];
//
// [path addLineToPoint:CGPointMake(200, 200)];
//
// [path closePath];
shapeLayer.path = path.CGPath;
// [self.layer addSublayer:shapeLayer];
self.layer.mask = shapeLayer;
[self performSelector:@selector(aaaaa) withObject:self afterDelay:0.2];
}
- (void)aaaaa {
self.layer.locations = @[@(0.0),@(self.number)];
if (self.isNo) {
self.layer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor yellowColor].CGColor];
self.number = self.number - 0.05;
if (self.number < 0) {
self.number = 0;
self.isNo = !self.isNo;
}
} else {
self.layer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor yellowColor].CGColor];
self.number = self.number + 0.05;
if (self.number > 1) {
self.number = 1;
self.isNo = !self.isNo;
}
}
[self performSelector:@selector(aaaaa) withObject:self afterDelay:0.1];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
网友评论