标签:
CAGradientLayer 简介
CAGradientLayer 坐标系统
CAGradientLayer 属性介绍
CAGradientLayer 案例
三原色渐变
- (void)viewDidLoad {
[super viewDidLoad];
/**< 初始化colorsView */
UIView *colorsView = [[UIView alloc] init];
colorsView.bounds = CGRectMake(0, 0, 220, 220);
colorsView.center = self.view.center;
[self.view addSubview:colorsView];
/**< 初始化渐变层 */
CAGradientLayer *gradientColorLayer = [CAGradientLayer layer];
gradientColorLayer.frame = colorsView.bounds;
[colorsView.layer addSublayer:gradientColorLayer];
/**< 设置颜色组 */
gradientColorLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
/**< 设置颜色分割点 */
gradientColorLayer.locations = @[@(0.25), @(0.5), @(0.75)];
/**< 设置渐变颜色方向 */
// 1、起始位置
gradientColorLayer.startPoint = CGPointMake(0, 0);
// 2、结束位置
gradientColorLayer.endPoint = CGPointMake(0, 1);
}
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) CAGradientLayer *gradientLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加 CAGradientLayer
[self.view.layer addSublayer:self.gradientLayer];
// 延时3秒执行,实现分割点动画(隐式动画)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 颜色分割点效果
self.gradientLayer.locations = @[@(0.4), @(0.5), @(0.6)];
});
}
#pragma mark - Getter methods
- (CAGradientLayer *)gradientLayer {
if (!_gradientLayer) {
// 初始化并创建
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = CGRectMake(0, 0, 250, 250);
_gradientLayer.position = self.view.center;
_gradientLayer.borderWidth = 1.f;
// 设置颜色
_gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
// 设置颜色渐变方向
_gradientLayer.startPoint = CGPointMake(0, 0);
_gradientLayer.endPoint = CGPointMake(1, 0);
// 设置颜色分割点
_gradientLayer.locations = @[@(0.25), @(0.5), @(0.75)];
}
return _gradientLayer;
}
@end
色差动画
#import "ViewController.h"
@interface ViewController () {
NSTimer *_timer; /**< 定时器 */
}
@property (strong, nonatomic) CAGradientLayer *gradientLayer; /**< 渐变层 */
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
// 创建背景图片
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]];
imageView.bounds = CGRectMake(0, 0, 260, 450);
imageView.center = self.view.center;
[self.view addSubview:imageView];
// 添加渐变层
[imageView.layer addSublayer:self.gradientLayer];
// 初始化定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:1.f
target:self
selector:@selector(respondsToTimerEvent)
userInfo:nil
repeats:YES];
}
#pragma mark - Timer and animations methods
- (void)respondsToTimerEvent {
// 设定颜色组动画
self.gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor colorWithRed:arc4random()%255 / 255.f
green:arc4random()%255 / 255.f
blue:arc4random()%255 / 255.f
alpha:1].CGColor];
// 设置颜色分割点动画
self.gradientLayer.locations = @[@(arc4random() %10 / 10.f), @(1.f)];
}
#pragma mark - Getter methods
- (CAGradientLayer *)gradientLayer {
if (!_gradientLayer) {
// 初始化渐变层
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = CGRectMake(0, 0, 260, 450);
// 设置颜色渐变方向
_gradientLayer.startPoint = CGPointMake(0, 0);
_gradientLayer.endPoint = CGPointMake(0, 1);
// 设定颜色组
_gradientLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
// 设定颜色分割点
_gradientLayer.locations = @[@(0.5f), @(1.f)];
}
return _gradientLayer;
}
@end
标签:
原文地址:http://www.cnblogs.com/YangFuShun/p/5750258.html