1

我需要以编程方式绘制(有点)复杂的渐变。这是我的参数。

#define SHADOW_OPACITY 0.35

const size_t num_locations = 4;
const CGFloat locations[num_locations] = { 0, 0.12, 0.42, 1.0 };
const CGFloat components[num_locations * 2] = { 0.0, 1.0 * SHADOW_OPACITY,
                                                0.0, 1.0 * SHADOW_OPACITY,
                                                0.0, 0.73 * SHADOW_OPACITY,
                                                0.0, 0.0 };

三个不透明的环和一个虚线环。一切正常。

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawGradientInContext:context];

    CGContextSetLineWidth(context, CIRCLE_STROKE_WIDTH);

    CGContextBeginPath(context);

        CGFloat dashLength = DASH_LENGTH(self.circleRadius);
        CGFloat dashArray[] = { dashLength, dashLength };
        CGContextSetLineDash(context, 0, dashArray, 2);
        CGContextAddArc(context, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds), self.circleRadius, 0, 2 * M_PI, YES);

    CGContextClosePath(context);

    CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
    CGContextDrawPath(context, kCGPathStroke);
}

- (void)drawGradientInContext:(CGContextRef)context
{
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, locations, num_locations);

    CGPoint gradientCenter = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    CGContextDrawRadialGradient(context,
                                gradient,
                                gradientCenter,
                                self.circleRadius - 1,
                                gradientCenter,
                                self.circleRadius + SHADOW_WIDTH,
                                kCGGradientDrawsAfterEndLocation);

    CGColorSpaceRelease(colorspace);
    CGGradientRelease(gradient);
}

好吧,我需要应用缩放。

- (void)zoom:(UIPinchGestureRecognizer *)sender
{
    effectiveScale = beginGestureScale * sender.scale;
    self.circleRadius = effectiveScale * BASE_RADIUS;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
        beginGestureScale = effectiveScale ? effectiveScale : 1.0;
    }

    return YES;
}

好吧,既然事情搞砸了:它会出故障!

在此处输入图像描述

我可以在那里做什么?

4

0 回答 0