3

最新の SDK を使用して iOS 5.0 以降のアプリを開発しています。

私は似たようなことをしたい

図 8-6

放射状グラデーションをネストする方法を知りたいです。

これは私のコードです:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    _innerColor = [UIColor yellowColor];
    _outerColor = [UIColor redColor];

    CGContextSetAllowsAntialiasing(context, true);
    CGContextSetShouldAntialias(context, true);
    
    CGFloat graWidth = layer.frame.size.width / 2;
    CGFloat graHeight = layer.frame.size.height / 2;

    CGFloat firstGlossLocation = 0.0f;
    CGFloat outterPercent = 0.0f;
    
    if (_isUnSelected)
    {
        firstGlossLocation = 0.7f;
        outterPercent = graWidth * 0.0f;
    }
    else
    {
        firstGlossLocation = 0.0f;
        outterPercent = graWidth * 0.25f;
    }
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGFloat iRed = 0.0, iGreen = 0.0, iBlue = 0.0, iAlpha =0.0;
    [_innerColor getRed:&iRed green:&iGreen blue:&iBlue alpha:&iAlpha];
    
    CGFloat oRed = 0.0, oGreen = 0.0, oBlue = 0.0, oAlpha =0.0;
    [_outerColor getRed:&oRed green:&oGreen blue:&oBlue alpha:&oAlpha];
    
    CGFloat gradientColors[] = {
        oRed, oGreen, oBlue, oAlpha,
        iRed, iGreen, iBlue, iAlpha
    };
    
    // We have to change first value when user taps over the Guage.
    // The second one must one to fill the entire Gauge.
    CGFloat glossLocations[] = {firstGlossLocation, 1};
    CGGradientRef ballGradient = CGGradientCreateWithColorComponents(colorSpace, gradientColors, glossLocations, 2);
    
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, graWidth*2, graHeight*2));
    CGContextClip(context);
    
    CGPoint startPoint = CGPointMake(graWidth, graHeight);
    CGPoint endPoint = CGPointMake(graWidth, graHeight);
    CGContextDrawRadialGradient(context, ballGradient, startPoint, 0, endPoint, graWidth - outterPercent, kCGGradientDrawsAfterEndLocation);
    CGContextDrawPath(context, kCGPathStroke);
}

_isUnSelectedが のときに2 番目のグラデーションを描画したいNO

2 つ目のネストされたグラデーションを描画する方法を知っていますか?

4

1 に答える 1

4

図に示されているグラデーションを描画するために、「2 番目のネストされたグラデーション」は必要ありません。それが1つのグラデーションです。グラデーションは複数の場所に複数の色を持つことができ、それがその図に示されているものです。4 つのカラー/ポイントがあるように見えます。

したがって、描画を変更する必要がある場合は、最初のグラデーション (単純な 2 ポイント グラデーション) を 2 番目のグラデーション (4 つの色/ポイント) に置き換えます。その逆も同様です。

于 2013-04-17T17:30:31.950 に答える