iOS 渐变色和毛玻璃效果
2016-11-17 本文已影响191人
HelloRyan
使用CAGradientLayer实现渐变色效果
#define kRGBColor(r, g, b, a) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha: a]
// 创建 CAGradientLayer 对象
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
// 设置 gradientLayer 的 Frame
gradientLayer.frame = self.view.bounds;
// 创建渐变色数组,需要转换为CGColor颜色
gradientLayer.colors = @[(id)[UIColor whiteColor].CGColor,(id)kRGBColor(30, 144, 255, 1).CGColor];
// 设置颜色分割点
gradientLayer.locations = @[@(0.1f) ,@(1)];
// 设置渐变颜色方向,左上点为(0,0), 右下点为(1,1)
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);
// 添加渐变色到创建的 UIView 上
[self.view.layer addSublayer:gradientLayer];
毛玻璃效果
// 创建imageView
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, self.view.bounds.size.width, 200)];
[imageView setImage:[UIImage imageNamed:@"cr7.jpg"]];
[self.view addSubview:imageView];
// blur效果
UIVisualEffectView *visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
visual.frame = imageView.bounds;
visual.alpha = 0.8f;
[imageView addSubview:visual];