2013-02-18 4 views
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"]; 
} 

답변

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

작동하지 않습니다. 직접 사용해보세요 –

+0

@MuhammadUmar이 프레임 워크에서 사용중인 프레임 워크는 무엇입니까? – Dilip

+0

QuartzCore. opacityAnimation.duration = ANIMATION_DURATION을 제거하면 작동합니다. 그리고 다른 애니메이션 지속 시간이지만 애니메이션이 멈 추면 여전히 하나의 버그가 있습니다. imageView는 여전히 자체에 추가되고 큰 Red Circle을 볼 수 있습니다. 이 원을 제거하고 싶습니다. atEndAnimation 호출 함수 removeExistingAnimation –

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를 클래스의 어딘가에 정의해야 반복 작업을 중단해야하는 시점을 알 수 있습니다.

(테스트되지 않았으므로 약간의 오류가있을 수 있습니다.)