IOS:使用计时器移动图像
嗨,我有一个问题,当我点击一个按钮 我需要一个计时器,它将我的代码停止几毫秒,以创建流体的运动 这是我的代码:IOS:使用计时器移动图像,ios,image,move,fluid,Ios,Image,Move,Fluid,嗨,我有一个问题,当我点击一个按钮 我需要一个计时器,它将我的代码停止几毫秒,以创建流体的运动 这是我的代码: - (IBAction)goRight:(id)sender { sprite.image = [UIImage imageNamed:@"sprite_rr.png"]; [NSThread sleepForTimeInterval:1]; sprite.center = CGPointMake(sprite.center.x + 5, sprite.cent
- (IBAction)goRight:(id)sender {
sprite.image = [UIImage imageNamed:@"sprite_rr.png"];
[NSThread sleepForTimeInterval:1];
sprite.center = CGPointMake(sprite.center.x + 5, sprite.center.y);
sprite.image = [UIImage imageNamed:@"sprite_lr.png"];
}
但这段代码需要30毫秒,然后直接执行,无需等待
你能帮我吗?
谢谢
PS:很抱歉我的英语不好^使用
[NSThread sleepForTimeInterval:1]的错误做法代码>。这将停止在主线程上运行的所有内容。当到达这一行代码时,主线程将在指定的时间内被阻塞,然后剩余的代码将被执行。请记住,当您阻止线程时,您的应用程序将冻结,因为所有用户界面更新都是在此线程上完成的,无法执行。除非你有理由,否则不要使用这个
如果没有提供更多的代码,或者在设置UIImageView的动画时,您希望停止什么的示例,我认为我们无法提供进一步的帮助。您可以使用以下内容:
[UIView animateWithDuration: 1
delay: 0.03 // delays the animation for however long you set. This is 30 ms.
options: UIViewAnimationOptionCurveEaseIn
animations: ^{
[sprite setFrame:CGRectMake(sprite.frame.origin.x+5, sprite.frame.origin.y, sprite.frame.size.width, sprite.frame.size.height)];
}
completion: ^(BOOL finished){}];
尝试:
为了直观地为对象设置动画或“移动”,您需要使用animateWithDuration:animations:
方法
编辑:尝试设置框架而不是修改中心。谢谢,但图像是一样的,没有变化:/I编辑了我的答案,现在就试试。通常设置新的框架是一件痛苦的事情。很抱歉,我是iOS上的新开发人员,当我编写“frame.point.x”时,出现了一个错误。IDE说使用frame.size或frame.originsorry,frame.origin.x
谢谢,太完美了!谢谢:)谢谢,但图像是一样的,没有变化:/
- (IBAction)goRight:(id)sender {
sprite.image = [UIImage imageNamed:@"sprite_rr.png"];
CGRect frame = sprite.frame;
frame.origin.x += 5;
[UIView animateWithDuration:1.0 animations:^{
[sprite setFrame: frame];
sprite.image = [UIImage imageNamed:@"sprite_lr.png"];
}];
}