2016-12-06 17 views
0

は、私が2秒で0%〜50%Objective-CでUILabelが徐々に表示する数値を徐々に増やすには?

UILabel *percentage.text = [NSString stringWithFormat:@" %ld%%", 0]; 

からUILabelで表示される数を増やしたいと言います。ここで

percentage.text = [NSString stringWithFormat:@" %ld%%", 0] 

は、私はNSTimerを使用しようとしたものです:

self.percent = 0; 
self.timer = [NSTimer timerWithTimeInterval:0.04 
            target:self 
            selector:@selector(updateLabel) 
            userInfo:nil 
            repeats:YES]; 
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; 

//updateLabel 
- (void)updateLabel{ 
    self.percentage.text = [NSString stringWithFormat:@" %ld%%", self.percent++]; 
    if (self.percent > 50) { 
     [self.timer invalidate]; 
     self.timer = nil; 
    } 
} 

をしかし、UIは確か際に、非常にぎくしゃく見えるすべての変更が積層されているように、すべてのルックスで、滑らかではない、時には適用されますがありません確実に2秒以上かかる。これは0.04秒が短すぎるためですか?

良い方法がありますか?

答えて

0

これはあなたの問題の原因であるかどうかはわからない...

あなたのラベルテキストたびに新しい時間を追加しないでください。毎回タイマーが起動すると、新しい値にラベルを設定します。そして、あなたがスムーズに実行を取得し、タイマーの時間間隔を調整し

self.percentage.text = [NSString stringWithFormat: @"%ld%%", 
    self.percent++] 
+0

申し訳ありませんが、問題はないのthats –

0

:という固定

self.timer = [NSTimer timerWithTimeInterval:0.0001 
             target:self 
             selector:@selector(updateLabel) 
             userInfo:nil 
             repeats:YES]; 
関連する問題