UIView两个动画并存 [英] UIView two animations coexisting

查看:151
本文介绍了UIView两个动画并存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用第二个动画后,如何让一个动画永远继续?例如:



1)开始一个对象脉动
2)在脉动时移动它
3)继续脉动



除了第二个动画之外的所有工作都无限期地停止第一个动画。
下面是一些示例代码:

  // Pulsate ** 

[UIView animateWithDuration :0.25
延迟:0
选项:(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];


//移动**
[UIView animateWithDuration:0.30
延迟:0
选项:( UIViewAnimationCurveEaseOut | UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState)
动画:^ {
[[(UIPinchGestureRecognizer *)sender view] setCenter:CGPointMake(myAppDelegate.MCViewReference.center.x-300,myAppDelegate.MCViewReference.center.y)];
}
completion:^(BOOL finished){
}];


解决方案

基于动画,因为你有他们在这里。您将需要使用显式动画与 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:。 >

How can I have one animation continue forever after calling a second animation? For example:

1) start an object pulsating 2) Move it while its pulsating 3) it continues pulsating

Everything works except the second animation is stopping the first one indefinitely. Below is some sample code:

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

解决方案

You will not be able to do this with the block-based animations as you have them here. You will need to split your animations up using explicit animations with CABasicAnimation. Create one animation for the pulsating effect and set it to repeat indefinitely. Then you can move it around by setting the center (either animated or non-animated).

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

As soon as you add the animation to the layer, it will begin animating. To remove the animation, simply call [self.layer removeAnimationForKey:@"pulse" or removeAllAnimations:.

这篇关于UIView两个动画并存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆