5

最近はCAで遊んでいます。今、私はちょっと立ち往生しています。これは私がアニメーション化したいものです: ここに画像の説明を入力

今のところ、円のアニメーションは既に機能しています。アニメーションを作成するために、CALayer をサブクラス化しました。ここからどこへ行けばいいのか本当にわかりません。CATextLayer のサブレイヤーはどこに追加すればよいですか? 両方を同時にアニメーション化して、線のあるテキストが円の端に張り付いているようにするにはどうすればよいですか?

コードやその他のものが必要な場合はお知らせください。

ここで助けてもらえると本当にうれしいです:-)

どうもありがとう!

4

2 に答える 2

5

以下、ご要望の内容を実現することができました。コードは非常に大雑把で、数分かかっただけで、リリースを見逃している可能性があります。UILabel を CATextLayer に交換するだけです (簡潔にするために UILabel を使用します)。

    CAShapeLayer* circle = [[CAShapeLayer alloc] init];
    CGMutablePathRef path = CGPathCreateMutable();
    CGRect textRect = CGRectMake(self.bounds.size.width/4, (self.bounds.size.height-self.bounds.size.width/2)/2, self.bounds.size.width/2, self.bounds.size.width/2);
    float midX = CGRectGetMidX(textRect);
    float midY = CGRectGetMidY(textRect);
    CGAffineTransform t = CGAffineTransformConcat(
                            CGAffineTransformConcat(
                                CGAffineTransformMakeTranslation(-midX, -midY), 
                                CGAffineTransformMakeRotation(-1.57079633/0.99)), 
                            CGAffineTransformMakeTranslation(midX, midY));
    CGPathAddEllipseInRect(path, &t, textRect);
    circle.path = path;
    circle.frame = self.bounds;
    circle.fillColor = [UIColor clearColor].CGColor;
    circle.strokeColor = [UIColor blackColor].CGColor;
    circle.lineWidth = 60.0f;
    [self.layer addSublayer:circle];

    CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.duration = 15.0f;
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [circle addAnimation:animation forKey:@"strokeEnd"];

    [circle release];

    UILabel* label = [[UILabel alloc] init];
    label.text = @"Test Text";
    label.font = [UIFont systemFontOfSize:20.0f];
    label.center = CGPathGetCurrentPoint(path);
    label.transform = CGAffineTransformMakeRotation(1.57079633);
    [label sizeToFit];
    [self.layer addSublayer:label.layer];

    CAKeyframeAnimation* textAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    textAnimation.duration = 15.0f;
    textAnimation.path = path;
    textAnimation.rotationMode = kCAAnimationRotateAuto;
    textAnimation.calculationMode = kCAAnimationCubicPaced;
    textAnimation.removedOnCompletion = NO;
    [label.layer addAnimation:textAnimation forKey:@"position"];
于 2011-11-10T02:49:11.073 に答える
0

新しい UIView クラスを作成してから、layoutSubviews メソッドを使用することをお勧めします。
UIView クラスの詳細については、クラス リファレンスを参照してください。 メインビューでこれを使用するだけです:

myView = [[myView alloc] initWithFrame:CGRectMake(100,100,100,100)];
[self.view addSubview:myView];

また、layoutSubviews の場合は次のようになります。

- (void) layoutSubviews{
     [super layoutSubviews];

      mainLayer.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);

      CALayer *circle = [[[CALayer alloc] init] autorelease];
      //You have to set up the circle image and everything
      circle.position = mainLayer.position;
}

もちろん、myView の実装を .h に追加する必要がありますが、それ以外はすべて機能するはずです。

編集: 申し訳ありませんが、テキスト レイヤー コードを layoutSubview に追加するのを忘れていましたが、レイヤーを作成してその位置を mainLayer.position に設定するだけです。

于 2011-11-05T23:22:28.727 に答える