简体   繁体   English

移动一个 uiimageview 动画

[英]Move an uiimageview animated

How could I make a UIImage move from the bottom of the screen, to the top of the screen in 20 seconds (about 35 pixels per seconds) I don't mean you should drag it by yourself, but it should automatically move to the top.我怎么能让一个UIImage在20秒内从屏幕底部移动到屏幕顶部(大约每秒35像素)我不是说你应该自己拖动它,但它应该自动移动到顶部. Thank you谢谢

Firstly, apple doc is your friend.首先, apple doc是你的朋友。 All of the information I'm giving you here is derived from this.我在这里给你的所有信息都来自于此。 Apple also provides a LOT of sample code, and you should definitely take a look at it. Apple 还提供了大量示例代码,您一定要看一看。

The way you can (easily) accomplish this is using UIView animations.您可以(轻松)完成此操作的方法是使用 UIView 动画。 Assuming that you have a UIImageView for your image, you can use the animateWithDuration:(NSTimeInterval)duration animations:... method.假设您的图像有一个 UIImageView,您可以使用animateWithDuration:(NSTimeInterval)duration animations:...方法。

For example:例如:

[UIView animateWithDuration:10.0f animations:^{
    //Move the image view to 100, 100 over 10 seconds.
    imageView.frame = CGRectMake(100.0f, 100.0f, imageView.frame.size.width, imageView.frame.size.height);
}];

You could get fancier by adding more and more options to the animation, getting a completion block, etc. This is all achieved with variations of the 'animateWithDuration' method.您可以通过向 animation 添加越来越多的选项、获取完成块等来变得更有趣。这都是通过“animateWithDuration”方法的变体实现的。 There are tons of tutorials on UIView animations out there, and tons of documentation.那里有大量关于 UIView 动画的教程和大量文档。

If you don't want to use blocks (the ^{...code...} bit above) you can run your animation like this:如果您不想使用块(上面的 ^{...code...} 位),您可以像这样运行 animation:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:10.0f];
imageView.frame = ...
[UIView commitAnimations];

If I understand it you can do an animation of your own object, in this case try the beginAnimation如果我理解你可以做一个 animation 你自己的 object,在这种情况下试试 beginAnimation

...
initial position

[UIView beginAnimation];
[UIView setAnimationDuration:dim/35.];
[UIView setAnimationCurve:[UIViewAnimationCurveLinear];

your animation

[UIView commitAnimations];
...

with this calculation dim/35 calculations by the second place to get your animation 35px / s用第二名的这个计算 dim/35 计算得到你的 animation 35px/s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM