UIView两个动画共存

时间:2012-04-10 19:14:11

标签: iphone objective-c ios xcode cocoa-touch

如何在调用第二个动画后永久地继续播放一个动画?例如:

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){
                }];    

2 个答案:

答案 0 :(得分: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 :(得分:1)

您可以将动画分成几个阶段,然后使用完成块开始下一阶段。