CAGradientLayer可以用来生成2种活着多种颜色的平滑渐变,像彩虹,金属风格的光影效果等,使用CAGradientLayer的好处在于使用了硬件加速。
具体使用很简单,不用多少,直接上代码和效果图即可。
//
// CAGradientLayer.m
// iOSanimation
//
// Created by biyabi on 15/11/30.
// Copyright © 2015年 caijunrong. All rights reserved.
//
#import "CAGradientLayerVC.h"
@interface CAGradientLayerVC ()
@property (nonatomic, strong) UIView *GView;
@end
@implementation CAGradientLayerVC
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor whiteColor]];
//增加CAGradientLayer渐变效果
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.GView.bounds;
[self.GView.layer addSublayer:gradientLayer];
//注意这里是数组,而且要进行类型转换
gradientLayer.colors = @[(__bridge id)[UIColor lightGrayColor].CGColor,(__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor groupTableViewBackgroundColor].CGColor];
//如果只是基础渐变的话,这个可以不用加,按比例
gradientLayer.locations = @[@0,@0.5,@0.8];
//启始点,比例
gradientLayer.startPoint = CGPointMake(0, 0);
//结束点,比例
gradientLayer.endPoint = CGPointMake(1, 1);
}
- (UIView *)GView{
if (!_GView) {
_GView = [[UIView alloc]initWithFrame:CGRectMake(30, 94, [UIScreen mainScreen].bounds.size.width - 30*2,[UIScreen mainScreen].bounds.size.width - 30*2 )];
[self.view addSubview:_GView];
}
return _GView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
效果图图下: