昨天学习了工程的建立和UIView视图的简单使用,今天总结一些最基本的控件
一、UILabel
标签,很简单,显示文字的
下面是UILabel的各种属性
//初始化设置大小,有自己的初始化方法就用自己的,没有就用父类的 UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10,250, 150)]; //设置Label的行数,前提是Frame高度要够,当设置为0,文字会根据高度自动折行 myLabel.numberOfLines = 0; //设置label背景色 [myLabel setBackgroundColor:[UIColor greenColor]]; myLabel.text = @"He that would command must serve."; //设置字体的颜色 myLabel.textColor = [UIColor blueColor]; //输出系统默认的字体大小 NSLog(@"%@",myLabel.font); //设置字体大小 myLabel.font = [UIFont boldSystemFontOfSize:20];//加粗bold,有些字体不支持 //阴影的颜色和偏移量 myLabel.shadowColor = [UIColor brownColor]; myLabel.shadowOffset = CGSizeMake(3, 3); //得到系统提供的所有字体 NSArray *fonts = [UIFont familyNames]; NSLog(@"%@",fonts); //myLabel.font = [UIFont fontWithName:@"Zapfino" size:15]; //设置Label的折行方式 myLabel.lineBreakMode = NSLineBreakByTruncatingMiddle; //设置文字对齐方式,居中,左对齐等等 myLabel.textAlignment = NSTextAlignmentCenter; //把Label添加到window上 [self.window addSubview:myLabel];
二、UIIamgeView
类似于相框,先创建好再放图片进去
首先拖动一个图片ganggang.jpg到工程目录下
在代码中添加相框和照片对象,这是图片最基本的应用。
在工程中不允许有两个名字相同的图片,即使路径不同
//初始化相框 UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 200, 200, 300)]; //添加相框到window [self.window addSubview:myImageView]; //创建一个图片对象,当前目录下的图片名 UIImage *myImage = [UIImage imageNamed:@"ganggang.jpg"]; //也可以根据路径得到图片,创建一个路径字符串 NSString *path = [[NSBundle mainBundle]pathForResource:@"/Users/Documents/Class_02_UIButtonImage" ofType:@"jpg"]; //根据路径得到图片 UIImage *image = [UIImage imageWithContentsOfFile:path]; //把图片对象赋给相框 [myImageView setImage:myImage];运行后可以看到label和图片的效果。
三、UIView动态图制作
通过将gif的每一帧图放在UIImageView的动态数组里,进行播放操作
#pragma mark 动态图 //创建一个ImageView相框 UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(100,50, 250, 250)]; //imageV.center = self.window.center; //将gif的每一帧放入数组中 NSMutableArray *imageArray = [NSMutableArray array]; for (int i = 1; i <= 20; i++) { UIImage *itemImage = [UIImage imageNamed:[NSString stringWithFormat:@"zc%d.tiff",i]]; [imageArray addObject:itemImage]; } //将一组图片放在imageView的动态数组里面 imageV.animationImages = imageArray; //设置动画持续时间秒,所有图片在这个时间内播完 imageV.animationDuration = 2; //设置循环次数并让动画开始,两句顺序不能颠倒 imageV.animationRepeatCount = 1000; //操作动画开始 //[imageV startAnimating]; //动画未开始时设置占位图片 imageV.image = [UIImage imageNamed:@"zc1.tiff"]; //显示在window上 [self.window addSubview:imageV]; //设置相框的标签 imageV.tag = 1000; //创建一个按钮,控制动画开始和结束 UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [startBtn setTitle:@"开始动画" forState:UIControlStateNormal]; startBtn.frame = CGRectMake(0, 0, 100, 100); [self.window addSubview:startBtn]; [startBtn addTarget:self action:@selector(startBtnTouch:) forControlEvents:UIControlEventTouchUpInside]; -(void)startBtnTouch:(UIButton*)sender { //得到相框的标签 UIImageView *imageV = (UIImageView*)[self.window viewWithTag:1000]; sender.selected = !sender.selected; if (sender.selected) { //控制动图开始 [imageV startAnimating]; } else //动图停止 [imageV stopAnimating]; }
图片素材:不会上传压缩包...自己找张gif把每一帧拖出来命名
效果如图,当然他会动
明天总结button的使用