Iphone 延迟动画–;目标-C

Iphone 延迟动画–;目标-C,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我使用下面的代码来制作一个圆的动画。然而,它一直在闪烁。我想将动画的重新启动延迟5秒。我该怎么做 -(void)start { [self removeExistingAnimation]; //create the image UIImage* img = [UIImage imageNamed:@"redCircle.png"]; imageView = [[UIImageView alloc] initWithImage

我使用下面的代码来制作一个圆的动画。然而,它一直在闪烁。我想将动画的重新启动延迟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"];
}
这样做:

-(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"];

}

这看起来像是肮脏的方法,但对我来说很有效,希望它能帮助你。

这不是有点干净吗?或者,有什么原因不能使用块来设置动画

    - (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,这样你就知道他什么时候应该停止重复


(未经测试,因此可能包含一些错误)

@MuhammadUmar此动画使用哪个框架?QuartzCore。如果我删除opacityAnimation.duration=ANIMATION\u duration;和其他动画持续时间,但当动画停止时仍然有一个bug,imageView仍然添加在self中,我可以看到红色的大圆圈。我想删除这个圆圈,比如一个显示调用函数RemoveExistingAnimation是的,如果删除所有持续时间代码,它工作正常。但是你让它不断闪烁,你想让它在闪烁后结束吗?在我的代码中,我看到它完美地闪烁着。最小和最大比率的值是多少?