动画在更改UIImage时回切 [英] Animation Snaps back when the UIImage is changed

查看:102
本文介绍了动画在更改UIImage时回切的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下并按住一个按钮时,我有一个UIImageView在屏幕上运行。当按钮被按下时,改变UIImageView的UIImage,当按钮被放开时,我把它改变为它的原始UIImage。当图像变回时,它会回到图像开始的位置。



按下此按钮时会调用此计时器:

  /这是当按下按钮时改变的图像。 
imView.image = image2;
runTimer = [NSTimer scheduledTimerWithTimeInterval:0.04
target:self
selector:@selector(perform)
userInfo:nil
重复:YES];

当按钮停止时会调用:

   - (IBAction)stopPerform:(id)sender {
[runTimer invalidate];

//这是什么动画动画回来:
//没有这个动画不回弹
imView.image = image1;
}

- (void)performRight {

CGPoint point0 = imView.layer.position;
CGPoint point1 = {point0.x + 4,point0.y};

CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@position.x];
anim.fromValue = @(point0.x);
anim.toValue = @(point1.x);
anim.duration = 0.2f;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

//首先我们更新模型层的属性。
imView.layer.position = point1;

//现在我们附加动画。
[imView.layer addAnimation:anim forKey:@position.x];
}

我需要将图像中的更改添加到动画中吗?如果是这样怎么样?核心动画使用不同的属性集来表示一个对象:


$

$

b $ b

核心动画编程指南






(或简单的图层树)是您的应用程序与之互动最多的。此树中的对象是存储任何动画的目标值的模型对象。每当您更改图层的属性时,都会使用其中一个对象。



演示文稿树包含任何正在运行的动画。而图层树对象包含动画的目标值,而表示树中的对象反映屏幕上显示的当前值。您不应该修改此树中的对象。






因此,您可以使用这些对象来读取当前动画值,也可以从这些值开始创建新动画。当你改变表现层的属性动画时,但是一旦动画完成,对象就回到它的模型属性值。



你需要做什么来修复是使用 [CAAnimation animationDidStop:finished:] 委托方法设置最终的属性值和你想做的任何事情。我想你可以使用这个转储那个可怕的 NSTimer 代码,你正在使用,世界的一小部分将是更好。


I have an UIImageView that runs across the screen when a button is pressed and held. When the button is pressed is changes the UIImage of the UIImageView and when the button is let go I change it to its original UIImage. When ever the image changes back it snaps back to the location that the image started.

This Timer is called when the button is pressed:

//This is the image that changes when the button is pressed.
imView.image = image2;
runTimer = [NSTimer scheduledTimerWithTimeInterval:0.04
                                            target:self
                                          selector:@selector(perform)
                                          userInfo:nil
                                           repeats:YES];

This is called When the button stops being held:

- (IBAction)stopPerform:(id)sender{
   [runTimer invalidate];

   //THIS IS WHAT SNAPS THE ANIMATION BACK:
   //Without this the animation does not snap back
   imView.image = image1;
}

- (void)performRight{

 CGPoint point0 = imView.layer.position;
 CGPoint point1 = { point0.x + 4, point0.y };

 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
 anim.fromValue    = @(point0.x);
 anim.toValue  = @(point1.x);
 anim.duration   = 0.2f;
 anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

 // First we update the model layer's property.
 imView.layer.position = point1;

 // Now we attach the animation.
 [imView.layer  addAnimation:anim forKey:@"position.x"];
}

Do I need to add the change in images to the animation? If so how? Im really confused.

解决方案

Core Animation uses different sets of properties to represent an object:

From Core Animation Programming Guide:


model layer tree (or simply "layer tree") are the ones your app interacts with the most. The objects in this tree are the model objects that store the target values for any animations. Whenever you change the property of a layer, you use one of these objects.

presentation tree contain the in-flight values for any running animations. Whereas the layer tree objects contain the target values for an animation, the objects in the presentation tree reflect the current values as they appear onscreen. You should never modify the objects in this tree. Instead, you use these objects to read current animation values, perhaps to create a new animation starting at those values.


So when you animate the properties you change the presentation layer, but once the animation is finished the object reverts back to its model property values.

What you need to do to fix this is use the [CAAnimation animationDidStop:finished:] delegate method to set the final property value and anything else you would like to do. I think you could use this to dump that horrible NSTimer code you are using and one small part of the world will be that much better.

这篇关于动画在更改UIImage时回切的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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