2013-02-18 173 views
0

我正在使用下面的代碼來爲一個圓圈設置動畫。它不斷閃爍,我想延遲動畫重啓,讓我們說5秒。我怎樣才能做到這一點?延遲動畫iphon

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

回答

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

} 

這看起來像髒的方法,但爲我工作,跳它幫助你..

+0

不起作用。親自嘗試一下 – 2013-02-18 15:57:08

+0

@MuhammadUmar你用這個動畫製作了哪個框架? – Dilip 2013-02-18 16:00:18

+0

QuartzCore。它工作,如果我刪除opacityAnimation.duration = ANIMATION_DURATION;和其他動畫持續時間,但動畫停止時仍然存在一個錯誤,imageView仍然添加到自我中,我可以看到大紅圈。我想刪除這個圈子,像atEndAnimation調用函數removeExistingAnimation – 2013-02-18 16:03:41

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,這樣你就知道他應該什麼時候停止重複。

(未測試,所以可能包含一些錯誤)