05-常用UI控件之 UIImageView
来源:互联网 发布:mysql count 百万 编辑:程序博客网 时间:2024/05/12 08:58
UIImageView,顾名思义,是用来放置图片的。使用Interface Builder设计界面时,当然可以直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码。
1、创建一个UIImageView:
创建一个UIImageView对象有五种方法:
比较常用的是前边三个。至于第四个,当这个ImageView的highlighted属性是YES时,显示的就是参数highlightedImage,一般情况下显示的是第一个参数UIImage。
2、frame与bounds属性:
上述创建一个UIImageView的方法中,第二个方法是在创建时就设定位置和大小。
当之后想改变位置时,可以重新设定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);
那么这个属性跟frame有什么区别呢?
我的理解是,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)。
3、contentMode属性:
这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有以下几个常量可供设定:
UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight
UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill
4、更改位置
更改一个UIImageView的位置,可以
4.1 直接修改其frame属性
4.2 修改其center属性:
imageView.center = CGPointMake(CGFloat x, CGFloat y);
center属性指的就是这个ImageView的中间点。
4.3 使用transform属性
imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。
5、旋转图像
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
要注意它是按照顺时针方向旋转的,而且旋转中心是原始ImageView的中心,也就是center属性表示的位置。
这个方法的参数angle的单位是弧度,而不是我们最常用的度数,所以可以写一个宏定义:
#define degreesToRadians(x) (M_PI*(x)/180.0)
6、缩放图像
还是使用transform属性:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
7、播放一系列图片
8、为图片添加单击事件:
一定要先将userInteractionEnabled置为YES,这样才能响应单击事件。
9、其他设置
_imageDate= [NSMutableArrayarrayWithCapacity:21];
//3.创建对象
UIImageView*image = [[UIImageViewalloc]initWithFrame:CGRectMake(20,20, [UIScreenmainScreen].bounds.size.width,200)];
[self.viewaddSubview:image];
//4.循环拿到图片并存入数组
for (int i = 1; i <=21; i++) {
NSString*str = [NSStringstringWithFormat:@"%d.jpg", i];
UIImage*im = [UIImageimageNamed:str];
[_imageDateaddObject:im];
}
//4.设置动画数组时常
image.animationImages= _imageDate;
image.animationDuration= 1.5;
[image startAnimating];
UIImage *image2 = [image stretchableImageWithLeftCapWidth:5topCapHeight:0];
imageView.image = [UIImage imageNamed:@"image.png"];
[imageView setContentStretch:CGRectMake(150.0/300.0,75.0/150.0,10.0/300.0,10.0/150.0)];
- 05-常用UI控件之 UIImageView
- UI 一一 常用控件-UILabel,UIImageView
- IOS开发UI系列之常用控件 UILabel, UITextField, UIButton, UIImageView等常用属性与方法
- iOS UI基础控件之UIImageView
- UI控件笔记(二):UI之UILabel和UIImageView
- UI控件 - UIImageView
- IOS UI控件 - UIImageView
- UI控件--UIImageView
- ios-UI控件精讲之【6】-UIImageView
- ui控件之UIImageView(imageView的动画效果)
- UI 常用方法总结之--- UIImageView UISlider UISegmentedControl (不断更新中)
- UI连载五--常用UI控件—Label、Button、UITextField、UIImageView、UISlider、UISwitch、UIActivityIndicatorView等
- UI常用控件之UISegmentControl
- iOS开发之六:常用控件--UIImageView的使用
- UI之UILabel和UIImageView
- UI基础2控件:UILabel,UIImageView
- UI-基本控件( UILabel UITextField UIImageView UIButton)
- iOS基础——UI控件之UIView、UITextField、UILabel、UIColor、UIButton、UIImageView
- 04-常用UI控件之 UIlable UIButton UITextFiled
- 看《士兵突击》有感~~
- 2015年7月9日 UITableView
- 红黑树
- android 返回键两次退出
- 05-常用UI控件之 UIImageView
- iOS8旋转屏
- linux命令行操作之文件操作总结
- 机器学习技法总结(四)(aggregation,vote,bootstrap...)
- Linux C语言程序设计(四)——枚举与结构体
- 06-常用UI控件之 UISlider滑块
- csharp: DataTable Rename ColumnName and remove Column
- csharp: DataTable Rename ColumnName and remove Column
- 利用正则表达式排除特定字符串