iOS UI-Lable标签、NStimer定时器和RunLoop超级死循环

 1     // 标签UILable -显示文字
 2     // 1.创建标签
 3     UILabel *lable = [[UILabel alloc] init];
 4     // 2.设置标签的坐标和大小
 5     [lable setFrame:CGRectMake(0,20, 375, 50)];
 6     // 3.标签内容
 7     lable.text = @"";
 8     // 设置字体
 9     lable.font = [UIFont systemFontOfSize:20];
10     // 设置字体颜色
11     lable.textColor = [UIColor blackColor];
12     // 设置内容换行
13     lable.numberOfLines = 0;
14     //设置圆角
15     lable.layer.cornerRadius = 10;
16     lable.layer.masksToBounds = NO;
17     // 设置标签内容居中
18     lable.textAlignment = NSTextAlignmentCenter;
19     // 设置标签背景
20     lable.backgroundColor = [UIColor blackColor];
21     // 加载图片
22     UIImageView *tempView = [[UIImageView alloc] init];
23     [tempView setFrame:CGRectMake(255, 0, 120, 50)];
24     tempView.image = [UIImage imageNamed:@"1.png"];
25     // 4.在当前视图之上加载子视图
26     [self.view addSubview:lable];
27     [lable addSubview:tempView];
 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 {
 5     NSTimer *timer;
 6     NSInteger length;
 7 }
 8 @property (weak, nonatomic) IBOutlet UILabel *lable;
 9 
10 @end
11 
12 @implementation ViewController
13 
14 -(void) lableMove
15 {
16     if (self.lable.frame.origin.y>667) {
17         length = -10;
18     }
19     else if (self.lable.frame.origin.y<0)
20     {
21         length = 10;
22     }
23     CGRect rect = self.lable.frame;
24     rect.origin.y +=length;
25     self.lable.frame = rect;
26     
27     if (self.lable.frame.origin.y>700) {
28         [timer invalidate];
29         [self.lable removeFromSuperview];
30     }
31 }
32 
33 - (void)viewDidLoad {
34     [super viewDidLoad];
35     /*
36      NSTimeInterval 计时器响应时间间隔
37      target:计时器的响应者
38      selector:计时器的响应方法
39      userInfo:nil
40      repeats:计时器是否重复响应
41      */
42     length = 10;
43     timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(lableMove) userInfo:nil repeats:YES];
44     
45     //将定时器插入runLoop(超级死循环)中
46     [[NSRunLoop currentRunLoop]  addTimer:timer forMode:NSRunLoopCommonModes];
47 }
原文地址:https://www.cnblogs.com/oc-bowen/p/5069691.html