0

次のコードを使用して、円をアニメーション化します。ただし、常に点滅しています。アニメーションの再開を5秒遅らせたい。どうやってやるの?

-(void)start
{    
    [self removeExistingAnimation];
    
    //create the image
    UIImage* img = [UIImage imageNamed:@"redCircle.png"];
    imageView = [[UIImageView alloc] initWithImage:img];
    imageView.frame = CGRectMake(0, 0, 0, 0);
    [self addSubview:imageView];
    
    //opacity animation setup
    CABasicAnimation *opacityAnimation;
    
    opacityAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.duration = ANIMATION_DURATION;
    opacityAnimation.repeatCount = ANIMATION_REPEAT;
    //theAnimation.autoreverses=YES;
    opacityAnimation.fromValue = [NSNumber numberWithFloat:0.6];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.025];
    //resize animation setup
    CABasicAnimation *transformAnimation;
    
    transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    transformAnimation.duration = ANIMATION_DURATION;
    transformAnimation.repeatCount = ANIMATION_REPEAT;
    //transformAnimation.autoreverses=YES;
    transformAnimation.fromValue = [NSNumber numberWithFloat:MIN_RATIO];
    transformAnimation.toValue = [NSNumber numberWithFloat:MAX_RATIO];
    
    //group the two animation
    CAAnimationGroup *group = [CAAnimationGroup animation];
    
    group.repeatCount = ANIMATION_REPEAT;
    [group setAnimations:[NSArray arrayWithObjects:opacityAnimation, transformAnimation, nil]];
    group.duration = ANIMATION_DURATION;

    //apply the grouped animaton
    [imageView.layer addAnimation:group forKey:@"groupAnimation"];
}
4

2 に答える 2

2

このようにしてください:

-(void)start
{    
    [self removeExistingAnimation];

    //create the image
    UIImage* img = [UIImage imageNamed:@"redCircle.png"];
    imageView = [[UIImageView alloc] initWithImage:img];
    imageView.frame = CGRectMake(0, 0, 0, 0);
    [self addSubview:imageView];

    [self doAnimation];

    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(doAnimation) userInfo:nil repeats:YES];
}

-(void)doAnimation
{

//opacity animation setup
    CABasicAnimation *opacityAnimation;

    opacityAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.duration = ANIMATION_DURATION;
    opacityAnimation.repeatCount = ANIMATION_REPEAT;
    //theAnimation.autoreverses=YES;
    opacityAnimation.fromValue = [NSNumber numberWithFloat:0.6];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.025];
    //resize animation setup
    CABasicAnimation *transformAnimation;

    transformAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];

    transformAnimation.duration = ANIMATION_DURATION;
    transformAnimation.repeatCount = ANIMATION_REPEAT;
    //transformAnimation.autoreverses=YES;
    transformAnimation.fromValue = [NSNumber numberWithFloat:MIN_RATIO];
    transformAnimation.toValue = [NSNumber numberWithFloat:MAX_RATIO];

    //group the two animation
    CAAnimationGroup *group = [CAAnimationGroup animation];

    group.repeatCount = ANIMATION_REPEAT;
    [group setAnimations:[NSArray arrayWithObjects:opacityAnimation, transformAnimation, nil]];
    group.duration = ANIMATION_DURATION;

    //apply the grouped animaton
    [imageView.layer addAnimation:group forKey:@"groupAnimation"];

}

これは汚い方法のように見えますが、私のために働きました、それがあなたを助けることを願っています。

于 2013-02-18T15:27:28.660 に答える
0

これは少しきれいではありませんか?または、アニメーションにブロックを使用できない理由はありますか?

    - (void) start {
        //create the image
        UIImage* img = [UIImage imageNamed:@"redCircle.png"];
        imageView = [[UIImageView alloc] initWithImage:img];
        [self addSubview:imageView];

        [self animateWithDelay:0.0];
    }

    - (void) animateWithDelay:(CGFloat)delay {

        imageView.alpha = 0.0;
        imageView.transfrom = CGAffineTransformScale(myImage.transform, MIN_RATIO, MIN_RATIO);
        [UIImageView animateWithDuration:ANIMATION_DURATION delay:delay options:0 animations:^{

            imageView.alpha = 1.0;
            imageView.transfrom = CGAffineTransformScale(myImage.transform, MAX_RATIO, MAX_RATIO);

        } completion:^{
            animationCount++;
            if (ANIMATION_REPEAT != animationCount) {
                [self animateWithDelay:5.0];
            }
        }]; 

    }

クラスのどこかにanimationCountを定義して、彼がいつ繰り返すのをやめるべきかがわかるようにする必要があります。

(テストされていないため、エラーが含まれている可能性があります)

于 2013-02-18T16:02:04.363 に答える