【问题标题】:Delay an animation – Objective-C延迟动画 – Objective-C
【发布时间】:2013-02-18 15:21:19
【问题描述】:

我正在使用以下代码为圆设置动画。但是,它一直在闪烁。我想将动画的重启延迟 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"];
}

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    这样做:

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

    这看起来像肮脏的方法,但对我有用,希望对你有帮助..

    【讨论】:

    • @MuhammadUmar 这个动画你用的是哪个框架?
    • 石英核心。如果我删除 opacityAnimation.duration = ANIMATION_DURATION; 它会起作用和其他动画持续时间,但动画停止时仍然存在一个错误,imageView 仍然添加到 self 中,我可以看到大红圈。我想删除这个圆圈,比如 atEndAnimation 调用函数 removeExistingAnimation
    • 是的,如果您删除所有持续时间代码,您的工作正常。
    • 但是你让它一直在闪烁,你想让它在闪烁后结束吗?在我的代码中,我看到它完美地闪烁。
    • 最小和最大比率的值是多少?
    【解决方案2】:

    这不是更干净一点吗?或者你有什么理由不能使用块来制作动画?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多