如何编号减少?

时间:2014-03-28 19:09:15

标签: objective-c

当用户获得硬币或使用硬币时,我希望他们体验他们的硬币减去现场( - ),而不是显示{score = score -10}的标签,突然间10点消失:

相反,他们应该看到以动画的方式扣除分数50 - 49 - 48 - 47 - 46,依此类推,直到10分完全减去。

代码:

[scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]]; 

代码:例如: - 100 | - 10 | +3 | +5

for (int i = 1; i<= 10; i++) { NSLog (@“%d”, i);

3 个答案:

答案 0 :(得分:1)

你好Jelter,

你可以试试这个。它会起作用......

//Create a timer object

@property (strong, nonatomic) NSTimer *timer;

//Score label

@property (weak, nonatomic) IBOutlet UILabel *score;

//想要更新分数时调用此项我假设按下您更新分数的按钮。

- (IBAction)scoreButtonPressed:(id)sender {
    self.timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self   selector:@selector(updateScore) userInfo:nil repeats:YES];
}

-(void)updateScore{
    static int i = 0;
    CATransition *animation = [CATransition animation];
    animation.duration = 1.0;
    animation.type = kCATransitionFade;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.score.layer addAnimation:animation forKey:@"changeTextTransition"];

    // Change the text
    self.score.text = [NSString stringWithFormat:@"%d", [self.score.text intValue] - 1];
    i++;
    if (i == 10 && [self.timer isValid]) {
        [self.timer invalidate];
        i = 0;
    }
  }

答案 1 :(得分:-1)

在未经测试的情况下输入:

//currentScore keeps track of current score.  Defined in interface
//timer defined in interface
timer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self   selector:@selector(updateScore:) userInfo:@(currentScore - 10) repeats:YES];


- (void) updateScore:(NSNumber *)targetScore
{
    currentScore--;

    [scoreLabel setText:[NSString stringWithFormat:@"score: %d", currentScore]]; 

    if (currentScore==[targetScore intValue])
    {
        //Exit timer when goal is reached
        [timer invalidate];
    }
}

答案 2 :(得分:-2)

您可以使用for循环执行此操作。你需要添加延迟,因为这会发生得如此之快以至于你无法看到它。这可能会对您有所帮助Objective-C delay action with blocks

-(void)subtractIntegerFromScore:(int)integer {
    for (int i = 0; i < integer; i++) {
        score = score - 1;
        [scoreLabel setText:[NSString stringWithFormat:@"score: %d", score]]; ;
    }
}