晕死啦,弄一个动画效果差不多折腾了一下午了,到后来原来就是因为位置写的有问题,不说了,记下来,以后不能再犯这种错啦:
UIView *test = [[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)];
test.backgroundColor = [UIColor redColor];
[self.view addSubview:test];
[test release];
[UIView beginAnimations:@"animationID" context:NULL];
[UIView setAnimationDuration:3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:999999];
CGRect rect = test.frame; //切记:下面的三句和上边的两句位置千万不能搞错啦
rect.origin.y = 320;
test.frame = rect;
[UIView commitAnimations];
//-------------------------------------------------------
注:发现一个问题,若用上边的代码实现,则是从上到下,接着从下到 上,。。。,但我想实现的是从上到下,然后直接返回上,。。。群里一个兄弟发来的代码,还没验证,先记着,用的是block实现,苹果好像推荐这种方法 :
- (void)animation
{
[UIView animateWithDuration:1.0 animations:^{
[test setFrame:CGRectMake(40, 320, 240, 1)];
}
completion:^(BOOL finished) {
if (finished) {
[test setFrame:CGRectMake(40, 220, 240, 1)];
[self animation];
}
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
test = [[[UIView alloc] initWithFrame:CGRectMake(40, 220, 240, 1)] autorelease];
test.backgroundColor = [UIColor redColor];
[self.view addSubview:test];
[self animation];
}