在CALayer中应用清晰的颜色

时间:2015-06-18 12:52:12

标签: ios iphone ipad uiview calayer

如何在绿色覆盖的区域内以圆形UIView涂上清晰的颜色,或者中心的白色将从中心褪色。

enter image description here

以下是代码: -

   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);
}

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

2 个答案:

答案 0 :(得分: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];

答案 1 :(得分:0)

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