【发布时间】:2017-01-26 15:27:53
【问题描述】:
我设法用径向渐变颜色制作了一个圆形,如下所示:
@interface CircleView : UIView
@end
@implementation CircleView
- (void)drawRect:(CGRect)rect {
const CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSArray *gradientColors = [NSArray arrayWithObjects:
(id)[UIColor redColor].CGColor,
(id)[UIColor yellowColor].CGColor, nil];
CGFloat gradientLocations[] = {0.0, 0.5};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)gradientColors, gradientLocations);
UIBezierPath *roundedRectanglePath = [UIBezierPath bezierPathWithOvalInRect:rect];
CGContextSaveGState(context);
[roundedRectanglePath fill];
[roundedRectanglePath addClip];
CGPoint gradCenter= CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGContextDrawRadialGradient (context, gradient, gradCenter, 0, gradCenter, rect.size.width, kCGGradientDrawsBeforeStartLocation);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}
@end
确保在将 UIView 添加到情节提要并将其类设置为 CircleView 时,它的宽度 = 高度。
但是我想将[UIColor yellowColor] 变成[UIColor clearColor],这样看起来红色就会淡出,即外面是透明的。
我发现把[UIColor yellowColor]改成[UIColor clearColor],外面变成黑色的样子:
如何使红色以循环方式逐渐淡出-因为将[UIColor yellowColor]设置为[UIColor clearColor]显然不起作用?
【问题讨论】:
标签: ios objective-c core-graphics