如何让一个计时器倒计时,另一个随机移动一个对象?

时间:2014-11-23 04:32:08

标签: ios objective-c xcode6 nstimer

我有正确的代码可以随机移动一个对象,也可以使数字倒计时但是当计时器倒计时我无法移动对象。我发现奇怪的一件事是当我拿出关于TimeLeft标签的部分时,对象随机移动但显然数字没有倒计时。

主要问题是:如何让计时器倒计时并让对象同时移动?

(我的主要目标是在计时器到达零时使对象停止移动)

如果有人可以帮忙解决这个问题,我会很感激吗?

-(void)Workauto{
secondsCount1 = 10;
autoperiods = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(autoperiod) userInfo:nil repeats:YES];
}

-(void)autoperiod{
secondsCount1 = secondsCount1 -1;
int minuts = secondsCount1/ 60;
int seconds = secondsCount1 - (minuts * 60);

NSString *timerOutput = [NSString stringWithFormat:@"%2d:%.2d", minuts , seconds];
Count.text = timerOutput;
if (secondsCount1 == 0){
    secondsCount1=0;
    [autoperiods invalidate];
    autoperiods=nil;

    Count.hidden=YES;
    AutoTimeLeftLabel.hidden=YES;
    TimeLeft.hidden=NO;
    TimeLeftlabel.hidden=NO;

    Ball.hidden=NO;
    Ball2.hidden=NO;
    BluBall.hidden=NO;
    BluBall2.hidden=NO;

    RedHigh1.hidden=YES;
    RedHigh2.hidden=YES;
    RedHigh3.hidden=YES;
    RedHigh4.hidden=YES;

    RedLow1.hidden=YES;
    RedLow2.hidden=YES;

    BlueHigh1.hidden=YES;
    BlueHigh2.hidden=YES;
    BlueHigh3.hidden=YES;
    BlueHigh4.hidden=YES;

    BlueLow1.hidden=YES;
    BlueLow2.hidden=YES;

    RedMid1.hidden=YES;
    RedMid2.hidden=YES;
    RedMid3.hidden=YES;
    RedMid4.hidden=YES;

    BlueMid1.hidden=YES;
    BlueMid2.hidden=YES;
    BlueMid3.hidden=YES;
    BlueMid4.hidden=YES;

    move = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(move) userInfo:nil repeats:YES];
    pos = CGPointMake(4.0, -4.0);
    move2 = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self selector:@selector(move2) userInfo:nil repeats:YES];
    pos2 = CGPointMake(3.0, 4.0);
    Update = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
    DPad = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(DPad) userInfo:nil repeats:YES];

    //RedGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(RedGoals) userInfo:nil repeats:YES];
    //BlueGoals = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(BlueGoals) userInfo:nil repeats:YES];
    [self SetTimer];
}
}

-(void)SetTimer{
comeonandcount = 150;
GameTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(GameTimerCo) userInfo:nil repeats:YES];
}

-(void)GameTimerCo{
comeonandcount = comeonandcount - 1;
int minuts = comeonandcount / 60;
int seconds = comeonandcount - (minuts * 60);

NSString *timerOutputGame = [NSString stringWithFormat:@"%2d:%.2d", minuts , seconds];
TimeLeft.text = timerOutputGame;

if (comeonandcount == 0){
    comeonandcount=0;
    [GameTimer invalidate];
    GameTimer=nil;

    [move invalidate];
    [move2 invalidate];
}
}

1 个答案:

答案 0 :(得分:0)

  

我发现奇怪的一件事是,当我拿出关于TimeLeft标签的部分时,对象随机移动但显然数字没有倒计时。

做得很好!你已经实际解决了这个问题。那部分不是很奇怪&#34 ;;这是问题的原因。

您正在使用自动布局。好吧,当你设置标签的文本时,会导致布局发生。因此,整个界面中的约束断言自己并将所有对象放回原位。它就这么简单。