使用循环移动 UIImageView 时断断续续
Moving UIImageView with a loop stutters
我有一个代码可以随机创建一个 UIImageView
,然后调用一个方法来循环动画以将它们向下移动到屏幕。
//This is called after an image is created
-(void)loopImage:(UIImageView *)image {
[self moveImage:image];
if (image.frame.origin.y > 300) {
[image removeFromSuperview];
} else {
[self performSelector:@selector(loopImage:) withObject:image afterDelay:1.0f / 60.0f];
}
}
-(void)moveImage:(UIImageView *)image {
image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y + 3, 20, 20);
}
这完全符合我的要求,但图像的移动并不流畅。有时,在它沿着屏幕向下移动时,它会断断续续地向后移动几个像素(不仅在模拟器上,而且在我的测试设备上,也是 iPhone 5s)。有什么方法可以让图像顺畅地向下移动屏幕,但仍然能够检查碰撞?
我认为这里最简单和正确的方法是使用[UIView animateWithDuration]
。我认为您使用的内容不能保证视图循环中的正确执行顺序。
我有一个代码可以随机创建一个 UIImageView
,然后调用一个方法来循环动画以将它们向下移动到屏幕。
//This is called after an image is created
-(void)loopImage:(UIImageView *)image {
[self moveImage:image];
if (image.frame.origin.y > 300) {
[image removeFromSuperview];
} else {
[self performSelector:@selector(loopImage:) withObject:image afterDelay:1.0f / 60.0f];
}
}
-(void)moveImage:(UIImageView *)image {
image.frame = CGRectMake(image.frame.origin.x, image.frame.origin.y + 3, 20, 20);
}
这完全符合我的要求,但图像的移动并不流畅。有时,在它沿着屏幕向下移动时,它会断断续续地向后移动几个像素(不仅在模拟器上,而且在我的测试设备上,也是 iPhone 5s)。有什么方法可以让图像顺畅地向下移动屏幕,但仍然能够检查碰撞?
我认为这里最简单和正确的方法是使用[UIView animateWithDuration]
。我认为您使用的内容不能保证视图循环中的正确执行顺序。