如何做一个连续循环的UIView动画 [英] how to do a continuous recycling UIView animation

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

问题描述

以下code移动,从右边的ImageView的左一次,但我想不间断的做。向右移动到左,然后搬回离开屏幕外,重复右再次离开。

 的ImageView = [[UIImageView的页头] initWithFrame:方法CGRectMake(320,200,2635,200);图像= [UIImage的imageWithContentsOfFile:[一个NSBundle mainBundle] pathForResource:@动物ofType:@PNG]];
               [ImageView的setImage:图像]。[self.view addSubview:ImageView的]。[UIView的beginAnimations:@MoveAndStrech背景:无];
[UIView的setAnimationDuration:40]; // 30
[UIView的setAnimationBeginsFromCurrentState:YES];
CGPoint P,B,A,C,D,E;
p.x = 0;
p.y = 200;imageview.center = P;[UIView的commitAnimations];


解决方案

添加以下行:

  [UIView的setAnimationRepeatCount:HUGE_VAL];
[UIView的setAnimationRepeatAutoreverses:YES];

或者,如果你的目标的iOS 4.0+然后基于受阻的动画是preferred:

  [UIView的animateWithDuration:40.0f
                      延迟:0.0
                    选项​​:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                 动画:^(无效){imageview.center = CGPointMake(0,200);}
                 完成:NULL];

The following code move the the imageview from right to left once, but I want to do continously. move right to to left then move back to left offscreen and repeat right to left again.

imageview=[[UIImageView alloc] initWithFrame:CGRectMake(320, 200, 2635, 200)];

image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animal" ofType:@"png"]]; 
               [imageview setImage:image];

[self.view addSubview:imageview];

[UIView beginAnimations:@"MoveAndStrech" context:nil];
[UIView setAnimationDuration:40];     //30
[UIView setAnimationBeginsFromCurrentState:YES];
CGPoint p, b, a, c, d,e;
p.x = 0;
p.y = 200;

imageview.center=p;

[UIView commitAnimations];  

解决方案

Add the following lines:

[UIView setAnimationRepeatCount: HUGE_VAL];
[UIView setAnimationRepeatAutoreverses: YES];

Or if you target iOS 4.0+ then blocked-based animations are preferred:

[UIView animateWithDuration:40.0f
                      delay:0.0f
                    options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState
                 animations: ^(void){imageview.center = CGPointMake(0, 200);}
                 completion:NULL];

这篇关于如何做一个连续循环的UIView动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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