2012-04-10 62 views
3

如何在調用第二個動畫之後讓一個動畫永遠持續下去?例如:UIView兩個動畫並存

1)啓動的對象脈動 2)移動它,而它的脈動 3)它繼續脈動

一切正常,除了第二動畫無限期停止第一之一。 下面是一些示例代碼:

//Pulsate ** 

     [UIView animateWithDuration:0.25 
           delay:0 
          options: (UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionRepeat) 
         animations:^{ 
          CGAffineTransform currentTransform = self.transform; 
          CGAffineTransform newTransform1 = CGAffineTransformScale(currentTransform, .95, .95); 
          [self setTransform:newTransform1]; 
          CGAffineTransform newTransform2 = CGAffineTransformScale(currentTransform, 1, 1); 
          [self setTransform:newTransform2]; 
         } 
         completion:nil];  


//Move ** 
    [UIView animateWithDuration:0.30 
         delay:0 
        options: (UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) 
       animations:^{ 
        [[(UIPinchGestureRecognizer*)sender view] setCenter:CGPointMake(myAppDelegate.MCViewReference.center.x-300, myAppDelegate.MCViewReference.center.y)]; 
       } 
       completion:^(BOOL finished){ 
       }];  

回答

5

您將無法使用基於塊的動畫進行此操作,因爲您在此處具有這些動畫。您需要使用顯式動畫將動畫分割爲CABasicAnimation。爲脈動效果創建一個動畫並將其設置爲無限重複。然後,您可以通過設置中心(動畫或非動畫)來移動它。

CABasicAnimation *pulsation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
pulsation.fromValue = [NSNumber numberWithFloat:0.95f]; 
pulsation.toValue = [NSNumber numberWithFloat:1.f]; 
pulsation.duration = 0.25f; 
pulsation.autoreverses = YES; 
pulsation.repeatCount = INFINITY; 

[self.layer addAnimation:pulsation forKey:@"pulse"]; 

只要將動畫添加到圖層,它就會開始動畫。要刪除動畫,只需撥打[self.layer removeAnimationForKey:@"pulse"removeAllAnimations:即可。

1

你可以將動畫分成階段,並使用完畢塊,開始下一個階段。