我正在尝试按顺序在UILabel
上显示一组数字。
例如:我有一个包含5个数字的数组,每次我希望数组中的一个数字在屏幕上显示2秒钟然后消失,然后是下一个数字......
这是我到目前为止所做的,但它并不是我想要的。下面的代码只显示数组中的最后一个数字2秒,然后消失。
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray* myArray = [NSMutableArray array];
for (int i=0; i<5; i++) {
NSNumber *temp;
temp = [myArray objectAtIndex:i];
[self displayNumber:temp];
[myLabel setHidden:NO];
}
}
-(void) displayNumber: (NSNumber *) myNumber{
myLabel.text = [NSString stringWithFormat:@"%@", myNumber];
NSTimer *myTimer;
myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(hideLabel) userInfo:Nil repeats:NO];
}
-(void) hideLabel{
[myLabel setHidden:YES];
}
我想过有5个函数,每个函数在2秒后按顺序调用,但效率非常低。
我还有其他方法吗?
答案 0 :(得分:1)
更改此方法
希望它能为你提供你想要的东西
1)定义
NSMutableArray* myArray , int repeatCount; and NSTimer *myTimer in .h file and set its property;
2)
-(void)viewDidLoad
{
[super viewDidLoad];
//This will initialize your timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showLabel) userInfo:Nil repeats:NO];
//This will trigger your timer
[myTimer fire];
//Your timer will run in a loop
[[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
myArray = [NSMutableArray array];
}
-(void) displayNumber: (NSNumber *) myNumber{
myLabel.text = [NSString stringWithFormat:@"%@", myNumber];
}
-(void) showLabel{
if(repeatCount >= [myArray count])
{
repeatCount = 0;
}
NSNumber *temp = [myArray objectAtIndex:repeatCount];
repeatCount ++;
[self displayNumber:temp];
[myLabel setHidden:NO];
}
答案 1 :(得分:0)
我认为,你可以使用带计数器的计时器。
创建3个属性:
@property (nonatomic, assign) int counter;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) NSTimer *timer;
在viewDidLoad中设置它们:并启动计时器(重复):
- (void)viewDidLoad {
[super viewDidLoad];
_counter = 0;
_array = @[@0, @1, @2, @3, @4, @5];
_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextLabel) userInfo:Nil repeats:YES];
}
更新标签:
- (void)nextLabel{
if (_counter == [_array count]) {
//stop in the end of array
[_timer invalidate];
return;
}
[_label setText:[NSString stringWithFormat:@"%@", _array[_counter]]];
_counter++;
}
答案 2 :(得分:0)
试试这个:
// Initialize the timer when you want to change the first number
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(changeNumber:) userInfo:@{@"currentNumber" : [NSNumber numberWithInt:0]} repeats:NO];
-(void)changeNumber:(NSTimer *)timer
{
NSDictionary * dictionary = [timer userInfo];
int number = [(NSNumber *)[dictionary objectForKey:@"currentNumber"] integerValue];
if (number < [numbersArray count])
{
myLabel.text = [NSString stringWithFormat:@"%@", [numbersArray objectAtIndex:number]];
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeNumber:) userInfo:@{@"currentNumber" : [NSNumber numberWithInt:number + 1]} repeats:NO];
}
}
答案 3 :(得分:0)
将带有NSNumber实例的数组传递给下面的方法
- (void)updateLabelWithArray:(NSMutableArray *)array
{
if ([array count] != 0)
{
myLabel.text = [NSString stringWithFormat:@"%@", [array firstObject]];
[array removeObjectAtIndex:0];
[self performSelector:@selector(updateLabelWithArray:)
withObject:array
afterDelay:2.0f];
}
}
应该这样做。
答案 4 :(得分:0)
试试这个希望这有助于你.. :))
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
myArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:3],[NSNumber numberWithInt:5],[NSNumber numberWithInt:6],[NSNumber numberWithInt:7],[NSNumber numberWithInt:10], nil]; //your array contains any number you want
index = 0; //showing the numbers from 0th index to last indx
[self.myLabel setHidden:NO];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(DisplayFortwoSec) userInfo:nil repeats:YES];//call the method reguraly for 2 sec
}
- (void)DisplayFortwoSec
{
[self.myLabel setHidden:YES];
if(index == ([myArray count] - 1))
index = 0;//other stuffs to come out of loop like invalidating the timer
[self.myLabel setHidden:NO];
NSNumber *num = [myArray objectAtIndex:index];
NSString *strNum = [num stringValue];
self.myLabel.text = strNum;
[self.myLabel setHidden:NO];
index ++;
}