4

UIBezierPath10pxの丸みを帯びた角とグラデーションの塗りつぶしでを作成したいと思います。どうすればこの効果を達成できますか?

これが私がやりたいことのイメージです:

ここに画像の説明を入力してください

ご覧のとおり、この正方形には次のものがあります。

  • 2pxの黒い境界線
  • 10pxの丸い角
  • 赤から緑への線形グラデーション塗りつぶし

パターン画像の色を使用せずにプログラムでこれを行うにはどうすればよいですか?

パスを作成する方法は次のとおりです。

UIBezierPath *border = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10.0f];
[border setLineWidth:2];
[[UIColor blackColor] setStroke];
[border stroke];
[[UIColor redColor] setFill]; <-- what should I put here?
[border fill];
4

1 に答える 1

4

私が考える3つの方法。

  1. CAGradientLayer を作成し、それを theView.layer のサブレイヤーとして挿入します。レイヤーに丸みを帯びたコーナー半径を配置することもできます。もちろん、QuartzCore フレームワークをインポートする必要があります。

  2. 次のように CoreGraphics で実行します。

    CGGradientRef gradient;
    CGColorSpaceRef colorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0,  // Start color
            0.0, 1.0, 0.0, 1.0 }; // End color
    
    colorspace = CGColorSpaceCreateDeviceRGB();
    gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations);
    CGContextDrawLinearGradient (ctx, gradient, gradientStartPoint, gradientEndPoint, 0);
    CGGradientRelease(gradient);
    
  3. 幅が 1 ピクセルでグラデーションのある画面外の画像コンテキストを作成し、画像を生成してから、colorWithPatternImage で背景色を設定します。

これらは、最も簡単なものから最も難しいものの順に並べられています。

于 2012-02-12T16:11:55.920 に答える