iOS 基础2
2016-06-27 22:19
381 查看
// UIImageView
// 1. 创建对象
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:(CGRectMake(10,
100, 256,
192))];
// 打开用户交互
imageView.userInteractionEnabled =
YES;
// 2. 添加图片
// imageView.image = [UIImage imageNamed:@"avatar.jpg"]; // imageNamed
这种方式图片必须在工程中, 如果图片是
非png格式,一定要加后缀.jpg .xxx
// UIImage 对象的路径创建方法
imageView.image = [UIImage
imageWithContentsOfFile:@"/Users/wushumin/Pictures/sai专用文件夹/avatar.jpg"];
// 根据文件路径创建
// 3. 把图片视图添加到 self.view
上
[self.view
addSubview:imageView];
// 4. 释放
[imageView release];
// 手势识别器
重点:
// 手势识别器
是封装了一系列的触摸事件,来表示某一个事件
// UIView 继承自 UIResponder
,虽然可以响应用户的触摸操作,但是太麻烦。因此苹果公司
设计了一系列手势识别器方便我们调用
// 只要一个视图上面添加了手势识别器,就可以识别出该手势。
// 常用手势识别器有七种:
// 1. tap 轻拍手势
// 1. 创建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tapAction:)];
// 2. 设置属性
比如:轻拍手势有 轻拍几次
几根手指轻拍
tap.numberOfTapsRequired =
1; //
默认为1,拍一次
tap.numberOfTouchesRequired =
1; //
默认为1,一根手指
// 3. 添加给某个视图
// [imageView addGestureRecognizer:tap];
// 4. 释放
[tap release];
// 2. longPress
长按手势
// 创建
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(longPressAction:)];
// 设置属性
最短长按时间
longPress.minimumPressDuration =
2;// 2秒
// 添加
// [imageView addGestureRecognizer:longPress];
[longPress release];
// 3. pan 平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc] initWithTarget:self
action:@selector(panAction:)];
// [imageView addGestureRecognizer:pan];
[pan release];
// 4. rotation
旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc] initWithTarget:self
action:@selector(rotationAction:)];
// [imageView addGestureRecognizer:rotation];
// 5. pinch 捏合手势
(缩放手势)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(pinchAction:)];
// [imageView addGestureRecognizer:pinch];
[pinch release];
// 6. swipe 轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(swipeAction:)];
// 轻扫手势默认向右轻扫,
swipe.direction =
UISwipeGestureRecognizerDirectionRight;
// [imageView addGestureRecognizer:swipe];
[swipe release];
// 7. 屏幕边缘移动(轻扫)
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer
alloc] initWithTarget:self
action:@selector(sceenEdgePanAction:)];
// 屏幕边缘移动必须设置,移动方向
screenEdgePan.edges =
UIRectEdgeLeft; //
从屏幕左边移动
// 注意:
屏幕边缘移动手势,只有当从屏幕边缘移动的时候才触发
[imageView addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
// Do any additional setup after loading the view.
}
// 7. sceenEdgePanAction:
- (void)sceenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)sep
{
if (sep.edges ==
UIRectEdgeLeft) {
NSLog(@"从屏幕左边轻扫");
}
}
// 6. swipeAction:
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
// 判断方向
if (swipe.direction ==
UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右轻扫");
[UIView
animateWithDuration:0.5
animations:^{
CGRect rec = swipe.view.frame;
swipe.view.frame =
CGRectMake(rec.origin.x +
150, rec.origin.y, rec.size.width,
rec.size.height);
}];
}
}
// 5. pinchAction:
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
// 1. 获取缩放比例
CGFloat scale = pinch.scale;
// 2. 进行缩放变换
pinch.view.transform =
CGAffineTransformScale(pinch.view.transform, scale, scale);
// 3. 缩放比例 1:1 1.0
pinch.scale =
1.0;
}
// 4. rotationAction:
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// 1. 获取旋转角度
CGFloat angle = rotation.rotation;
// 2. 变换视图
rotation.view.transform =
CGAffineTransformRotate(rotation.view.transform, angle);
// 3. 角度归 0
rotation.rotation =
0;
}
// 3. panAction:
- (void)panAction:(UIPanGestureRecognizer *)pan
{
// 获取pan所在的视图
UIView *view = pan.view;
// 获取
手势在视图上变换
CGPoint p = [pan
translationInView:view];
// 视图
跟随手指移动
// view.transform = CGAffineTransformTranslate(view.transform,p.x,p.y);
// pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// 变换方法有两个方法
// 一种是带有 make的方法CGAffineTransformMakeTranslation ,
该方法以原始状态 为基准进行变换,中途如果手势停止,下次变换以原来位置为基准。
// 一种是
不带 make的方法
// CGAffineTransformTranslate
以上一次变换为基准,需要注意的是。 以上一次变换为基准,是在每一次的变换基础上
又 加上一个值,因此每次变换结束,我们都要把变换值 “清零” ——
移动需要 置为(0,0) ,缩放需要置为1.0
比例, 旋转需要
置为 0° 度。
// 平移变换置为0
// [pan setTranslation:(CGPointZero) inView:view];
// 以初始状态为基准变换的方法
view.transform =
CGAffineTransformMakeTranslation(p.x, p.y);
}
// 2. longPress
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 手势识别器的状态
// possible 可能为某个手势状态 --》 recognized
识别状态 ——》 begin
开始状态 ——》【changed】改变状态 ——》 end
结束状态
// begin ----> cancle
取消状态
// 长按手势是持续性手势
分为长按开始 刚被识别为长按手势
// 长按结束
: 手指离开屏幕
if (longPress.state ==
UIGestureRecognizerStateBegan) {
NSLog(@"长按手势开始");
}
}
// 1. tap
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"轻拍操作");
}
// 加载数据
- (void)setData
{
// 1. plist 文件路径
NSString *filePath = [[NSBundle
mainBundle] pathForResource:@"Students"
ofType:@"plist"];
// 2. 根据
根节点创建数据对象
NSArray *rootArray = [NSArray
arrayWithContentsOfFile:filePath];
// 3. 给 studentArray
开辟空间
self.studentArray = [NSMutableArray
array];
// 4. 把
字典里面的数据存放到 student 对象里面,再把student
对象,存放到数组里面
for (NSDictionary *dic
in rootArray) {
// (1) 创建student
对象
Student *student = [[Student
alloc] init];
// (2) 给student
对象赋值
// 通过 kvc
赋值
[student setValuesForKeysWithDictionary:dic];
// 执行了多次 setValueForKey:
直到全部赋值
// 如果
数据 和 model key值不照应,执行 setValueForKey
,找不到对应的key值,就会出错。因此
数据 和 model
的key值一定要照应。
// (3) 把赋值好的student
对象添加到数组里面
[self.studentArray
addObject:student];
[student release];
// 释放
}
}
// 1. 数据
转 model
// 2. 自定义单元格
// 3. 多个单元格使用
// 4. 自适应高度
// 一、
数据转模型
// 数据直接拿来存储,比如我们的学生信息存储
在字典里面,在使用的时候,特别麻烦,要通过key值取值,并且可维护性低。没有体现面向对象的思想。
// model mvc
框架里面的 model 层(数据模型层),把学生信息,映射为 model类——Student类,体现了面向对象的面向框架的编程思想,易于维护,且存取方便。
// 所做的操作:把
字典里面的值 放入到 student
里面。
// - (void)setData;
加载数据方法
[self setData];
NSLog(@"%@",
self.studentArray);
// 二、
自定义单元格
// 当单元格需要展示的内容比较复杂时,我们通常创建一个继承自UITableViewCell
的一个子类,作为自定义单元格。
// 三、
多个单元格的混合使用
// 在开发中,有时我们会根据数据的不同,来定义不同的单元格展示数据。
// 多个单元格使用时的注意点:
// 1. 要明确根据哪个因素,选择使用不同的单元格。比如:性别。
// 2. 创建单元格时都要加上重用标识符
// 3. 都使用重用机制
// 自适应高度
// 获取字符串高度
+ (CGFloat)heightForString:(NSString *)string
{
// 1. 获取字体属性,
字号, 字体名
等等,存储到字典里面
// object: introduceLabel
使用的字体对象
// key: 字体属性 NSFontAttributeName
NSDictionary *dic =
@{NSFontAttributeName:[UIFont
systemFontOfSize:18]};
// 2. 使用字符串实例方法,绘制一个矩形,
返回值为cgrect
// size: 允许绘制的最大宽高,如果固定宽度,则高度自适应,如果固定高度,则宽度自适应。前者,超过最大高度,则停止绘制,后者,超过最大宽度,停止绘制。
// attributes:
绘制矩形时,使用的字体信息
// options: 绘制基线,通常使用原始基线。
// context: 绘制上下文。通常给nil。
CGRect bounds = [string
boundingRectWithSize:(CGSizeMake(200,
10000)) options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:dic context:nil];
// 3. 返回bounds
的高度
return bounds.size.height;
}
// 单元格高度
+ (CGFloat)cellHeightForStudent:(Student *)student
{
// 单元格总高度 =
原有控件固定高度 + 自适应高度。
CGFloat cellHeight =
170 + [BoyTableViewCell
heightForString:student.introduce];
// 如果自适应后高度 >
原本布局高度,则使用自适应高度,否则使用原本布局高度
return cellHeight >
200 ? cellHeight : 200;
}
// 1. 创建对象
UIImageView *imageView = [[UIImageView
alloc] initWithFrame:(CGRectMake(10,
100, 256,
192))];
// 打开用户交互
imageView.userInteractionEnabled =
YES;
// 2. 添加图片
// imageView.image = [UIImage imageNamed:@"avatar.jpg"]; // imageNamed
这种方式图片必须在工程中, 如果图片是
非png格式,一定要加后缀.jpg .xxx
// UIImage 对象的路径创建方法
imageView.image = [UIImage
imageWithContentsOfFile:@"/Users/wushumin/Pictures/sai专用文件夹/avatar.jpg"];
// 根据文件路径创建
// 3. 把图片视图添加到 self.view
上
[self.view
addSubview:imageView];
// 4. 释放
[imageView release];
// 手势识别器
重点:
// 手势识别器
是封装了一系列的触摸事件,来表示某一个事件
// UIView 继承自 UIResponder
,虽然可以响应用户的触摸操作,但是太麻烦。因此苹果公司
设计了一系列手势识别器方便我们调用
// 只要一个视图上面添加了手势识别器,就可以识别出该手势。
// 常用手势识别器有七种:
// 1. tap 轻拍手势
// 1. 创建
UITapGestureRecognizer *tap = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(tapAction:)];
// 2. 设置属性
比如:轻拍手势有 轻拍几次
几根手指轻拍
tap.numberOfTapsRequired =
1; //
默认为1,拍一次
tap.numberOfTouchesRequired =
1; //
默认为1,一根手指
// 3. 添加给某个视图
// [imageView addGestureRecognizer:tap];
// 4. 释放
[tap release];
// 2. longPress
长按手势
// 创建
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(longPressAction:)];
// 设置属性
最短长按时间
longPress.minimumPressDuration =
2;// 2秒
// 添加
// [imageView addGestureRecognizer:longPress];
[longPress release];
// 3. pan 平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc] initWithTarget:self
action:@selector(panAction:)];
// [imageView addGestureRecognizer:pan];
[pan release];
// 4. rotation
旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer
alloc] initWithTarget:self
action:@selector(rotationAction:)];
// [imageView addGestureRecognizer:rotation];
// 5. pinch 捏合手势
(缩放手势)
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(pinchAction:)];
// [imageView addGestureRecognizer:pinch];
[pinch release];
// 6. swipe 轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(swipeAction:)];
// 轻扫手势默认向右轻扫,
swipe.direction =
UISwipeGestureRecognizerDirectionRight;
// [imageView addGestureRecognizer:swipe];
[swipe release];
// 7. 屏幕边缘移动(轻扫)
UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer
alloc] initWithTarget:self
action:@selector(sceenEdgePanAction:)];
// 屏幕边缘移动必须设置,移动方向
screenEdgePan.edges =
UIRectEdgeLeft; //
从屏幕左边移动
// 注意:
屏幕边缘移动手势,只有当从屏幕边缘移动的时候才触发
[imageView addGestureRecognizer:screenEdgePan];
[screenEdgePan release];
// Do any additional setup after loading the view.
}
// 7. sceenEdgePanAction:
- (void)sceenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)sep
{
if (sep.edges ==
UIRectEdgeLeft) {
NSLog(@"从屏幕左边轻扫");
}
}
// 6. swipeAction:
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
// 判断方向
if (swipe.direction ==
UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右轻扫");
[UIView
animateWithDuration:0.5
animations:^{
CGRect rec = swipe.view.frame;
swipe.view.frame =
CGRectMake(rec.origin.x +
150, rec.origin.y, rec.size.width,
rec.size.height);
}];
}
}
// 5. pinchAction:
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
// 1. 获取缩放比例
CGFloat scale = pinch.scale;
// 2. 进行缩放变换
pinch.view.transform =
CGAffineTransformScale(pinch.view.transform, scale, scale);
// 3. 缩放比例 1:1 1.0
pinch.scale =
1.0;
}
// 4. rotationAction:
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// 1. 获取旋转角度
CGFloat angle = rotation.rotation;
// 2. 变换视图
rotation.view.transform =
CGAffineTransformRotate(rotation.view.transform, angle);
// 3. 角度归 0
rotation.rotation =
0;
}
// 3. panAction:
- (void)panAction:(UIPanGestureRecognizer *)pan
{
// 获取pan所在的视图
UIView *view = pan.view;
// 获取
手势在视图上变换
CGPoint p = [pan
translationInView:view];
// 视图
跟随手指移动
// view.transform = CGAffineTransformTranslate(view.transform,p.x,p.y);
// pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// 变换方法有两个方法
// 一种是带有 make的方法CGAffineTransformMakeTranslation ,
该方法以原始状态 为基准进行变换,中途如果手势停止,下次变换以原来位置为基准。
// 一种是
不带 make的方法
// CGAffineTransformTranslate
以上一次变换为基准,需要注意的是。 以上一次变换为基准,是在每一次的变换基础上
又 加上一个值,因此每次变换结束,我们都要把变换值 “清零” ——
移动需要 置为(0,0) ,缩放需要置为1.0
比例, 旋转需要
置为 0° 度。
// 平移变换置为0
// [pan setTranslation:(CGPointZero) inView:view];
// 以初始状态为基准变换的方法
view.transform =
CGAffineTransformMakeTranslation(p.x, p.y);
}
// 2. longPress
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 手势识别器的状态
// possible 可能为某个手势状态 --》 recognized
识别状态 ——》 begin
开始状态 ——》【changed】改变状态 ——》 end
结束状态
// begin ----> cancle
取消状态
// 长按手势是持续性手势
分为长按开始 刚被识别为长按手势
// 长按结束
: 手指离开屏幕
if (longPress.state ==
UIGestureRecognizerStateBegan) {
NSLog(@"长按手势开始");
}
}
// 1. tap
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"轻拍操作");
}
// 加载数据
- (void)setData
{
// 1. plist 文件路径
NSString *filePath = [[NSBundle
mainBundle] pathForResource:@"Students"
ofType:@"plist"];
// 2. 根据
根节点创建数据对象
NSArray *rootArray = [NSArray
arrayWithContentsOfFile:filePath];
// 3. 给 studentArray
开辟空间
self.studentArray = [NSMutableArray
array];
// 4. 把
字典里面的数据存放到 student 对象里面,再把student
对象,存放到数组里面
for (NSDictionary *dic
in rootArray) {
// (1) 创建student
对象
Student *student = [[Student
alloc] init];
// (2) 给student
对象赋值
// 通过 kvc
赋值
[student setValuesForKeysWithDictionary:dic];
// 执行了多次 setValueForKey:
直到全部赋值
// 如果
数据 和 model key值不照应,执行 setValueForKey
,找不到对应的key值,就会出错。因此
数据 和 model
的key值一定要照应。
// (3) 把赋值好的student
对象添加到数组里面
[self.studentArray
addObject:student];
[student release];
// 释放
}
}
// 1. 数据
转 model
// 2. 自定义单元格
// 3. 多个单元格使用
// 4. 自适应高度
// 一、
数据转模型
// 数据直接拿来存储,比如我们的学生信息存储
在字典里面,在使用的时候,特别麻烦,要通过key值取值,并且可维护性低。没有体现面向对象的思想。
// model mvc
框架里面的 model 层(数据模型层),把学生信息,映射为 model类——Student类,体现了面向对象的面向框架的编程思想,易于维护,且存取方便。
// 所做的操作:把
字典里面的值 放入到 student
里面。
// - (void)setData;
加载数据方法
[self setData];
NSLog(@"%@",
self.studentArray);
// 二、
自定义单元格
// 当单元格需要展示的内容比较复杂时,我们通常创建一个继承自UITableViewCell
的一个子类,作为自定义单元格。
// 三、
多个单元格的混合使用
// 在开发中,有时我们会根据数据的不同,来定义不同的单元格展示数据。
// 多个单元格使用时的注意点:
// 1. 要明确根据哪个因素,选择使用不同的单元格。比如:性别。
// 2. 创建单元格时都要加上重用标识符
// 3. 都使用重用机制
// 自适应高度
// 获取字符串高度
+ (CGFloat)heightForString:(NSString *)string
{
// 1. 获取字体属性,
字号, 字体名
等等,存储到字典里面
// object: introduceLabel
使用的字体对象
// key: 字体属性 NSFontAttributeName
NSDictionary *dic =
@{NSFontAttributeName:[UIFont
systemFontOfSize:18]};
// 2. 使用字符串实例方法,绘制一个矩形,
返回值为cgrect
// size: 允许绘制的最大宽高,如果固定宽度,则高度自适应,如果固定高度,则宽度自适应。前者,超过最大高度,则停止绘制,后者,超过最大宽度,停止绘制。
// attributes:
绘制矩形时,使用的字体信息
// options: 绘制基线,通常使用原始基线。
// context: 绘制上下文。通常给nil。
CGRect bounds = [string
boundingRectWithSize:(CGSizeMake(200,
10000)) options:(NSStringDrawingUsesLineFragmentOrigin)
attributes:dic context:nil];
// 3. 返回bounds
的高度
return bounds.size.height;
}
// 单元格高度
+ (CGFloat)cellHeightForStudent:(Student *)student
{
// 单元格总高度 =
原有控件固定高度 + 自适应高度。
CGFloat cellHeight =
170 + [BoyTableViewCell
heightForString:student.introduce];
// 如果自适应后高度 >
原本布局高度,则使用自适应高度,否则使用原本布局高度
return cellHeight >
200 ? cellHeight : 200;
}
相关文章推荐
- iOS Category
- iOS开发之xib技巧介绍
- iOS block基础
- 项目总结4 类似网易云音乐导航栏指示器与Scrollview互动(个性推荐、歌单等)的简单实现(二)
- 【iOS】MD5(加密)/AES/Base64加密和解密
- iOS应用如何支持IPV6-b
- iOS 通知中心扩展制作初步-b
- iOS 数据持久化方式 - 归档 反归档
- iOS中 block 代理 通知的区别
- DM365 spi_norflash无操作系统启动和sys_bios启动(二)
- button点击无反应
- 十三.加速度传感器
- 十二.定时器
- 十.媒体播放
- 九.忙碌指示器
- iOS文件操作
- 六.快速拨打电话接口
- 五.旋转
- 四.文件系统
- 三.容器