UIImageView动画从左到右
我想将我的 UIImageView
从左移动到右,反之亦然。我通过使用以下代码完成了一半:
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:10];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos = mover.center;
pos.x = 100.0f;
mover.center = pos;
[UIView commitAnimations];
其中 mover
是 UIImageView
。我面临的问题是我无法将其完全从左向右移动。上面的代码只是将其从右移到中心。我想从中心向左走。有人可以指导我吗?
I want to move my UIImageView
from left to right and vice versa. I accomplish half of it by using the following code:
[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:10];
[UIView setAnimationRepeatAutoreverses:YES];
CGPoint pos = mover.center;
pos.x = 100.0f;
mover.center = pos;
[UIView commitAnimations];
Where mover
is a UIImageView
. The problem I am facing is that I am unable to move it from left to right completely. The above code is only moving it from right to center. I want to go further left from center. Can anybody guide me please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为 UIKit 动画为您提供直接关键帧动画来获得振荡效果。我们可以尝试使用委托通过触发一个又一个动画来实现它,但它不如
CAKeyframeAnimation
那么高效。要使用它,您必须在项目中包含QuartzCore
框架和#import
。您可以通过执行以下操作来实现振荡效果,这段代码从左到右振荡视图,非常接近您的描述,尽管为了获得您想要的确切效果,您可能需要稍微更改它。
I don't think
UIKit
animations provide you direct key frame animations to get the oscillation effect. We can try to achieve it using delegates by triggering one animation after another but it is not as efficient asCAKeyframeAnimation
. To use this, you will have to include theQuartzCore
framework in your project and#import <QuartzCore/QuartzCore.h>
. You can achieve your oscillation effect by doing something like this,This snippet of code oscillates a view left to right pretty close to your description although to get the exact effect you want you might have to change it a little.
假设您可以针对 iOS4,这可以通过以下方式实现
Assuming you can target iOS4, this can be achieved by
您应该考虑设置变换,而不是设置中心。使用中心,您可以设置中心,这意味着您必须根据图像大小正确计算中心。
要将其向左移动,请将变换设置为 -320 的平移。要将其移回,请将其设置为恒等变换。
Instead of setting center you should look into setting the transform. With center you set the center which means you have to calculate the center correctly in relation to the image size.
To move it left set the transform to a translation of -320. To move it back set it to the identitiy transform.