Ios NSTimer阻止我的图像移动
当我按下按钮时,我的图像将移动到一个方向 游戏的目标是,他们将移动图像以命中另一个图像,为此,我使用了Ios NSTimer阻止我的图像移动,ios,objective-c,nstimer,Ios,Objective C,Nstimer,当我按下按钮时,我的图像将移动到一个方向 游戏的目标是,他们将移动图像以命中另一个图像,为此,我使用了NSTimer 然后他们还有另一个NSTimer,因为他们只有15秒的时间来点击另一个图像。如果他们不这样做,游戏将[自动结束],但是,开始计时15秒的n计时器,我移动图像图像移动一点,然后图像再次回到中心 当我删除倒计时时,在我停止按下按钮后,图像不会返回到中心,但它会停止在其当前位置 这就是我在使用倒计时的时候想要它做的 代码: -(无效)高尔夫球场 { ball.center=CGPoi
NSTimer
然后他们还有另一个NSTimer
,因为他们只有15秒的时间来点击另一个图像。如果他们不这样做,游戏将[自动结束],但是,开始计时15秒的n计时器
,我移动图像图像移动一点,然后图像再次回到中心
当我删除倒计时时,在我停止按下按钮后,图像不会返回到中心,但它会停止在其当前位置
这就是我在使用倒计时的时候想要它做的
代码:
-(无效)高尔夫球场
{
ball.center=CGPointMake(ball.center.x-5,ball.center.y);
}
-(i动作)左
{
goLeft=[NSTimer scheduledTimerWithTimeInterval:0.05目标:自选择器:@selector(goLeft)userInfo:nil repeats:YES];
if(goLeft==nil)goLeft=[NSTimer scheduledTimerWithTimeInterval:0.05目标:自选择器:@selector(goLeft)用户信息:nil重复:YES];
}
-(iAction)左停
{
[高尔夫无效];
高尔夫=零;
}
//这是计时器:
-(iAction)doCountdown:(id)发送方;{
if(倒计时)
返回;
剩余刻度=25;
[自我更新标签];
倒计时=[NSTimer scheduledTimerWithTimeInterval:1.0目标:自选择器:@selector(handleTimerTick)userInfo:nil repeats:YES];
}
-(无效)handleTimerTick
{
剩下的蜱——;
[自我更新标签];
if(remainingTicks)我强烈建议不要使用NSTimer
来计时动画。如果您愿意,我可以回答有关SpriteKit
的任何问题,这是一个更好(更容易)的方法为动作设置动画的替代方法。@JaredH同意。至少,如果OP坚持使用UIKit
,他应该使用CADisplayLink
将更新与运行循环同步,或者使用核心动画来处理动画。@JaredH,如果你可以通过按住按钮来左右移动图像,然后停止还有,我能用计时器倒计时他们还有多少时间完成任务?谢谢answear:D@bbum好的,让我给你举个例子,比如当我移动它的时候left@JaredH如果是客观的,我很好:)但是我真的不喜欢斯威夫特
-(void)goLeft
{
ball.center = CGPointMake(ball.center.x -5, ball.center.y);
}
-(IBAction)left
{
goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(goLeft) userInfo:nil repeats:YES];
if (goLeft == nil) goLeft = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(goLeft) userInfo:nil repeats:YES];
}
-(IBAction)stopLeft
{
[goLeft invalidate];
goLeft = nil;
}
//And heres the timer :
-(IBAction)doCountdown: (id)sender;{
if (countdownTimer)
return;
remainingTicks = 25;
[self updateLabel];
countdownTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target: self selector: @selector(handleTimerTick) userInfo: nil repeats: YES];
}
-(void)handleTimerTick
{
remainingTicks--;
[self updateLabel];
if (remainingTicks <= 0) {
[countdownTimer invalidate];
countdownTimer = nil;
}
}
-(void)updateLabel
{
theLabel.text = [[NSNumber numberWithUnsignedInt: remainingTicks] stringValue];
}