文章目录
CAGradientLayer 渐变图层属性介绍示例
Core Graphics 实现渐变色示例
Demo地址总结扩展阅读
CAGradientLayer 渐变图层
CAGradientLayer 是 CALayer 的子类,用来制作渐变效果的图层。
属性介绍
colors
locations
startPoint
endPoint
示例
矩形渐变图层
CAGradientLayer
*gradientLayer
= [CAGradientLayer layer
];
gradientLayer
.frame
= CGRectMake(0, 64, [UIScreen mainScreen
].bounds
.size
.width
, [UIScreen mainScreen
].bounds
.size
.height
-64);
gradientLayer
.colors
= @[(id
)[UIColor redColor
].CGColor
,(id
)[UIColor greenColor
].CGColor
,(id
)[UIColor blueColor
].CGColor
];
gradientLayer
.startPoint
= CGPointMake(0, 0);
gradientLayer
.endPoint
= CGPointMake(1, 0);
[self.view
.layer addSublayer
:gradientLayer
];
不规则形状
由于 CAGradientLayer 不支持路径填充,所以只能绘制矩形的渐变。我们可以通过遮罩层 来显示不规则渐变图层,使用 CAShapeLayer 赋值给 CAGradientLayer 的 mask 属性,及可实现不规则图形的渐变效果。mask 也可以是图像等其他内容,重叠的部分会被显示,否则被隐藏。
#define getRandomNumberFromAtoB(A,B) (int)(A+(arc4random()%(B-A+1)))
UIBezierPath
*path
= [UIBezierPath bezierPath
];
NSInteger num
= 50;
CGFloat itemWidth
= [UIScreen mainScreen
].bounds
.size
.width
/num
;
for (int i
=0; i
<=num
; i
++) {
if (i
) {
[path addLineToPoint
:CGPointMake(i
*itemWidth
, getRandomNumberFromAtoB(100, 200))];
}
else{
[path moveToPoint
:CGPointMake(i
*itemWidth
, getRandomNumberFromAtoB(100, 200))];
}
}
[path addLineToPoint
:CGPointMake([UIScreen mainScreen
].bounds
.size
.width
, 300)];
[path addLineToPoint
:CGPointMake(0, 300)];
[path closePath
];
CAShapeLayer
*maskLayer
= [CAShapeLayer layer
];
maskLayer
.path
= path
.CGPath
;
gradientLayer
.mask
= maskLayer
;
Core Graphics 实现渐变色
CAGradientLayer 是对 Core Graphics 底层的一个面向对象的封装实现,我们直接使用 Core Graphics 来实现渐变色。Core Graphics 库中有两个方法用于绘制渐变颜色:CGContextDrawLinearGradient、CGContextDrawRadialGradient,前者可生成线性渐变,后者可生成径向渐变。Core Graphics 可以通过 CGMutablePathRef来绘制出各种形状里的渐变效果。
示例
线性渐变
首先创建一个path(形状)
CGMutablePathRef path
= CGPathCreateMutable();
CGPathMoveToPoint(path
, NULL, 0, 100);
CGPathAddLineToPoint(path
, NULL, [UIScreen mainScreen
].bounds
.size
.width
, 150);
CGPathAddLineToPoint(path
, NULL, [UIScreen mainScreen
].bounds
.size
.width
, 400);
CGPathAddLineToPoint(path
, NULL, 0, 350);
CGPathCloseSubpath(path
);
给 path 设置渐变效果
NSArray
*colors
= @[(id
)[UIColor redColor
].CGColor
,(id
)[UIColor greenColor
].CGColor
,(id
)[UIColor blueColor
].CGColor
];
UIGraphicsBeginImageContext(self.view
.bounds
.size
);
CGContextRef context
= UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace
= CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient
= CGGradientCreateWithColors(colorSpace
, (__bridge CFArrayRef
) colors
, NULL);
CGRect pathRect
= CGPathGetBoundingBox(path
);
CGPoint startPoint
= CGPointMake(CGRectGetMinX(pathRect
), CGRectGetMidY(pathRect
));
CGPoint endPoint
= CGPointMake(CGRectGetMaxX(pathRect
), CGRectGetMidY(pathRect
));
CGContextSaveGState(context
);
CGContextAddPath(context
, path
);
CGContextClip(context
);
CGContextDrawLinearGradient(context
, gradient
, startPoint
, endPoint
, kCGGradientDrawsBeforeStartLocation
);
CGContextRestoreGState(context
);
CGGradientRelease(gradient
);
CGColorSpaceRelease(colorSpace
);
CGPathRelease(path
);
从当前上下文获取图像并显示
UIImage
*img
= UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView
*imgView
= [[UIImageView alloc
] initWithImage
:img
];
[self.view addSubview
:imgView
];
径向渐变
NSArray
*colors
= @[(id
)[UIColor redColor
].CGColor
,(id
)[UIColor greenColor
].CGColor
,(id
)[UIColor blueColor
].CGColor
];
UIGraphicsBeginImageContext(self.view
.bounds
.size
);
CGContextRef context
= UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace
= CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient
= CGGradientCreateWithColors(colorSpace
, (__bridge CFArrayRef
) colors
, NULL);
CGRect pathRect
= CGPathGetBoundingBox(path
);
CGPoint center
= CGPointMake(CGRectGetMidX(pathRect
), CGRectGetMidY(pathRect
));
CGFloat radius
= MAX(pathRect
.size
.width
/ 2.0, pathRect
.size
.height
/ 2.0) * sqrt(2);
CGContextSaveGState(context
);
CGContextAddPath(context
, path
);
CGContextClip(context
);
CGContextDrawRadialGradient(context
, gradient
, center
, 0, center
, radius
, kCGGradientDrawsBeforeStartLocation
);
CGContextRestoreGState(context
);
CGGradientRelease(gradient
);
CGColorSpaceRelease(colorSpace
);
CGPathRelease(path
);
Demo地址
总结
使用 Core Graphics 实现渐变效果相对麻烦,幸好官方帮你进行了封装产生了 CAGradientLayer 渐变图层对象。有了 CAGradientLayer 对象实现渐变效果变得简单无比。
扩展阅读
iOS CALayer介绍iOS CAReplicatorLayer 复制图层iOS CAShapeLayer示例