【问题标题】:apply clear color in CALayer在 CALayer 中应用清晰的颜色
【发布时间】:2015-09-04 02:57:37
【问题描述】:

如何在这个圆形UIView中在绿色覆盖的区域内应用清晰的颜色,否则中心的白色会从中心淡出。

代码如下:-

   UIView *vwx=[[UIView alloc]initWithFrame:CGRectMake(150, self.view.frame.size.height/2, 100, 100)];

    vwx.layer.cornerRadius = 50;
    vwx.layer.masksToBounds = YES;
    CircleGradient *gradientLayer = [CircleGradient new];
    gradientLayer.frame = vwx.bounds;

    [vwx.layer addSublayer:gradientLayer];
    [self.view addSubview:vwx];

这是我在子类化CALayer中所做的

- (void)drawInContext:(CGContextRef)ctx
{

    size_t gradLocationsNum = 2;
    CGFloat gradLocations[2] = {0.0f, 1.0f};
    CGFloat gradColors[3] = {0.0f,0.0f,0.0f};
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, gradColors, gradLocations, gradLocationsNum);
    CGColorSpaceRelease(colorSpace);

    CGPoint gradCenter= CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    float gradRadius = MIN(self.bounds.size.width , self.bounds.size.height) ;

    CGContextDrawRadialGradient (ctx, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);


    CGGradientRelease(gradient);
}

任何帮助或建议都会有所帮助

【问题讨论】:

    标签: ios iphone ipad uiview calayer


    【解决方案1】:

    试试这个代码。确实解决了这个

    int radius = myRect.size.width;
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,  0, self.mapView.bounds.size.width, self.mapView.bounds.size.height) cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPath  bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) cornerRadius:radius];
    [path appendPath:circlePath];
    [path setUsesEvenOddFillRule:YES];
    
    CAShapeLayer *fillLayer = [CAShapeLayer layer];
    fillLayer.path = path.CGPath;
    fillLayer.fillRule = kCAFillRuleEvenOdd;
    fillLayer.fillColor = [UIColor grayColor].CGColor;
    fillLayer.opacity = 0.5;
    [view.layer addSublayer:fillLayer];
    

    【讨论】:

    • 我只有一个视图,即 vwx ...如何添加...您可以更新答案吗?
    • 哦,咳咳。尝试设置 CAShapeLayer 的背景颜色真是让人头疼。实际上是fillColor。谢谢!
    【解决方案2】:

    通过将circular UIView 的背景颜色设置为[[UIColor clearColor] CGColor];,您将获得透明(清晰)的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 2016-08-21
      • 2022-01-17
      • 2016-07-15
      • 2018-01-20
      • 2017-02-11
      相关资源
      最近更新 更多