5

iPhone Core Animation-Drawing a Circleに示すように、完全な円になるまで時計回りに描画されるアニメーション化された円を作成しようとしています。

問題は、CALayerオブジェクトが追加またはビルドされていないことです。drawInContext:CGContextRefテストして、自分のanimatingArcメソッドにアクセスしていないことを確認しました。

これまで私が行ったことは次のとおりです。

AnimateArc.hで

@interface AnimateArc : CALayer {

CAShapeLayer *circle;
}

-(void) animatingArc;

@end

AnimateArc.mで

-(void) drawInContext:(CGContextRef)ctx
{
CGFloat radius = 50.0;
circle = [CAShapeLayer layer];

//make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0, 0.0, 2 * radius, 2 * radius) cornerRadius:radius].CGPath;

    CGPoint centerPoint = CGPointMake(CGRectGetWidth(self.bounds)/2, CGRectGetHeight(self.bounds)/2);    

//center the shape in self.view
circle.position = centerPoint;

//configure appearence of circle
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor blackColor].CGColor;
circle.lineWidth = 5;                                           

/*CGPointMake((self.contentsCenter.size.width), (self.contentsCenter.size.height));*/

//path the circle
CGContextAddArc(ctx, centerPoint.x, centerPoint.y, radius, 0.0, 2 * M_PI, 0);
CGContextClosePath(ctx);

//fill it
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextFillPath(ctx); }

////////////////////////////////////////////////// /////////////////////

-(void) animatingArc
{
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"arcEnd"];
anim.duration = 20.0; //animate over 20 seconds
anim.repeatCount = 1.0; //animate only once
anim.removedOnCompletion = NO; //Reamin there after completion

//animate from start to end
anim.fromValue = [NSNumber numberWithFloat:50.0f];
anim.toValue = [NSNumber numberWithFloat:150.0f];

//experiment with timing to get appearence to look the way you want
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

//add animation to circle
[circle addAnimation:anim forKey:@"animatingArc"]; 
}

/////////////////////

//needed since key not part of animatable properties
+(BOOL) needsDisplayForKey:(NSString *)key
{
if([key isEqualToString:@"arcEnd"])
    return YES;
else
    return [super needsDisplayForKey:key];

}

//ensure custom properties copied to presentation layer
-(id) initWithLayer:(id)layer
{
if((self = [super initWithLayer:layer]))
{
    if ([layer isKindOfClass:[AnimateArc class]])
    {
        AnimateArc *other = (AnimateArc *) layer;
        [other setNeedsDisplay];
    }
}
return self; }

そして最後に私のviewControllerで、

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view.layer addSublayer:AnimateArcObject];
[AnimateArcObject animatingArc];
 }

フォーマットが悪いことをお詫びします...誰かが私が間違っていることを教えてもらえますか?また、Core Animationの初心者であり、自分が正しい方向に進んでいるかどうかわからないため、これら2つの関数にアクセスした後、コードがどこでもクラッシュする可能性があることにも疑問を持っています。

どんな助けでもありがたいです。ありがとう。

4

2 に答える 2

20

CoreAnimationでの私の辛い経験から、インスタンス化するもののプロパティを常に設定する必要があります。bounds CALayer

したがって、次のようなものが欠落しているため、レイヤーが表示されていません。

layer.bounds = CGRectMake(0, 0, width, height);

レイヤーをインスタンス化したらすぐにこれを配置し、そうすることを習慣にして、再びそれに陥らないようにする必要があります。

コードがクラッシュするのはごめんなさい。配布が多すぎて、どのようにリンクされているのかわからないので、お手伝いできません。

于 2012-07-21T22:29:39.230 に答える
0

少し調べてみたら、間違った方向に進んでいると思いました。そこで、このファイルを削除して、から継承しAnimateArcている新しいファイルを追加しましたUIViewController

次に、viewDidLoadメソッドで、 このリンクからパスを使用して円とアニメーションを作成するコードを記述しました。

親ビューコントローラに、を追加しましAnimatedArc ViewController's subviewた。今では完璧に機能しています:)

于 2012-08-12T04:21:13.600 に答える