【问题标题】:Circular UIView with a gradually fading out border带有逐渐淡出边框的圆形 UIView
【发布时间】: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


    【解决方案1】:

    在 HTML 术语中,+[UIColor clearColor] 实际上是 #000000,alpha 为 0.0。也就是说,名为clearUIColor是透明黑色的。

    在您使用此颜色的大多数情况下都可以,因为它是完全透明的。但是,当您在两种颜色之间创建渐变时,它可能会产生您在此处看到的强烈效果。

    您可能想使用-[UIColor withAlphaComponent:] 方法,例如

    UIColor *transparentRed = [[UIColor redColor] colorWithAlphaComponent:0.0];
    

    【讨论】:

      猜你喜欢
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-12
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多