楕円形や楕円形の放射状の勾配が必要です。CGContextDrawRadialGradientは完全な円しか描けないようです。私は正方形のコンテキストに描画して、長方形のコンテキストにコピー/描画しています。CGContextDrawRadialGradientを真円形の代わりに楕円形で描画する方法はありますか?
もっと良い方法がありますか?
ありがとうございます!
楕円形や楕円形の放射状の勾配が必要です。CGContextDrawRadialGradientは完全な円しか描けないようです。私は正方形のコンテキストに描画して、長方形のコンテキストにコピー/描画しています。CGContextDrawRadialGradientを真円形の代わりに楕円形で描画する方法はありますか?
もっと良い方法がありますか?
ありがとうございます!
CGContextDrawRadialGradient()を呼び出す直前にCGContextScaleCTM(context、2.0、1.0)を適用して、楕円を描くようにコンテキストの変換を変更することができます。ただし、開始点と終了点に逆変換を適用することを忘れないでください。
これを行うための唯一の方法はMark Fが提案したとおりですが、その答えにはわかりやすい例が必要だと思います。
は、iOSのビューで楕円形のグラデーションを描画(およびARCを使用して):
あなたが得る黒い背景のビューに入れ- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Create gradient
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = {0.0, 1.0};
UIColor *centerColor = [UIColor orangeColor];
UIColor *edgeColor = [UIColor purpleColor];
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)centerColor.CGColor, (__bridge id)edgeColor.CGColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
// Scaling transformation and keeping track of the inverse
CGAffineTransform scaleT = CGAffineTransformMakeScale(2, 1.0);
CGAffineTransform invScaleT = CGAffineTransformInvert(scaleT);
// Extract the Sx and Sy elements from the inverse matrix
// (See the Quartz documentation for the math behind the matrices)
CGPoint invS = CGPointMake(invScaleT.a, invScaleT.d);
// Transform center and radius of gradient with the inverse
CGPoint center = CGPointMake((self.bounds.size.width/2) * invS.x, (self.bounds.size.height/2) * invS.y);
CGFloat radius = (self.bounds.size.width/2) * invS.x;
// Draw the gradient with the scale transform on the context
CGContextScaleCTM(ctx, scaleT.a, scaleT.d);
CGContextDrawRadialGradient(ctx, gradient, center, 0, center, radius, kCGGradientDrawsBeforeStartLocation);
// Reset the context
CGContextScaleCTM(ctx, invS.x, invS.y);
// Continue to draw whatever else ...
// Clean up the memory used by Quartz
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
:
これは私が乗る助けました正しいトラック。ありがとう!私はこれを以下のコード例で展開しました。 –