ios - 如何在 IOS 中隐藏 UILabel 中的文本

标签 ios objective-c uilabel

我试图在 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 秒后按顺序调用,但这样效率很低。

还有其他方法吗?

最佳答案

改变这个方法

希望它对你有用

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];
}

关于ios - 如何在 IOS 中隐藏 UILabel 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21570231/

相关文章:

iphone - 向 iTunesConnect 中发布的新 iOS 应用程序版本添加新设备要求

objective-c - 解决涉及枚举的 Swift.h 和 Bridging-Header.h 循环引用

ios - 表格内的动态大小标签在 ios 中不刷新

ios - 带有未格式化表情符号的 NSAttributedString 结尾

swift - UILabel 上的渐变?

ios - 在 didFinishLaunchingWithOptions 完成之前显示 UIAlertView?

ios - 如何修复 iOS14 后未显示的 UITextField 背景图像?

ios - MGTwitter 引擎 - 返回用户名

ios - UIButton RAC 绑定(bind)

ios - 有谁知道适用于 iOS 的 newpipe android 项目吗?