// 标签UILable -显示文字
// 1.创建标签
UILabel *lable = [[UILabel alloc] init];
// 2.设置标签的坐标和大小
[lable setFrame:CGRectMake(,, , )];
// 3.标签内容
lable.text = @"";
// 设置字体
lable.font = [UIFont systemFontOfSize:];
// 设置字体颜色
lable.textColor = [UIColor blackColor];
// 设置内容换行
lable.numberOfLines = ;
//设置圆角
lable.layer.cornerRadius = ;
lable.layer.masksToBounds = NO;
// 设置标签内容居中
lable.textAlignment = NSTextAlignmentCenter;
// 设置标签背景
lable.backgroundColor = [UIColor blackColor];
// 加载图片
UIImageView *tempView = [[UIImageView alloc] init];
[tempView setFrame:CGRectMake(, , , )];
tempView.image = [UIImage imageNamed:@"1.png"];
// 4.在当前视图之上加载子视图
[self.view addSubview:lable];
[lable addSubview:tempView];
#import "ViewController.h" @interface ViewController ()
{
NSTimer *timer;
NSInteger length;
}
@property (weak, nonatomic) IBOutlet UILabel *lable; @end @implementation ViewController -(void) lableMove
{
if (self.lable.frame.origin.y>) {
length = -;
}
else if (self.lable.frame.origin.y<)
{
length = ;
}
CGRect rect = self.lable.frame;
rect.origin.y +=length;
self.lable.frame = rect; if (self.lable.frame.origin.y>) {
[timer invalidate];
[self.lable removeFromSuperview];
}
} - (void)viewDidLoad {
[super viewDidLoad];
/*
NSTimeInterval 计时器响应时间间隔
target:计时器的响应者
selector:计时器的响应方法
userInfo:nil
repeats:计时器是否重复响应
*/
length = ;
timer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(lableMove) userInfo:nil repeats:YES]; //将定时器插入runLoop(超级死循环)中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}