三角形をグラデーションで塗りつぶす方法を尋ねていますか? 次のコードはまさにそれを行います。それを機能させるには、QuartzCore フレームワークを追加する必要があります。
#import <QuartzCore/QuartzCore.h>
このコードは、三角形のシェイプ レイヤーを作成し、これをグラデーション レイヤーのマスクとして使用します。お役に立てれば。
- (void)drawRect:(CGRect)rect
{
CGFloat w = self.bounds.size.width / 4;
CGFloat h = self.bounds.size.height / 4;
CGFloat x = w * 2;
CGFloat y = h;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,NULL, x, y);
CGPathAddLineToPoint(path, NULL, x+w, y+2*h);
CGPathAddLineToPoint(path, NULL, x-w, y+2*h);
CGPathCloseSubpath(path);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
[shapeLayer setPath:path];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
gradientLayer.startPoint = CGPointMake(0.25, 1.0);
gradientLayer.endPoint = CGPointMake(0.75, 0.0);
UIColor *startColour = [UIColor redColor];
UIColor *endColour = [UIColor greenColor];
gradientLayer.colors = [NSArray arrayWithObjects:(id)[startColour CGColor], (id)[endColour CGColor], nil];
[gradientLayer setMask:shapeLayer];
[self.layer addSublayer:gradientLayer];
CGPathRelease(path);
}