3

画面上で人が上下にスライドすると変化するグラデーションの背景を持つアプリを作成したいと考えています。グラデーションを放射状にしたかったので、代わりに Core Graphics を使用しましたCAGradientLayer

UIView私のView Controllerには、そのdrawRectの次の実装を持つサブクラスのプロパティがあります。

- (void)drawRect:(CGRect) rect
{
    self.alpha = 1.0;

    // Draw radial gradient here using C language
    CGPoint startCenter, endCenter;

    startCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetHeight(rect));

    endCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetHeight(rect));


    //radial gradient radius;
    CGFloat startRadius = 0.17 * CGRectGetHeight(rect);
    CGFloat endRadius = 1.04 * CGRectGetHeight(rect);

    //gradient locations.    
    CGFloat locations[2] = {0.0, 0.8};

    //gradient color components.
    CGFloat components[8] = {
                              232/255.0, 249/255.0, 229/255.0, 1.0,
                              133/255.0, 174/255.0, 127/255.0, 1.0,
                             };

    //Drawing code.
    CGContextRef context = UIGraphicsGetCurrentContext();


    //Get RGB color space
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

    //create gradient.
    CGGradientRef gradient = CGGradientCreateWithColorComponents(space, components, locations, 2);
    CGColorSpaceRelease(space);
    //draw gradient.
    CGContextDrawRadialGradient(context, gradient, startCenter, startRadius,endCenter,endRadius, kCGGradientDrawsBeforeStartLocation);



    _contextToSave = UIGraphicsGetCurrentContext();  // useless

    CGGradientRelease(gradient);

グラデーションを更新できるようにしたいので、コンポーネントCGFloat配列でプロパティまたはインスタンス変数を使用してから変更をアニメーション化することを考えていましたが、その方法がわかりません。

viewDidAppearジェスチャ認識エンジンを待ち、ジェスチャに応じてグラデーションViewControllerを調整する必要があることはわかっていますが、問題はいつ更新するかではなく、どのように更新するかということでした。何も起こらなかった。

グラデーションとアニメーションの変更を作成しようとするだけで、次のコードviewDidAppearViewController.

[UIView animateWithDuration:6.0 animations:^{

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGFloat colorsComponents[8] = {
        145/255.0, 149/255.0, 229/255.0, 1.0,
        133/255.0, 74/255.0, 27/255.0, 1.0,
    };

    CGFloat locations[2] = {0.0, 0.8};

    //CGContextRef context = UIGraphicsGetCurrentContext();

    CGPoint startCenter, endCenter;

    startCenter = CGPointMake(0.5 * gradientView.frame.size.width, gradientView.frame.size.height);

    startCenter = endCenter;

    CGFloat startRadius = 0.17 * gradientView.frame.size.height;
    CGFloat endRadius = 1.04 * gradientView.frame.size.height;


    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colorsComponents, locations, 2);
    CGContextDrawRadialGradient(_subclassofView.contextToSave, gradient, startCenter, startRadius, endCenter, endRadius, kCGGradientDrawsBeforeStartLocation);

}];

助けてくれてありがとう。

4

2 に答える 2

0

いずれにせよ、これは初心者の iOS コーダーに役立ちます。私の質問に対する解決策を共有します。これが最もエレガントな解決策であるか、最高のパフォーマンスを発揮する解決策であるかはわかりませんが、今のところうまくいくようです... 基本的に、UIView のサブクラスの drawrect 内で、すべてのコンポーネントの値を変更しました ( RGB とアルファを含む色) をプロパティに存在する値に変換します。このプロパティは NSMutableArray であり、View Controller で値を変更してから [self.subview setNeedsDisplay] を呼び出すために使用します。

于 2013-09-22T18:33:36.287 に答える