UIKit基本使用
UIColor
一个UIColor代表一种颜色,通过UIColor的类方法,可以获得很多常用的颜色
+ (UIColor *)blackColor; // 0.0 white 黑色
+ (UIColor *)darkGrayColor; // 0.333 white 深灰色
+ (UIColor *)lightGrayColor; // 0.667 white 亮灰色
+ (UIColor *)whiteColor; // 1.0 white 白色
+ (UIColor *)grayColor; // 0.5 white 灰色
+ (UIColor *)redColor; // 1.0, 0.0, 0.0 RGB 红色
+ (UIColor *)greenColor; // 0.0, 1.0, 0.0 RGB 绿色
+ (UIColor *)blueColor; // 0.0, 0.0, 1.0 RGB 蓝色
+ (UIColor *)cyanColor; // 0.0, 1.0, 1.0 RGB 青色
+ (UIColor *)yellowColor; // 1.0, 1.0, 0.0 RGB 黄色
+ (UIColor *)magentaColor; // 1.0, 0.0, 1.0 RGB 品红
+ (UIColor *)orangeColor; // 1.0, 0.5, 0.0 RGB 橙色
+ (UIColor *)purpleColor; // 0.5, 0.0, 0.5 RGB 紫色
+ (UIColor *)brownColor; // 0.6, 0.4, 0.2 RGB 棕色
+ (UIColor *)clearColor; // 0.0 white, 0.0 alpha 清除颜色(空色
UIImage
下面演示用代码创建按钮
一个UIImage对象代表一张图片,一般通过imageNamed:方法就可以通过文件名加载项目中的图片(PNG格式的图片可以省略扩展名)
// 2. 图片视图
CGFloat imageW = 200;
CGFloat imageX = (320 - imageW) / 2; // 图片居中
CGFloat imageH = 200;
CGFloat imageY = 80;
// 实例化一个图像视图
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
// 实例化一个图像
/**
[UIImage imageNamed:imageName];
在图片使用完成后,不会直接被释放掉,具体释放时间由系统决定
适用于图片小,常用的图像处理
*/
UIImage *image = [UIImage imageNamed:@"图片名称"];
// 将图像显示在imageView中
imageView.image = image;
// 添加到视图
[self.view addSubview:imageView];
UIButton
在用代码创建按钮的同时指定按钮样式
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIButtonTypeCustom:无类型,按钮的内容需要自定义
UIButtonTypeDetailDisclosure:
UIButtonTypeInfoLight:
UIButtonTypeInfoDark:
UIButtonTypeContactAdd:
按钮是有多种状态的:
normal(普通状态):默认情况, UIControlStateNormal
highlighted(高亮状态):按钮被按下去的时候(手指还未松开), UIControlStateHighlighted
设置按钮在不同状态下的背景图片(为了保证高亮的图片正常显示,必须设置按钮的type为custom)
下面演示用代码创建按钮
// 1.创建一个自定义的按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 2.添加按钮
[self.view addSubview:btn];
// 3.设置按钮的位置和尺寸
btn.frame = CGRectMake(0, 0, 100, 100);
// 设置居中 x: 右侧图片的x起点位置/2
leftBtn.center = CGPointMake(self.icon.frame.origin.x / 2, self.icon.center.y);
// 设置居中 x: 视图宽度 - 右侧图片的x起点位置/2
rightBtn.center = CGPointMake(self.view.frame.size.width - self.icon.frame.origin.x / 2, self.icon.center.y);
// 4.监听按钮点击(点击按钮后就会调用self的btnClick方法)
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
// 5.设置按钮在默认状态下的属性
// 5.1.默认状态的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"]
// 默认背景
[btn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
// 高亮背景
[btn setBackgroundImage:[UIImage imageNamed:@"right_highlighted"] forState:UIControlStateHighlighted];
forState:UIControlStateNormal];
// 5.2.默认状态的文字
[btn setTitle:@"点我啊" forState:UIControlStateNormal];
// 5.3.默认状态的文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 6.设置按钮在高亮状态下的属性
// 6.1.高亮状态的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];
// 6.2.高亮状态的文字
[btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
// 6.3.高亮状态的文字颜色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
// 创建一个自定义的按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 默认状态的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
// 默认状态的文字
[btn setTitle:@"点我啊" forState:UIControlStateNormal];
// 默认状态的文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 设置按钮是否能够点击
self.btn.enabled = (self.index != 4);
UILabel
下面演示用代码创建按钮
// 1. 序号标签
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 320, 40)];
// 设置内容
label.text = @"1/5";
// 设置行数 默认1行,0为不限制行数
label.numberOfLines = 0;
// 居中对齐
label.textAlignment = NSTextAlignmentCenter;
// 设置透明度
label.alpha = 1.0;
// 添加到视图
[self.view addSubview:label];
// 上面为实例化一个UILabel显示在视图上,提示用户下载完成
// 动画效果 完成之后,将Label从视图中删除
// 1. 首尾式动画,只能做动画,要处理完成后的操作不方便
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
label.alpha = 1.0;
[UIView commitAnimations];
// 2. block动画比首尾式动画简单,而且能够控制动画结束后的操作 在iOS中,基本都使用首尾式动画
[UIView animateWithDuration:2.0 animations:^{
label.alpha = 0.0;
} completion:^(BOOL finished) {
// 删除label
[label removeFromSuperview];
}];
UILabel
下面演示用代码创建按钮
1.介绍UIScrollView的背景
2.介绍scrollView一些属性(主要有三个)
1>.要想使用scrollView必须做两件事
1.设置scrollView内容
2.设置contentSize
2>.其他重要属性
1.contentOffset(滚动位置)
2.contentInset(注意:在storyborad里面设置效果不同)
3.喜马拉雅项目
1>.分析页面结构(scrollView的frame确定)
2>.在storyboard拖控件
3>.重点scrollView(设置内容并且设置contentSize)
4>.调节scrollView的显示位置
4.代理
1>代理思想两个思想
1).监听思想:B监听A发生了什么事情
2).通知思想:A发生了一些事情,要通知B去做
2>scrollView的代理使用
1).如何成为代理(三步)
*声明协议
*设置代理对象self.scrollView.delegate = self;
*实现协议方法
2).代理监听scrollView的拖拽事件
3).用代理实现缩放
*成为UIScrollView的代理()
*设置缩放对象(通过viewForZoomingInScrollView方法)
*设置缩放为范围(maximumZoomScale、minimumZoomScale)
1.图片轮播(可以采用两种维度去分解工作:功能、MVC)
1.UI(分析UI如何实现storyboard、代码创建)
1.scrollView(有两件事)
2.图片(代码增加到scrollView)
3.UIPageControl(需设置总页数、当前页码)
2.业务
1.拖动(整页切换,UIScrollView的宽度为一页)
2.页码设置(当前是第几页)
3.自动滚动
4.优化(timer的机制:触摸式移除,放开时再加进来)
2.智能猜图扩展(代理)
1.UIAlertView的使用以及常见代理使用 (介绍UIWindow的level模式,level最高是UIWindowLevelStatusBar,可以覆盖status bar)
2.UIActionSheetView简单使用以及代理使用(强调了危险操作,标红显示按钮)
3.应用管理扩展(定义协议并使用)
1.定义协议(三步)
*定义protocol(两种optional[代理对象可不实现]、required[代理对象必须实现])
*增加代理属性(weak) @property (weak, nonatomic) id<LFAppInfoViewDelegate> delegate;
*给代理发消息,调用代理的方法(需要判断代理对象是否实现了该方法,不判断调用后(编译时不会)会报错)
注意:定义协议的名称命名[类名+Delegate]、协议方法的命名规范[方法名称需要去掉前缀,并且将自己作为参数]
2.使用代理(三步)
*声明协议
*设置代理对象
*实现协议方法(本例是在代理对象[控制器] 添加一个UILabel)
tableView
1.调用数据源的下面方法得知一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
2.调用数据源的下面方法得知每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.调用数据源的下面方法得知每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableViewCell
还有一个非常重要的问题:有时候需要自定义UITableViewCell(用一个子类继承UITableViewCell),而且每一行用的不一定是同一种UITableViewCell,所以一个UITableView可能拥有不同类型的UITableViewCell,对象池中也会有很多不同类型的UITableViewCell,那么UITableView在重用UITableViewCell时可能会得到错误类型的UITableViewCell
解决方案:UITableViewCell有个NSString *reuseIdentifier属性,可以在初始化UITableViewCell的时候传入一个特定的字符串标识来设置reuseIdentifier(一般用UITableViewCell的类名)。当UITableView要求dataSource返回UITableViewCell时,先通过一个字符串标识到对象池中查找对应类型的UITableViewCell对象,如果有,就重用,如果没有,就传入这个字符串标识来初始化一个UITableViewCell对象
Cell的重用代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.定义一个cell的标识
static NSString *ID = @"mjcell";
// 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果缓存池中没有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 4.设置cell的属性...
return cell;
}