UIView的两个动画共存(UIView two animations coexisting)

2019-09-16 11:15发布

怎样才可以有一个动画调用第二动画后永远持续下去? 例如:

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

Answer 1:

你将不能够因为你在这里有他们的基于块的动画来做到这一点。 您将需要使用显式的动画与分裂你的动画了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:



Answer 2:

你可以将动画分成阶段,并使用完毕块,开始下一个阶段。



文章来源: UIView two animations coexisting