UIKit 手写控件 UIImageView 和UITableView

UIImageView 是用来放置图片的性能

建立⼀一个UIImageView对象有五种⽅方法:
1.UIImageView *imageView1 = [[UIImageView alloc] init]; 实例化了一个UIImageView类型的对象 
2. UIImageView *imageView2 = [[UIImageView alloc] initWithFrame: (CGRect)]; 实例化了一个UIImageView类型的对象同时设置了图片的位置
3.UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)];实例化了一个UIImageView类型的对象同时设置了图片是哪个图片对象spa

不经常使用
1. UIImageView *imageView4 = [[UIImageView alloc] initWithImage: (UIImage *) highlightedImage:(UIImage *)];当这个ImageView的highlighted高亮的属性是 YES时,显示的就是参数highlightedImage高亮的,通常状况下显示的是第一个参数UIImage 
2. UIImageView *imageView5 = [[UIImageView alloc] initWithCoder: (NSCoder *)];代理

当以后想改变位置时,能够从新设定frame属性:
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
注意到UIImageView还有一个bounds属性:
imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);orm

frame设置其位置和大小,而bounds只能设置其大小,其参数中的x、y不起做用即使是以前没有设定frame属性,控件最终的位置也不是bounds所设定的参数。bounds实现的是将UIImageView控件以原来的中心为中心进行缩放。
例如 以下代码:
imageView.frame = CGRectMake(0, 0, 320, 460); imageView.bounds =
CGRectMake(100, 100, 160, 230); 执行以后,这个imageView的位置和大小是(80, 115, 160, 230)。对象

contentMode属性:
这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有如下⼏个常量可供设定:
UIViewContentModeScaleToFill 
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill 
UIViewContentModeRedraw 
UIViewContentModeCenter 
UIViewContentModeTop 
UIViewContentModeBottom
UIViewContentModeLeft 
UIViewContentModeRight 
UIViewContentModeTopLeft
UIViewContentModeTopRight 
UIViewContentModeBottomLeft 
UIViewContentModeBottomRight继承

更改位置:
更改一个UIImageView的位置,能够直接修改其frame属性
修改其center属性:
imageView.center = CGPointMake(CGFloat x, CGFloat y); center属性指的就是这个ImageView的中间点。
使用transform属性
imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy); 其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。事件

旋转图像:
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
要注意它是按照顺时针方向旋转的,⽽且旋转中心是原始ImageView的中心,
也就是center属性表⽰示的位置。
这个⽅法的参数angle的单位是弧度,而不是咱们最经常使用的度数,因此能够写一
个宏定义:
#define degreesToRadians(x) (M_PI*(x)/180.0) 其中x写要旋转的角度图片

缩放图像:
仍是使用transform属性:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
其中,CGFloat scale_w与CGFloat scale_h分别表⽰将原来的宽度和高度缩放到多少倍get

播放⼀系列图片:
imageView.animationImages = imagesArray; // 设定全部的图片在多少秒内播放完毕 imageView.animationDuration = [imagesArray count]; // 不重复播放多少遍,0表示无数遍 imageView.animationRepeatCount = 0; // 开始播放 [imageView startAnimating];animation

为图片添加单击事件:
imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:singleTap]; 必定要先将userInteractionEnabled置为YES,这样才能响应单击事件。

其余设置:
imageView.hidden = YES或者NO; // 隐藏或者显示图片 imageView.alpha = (CGFloat) al; // 设置透明度 imageView.highlightedImage = (UIImage *)hightlightedImage; // 设置⾼亮时显示的图片 imageView.image = (UIImage *)image; // 设置正常显示的图片 [imageView sizeToFit]; // 将图⽚尺寸调整为与内容图片相同

UIButton和UIImageView的区别:
1> UIImageView只能一种图片(图片默认会填充整个UIImageView) image\setImage: 
2> UIButton能显示2种图片
背景(背景会填充整个UIButton) setBackroungImage:forState:
前置(覆盖在背景上面的图片,按照以前的尺寸显示) setImage:forState: * 还能显⽰文字

1> UIImageView默认是不能响应点击事件
2> UIButton能响应点击事件 : addTarget:action:forControlEvents:

1> UIImageView : 只显⽰图片,不监听点击,点击了图片后不作任何反应 
2> UIButton : 既显示图片,又监听点击,点击了图片后作一些其余事情

1> UIButton之因此能添加监听器来监听事件,是由于它继承自UIControl
2> UIImagevIew之因此不能添加监听器来监听事件,是由于它直接继承自UIView

UITableView

几乎大多数的IOS项目中均可以看获得UITableView的影子,UITableView是 iPhone中比较经常使用的,⽤的比较多的控件。主要是用于展现一系列表的数据。
特色:显示大型内容的列表,单行,多列,垂直滚动,没有水平滚动,大量的数据集,性能强大。
UITableView有两个默认的内置风格 一个是简明风格 UITableViewStylePlain。 另外一个是组团风格UITableViewStyleGrouped

UITableView有两个代理协议

Protocol UITableViewDataSource:⽤来给TableView提供数据
什么是data source:数据的来源。⼀般状况下咱们会设置拥有 UITableView的这个UIViewController为他的data source。
Protocal UITableViewDelegate:控制TableView的展现方式以及事件响应

协议实现:

//设置分区高度-(NSString*)tableView:(UITableView*)tableView:titleForHeaderInSection: (NSInteger)section

//改变行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath

//行缩进
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath: (NSIndexPath *)indexPath 

相关文章
相关标签/搜索