1.contentMode属性spa
这个属性是用来设置图片的显示方式,如居中、居右,是否缩放等,有如下几个常量可供设定:orm
UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIViewContentModeRedraw UIViewContentModeCenter UIViewContentModeTop UIViewContentModeBottom UIViewContentModeLeft UIViewContentModeRight UIViewContentModeTopLeft UIViewContentModeTopRight UIViewContentModeBottomLeft UIViewContentModeBottomRight
注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会致使图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,并且所有显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,可是是填充整个ImageView的,可能只有部分图片显示出来。
2.更改位置事件
更改一个UIImageView的位置,能够图片
2.1 直接修改其frame属性get
2.2 修改其center属性:it
imageView.center = CGPointMake(CGFloat x, CGFloat y);
center属性指的就是这个ImageView的中间点。io
2.3 使用transform属性form
imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。class
三、旋转图像transform
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
要注意它是按照顺时针方向旋转的,并且旋转中心是原始ImageView的中心,也就是center属性表示的位置。
这个方法的参数angle的单位是弧度,而不是咱们最经常使用的度数,因此能够写一个宏定义:
#define degreesToRadians(x) (M_PI*(x)/180.0)
四、缩放图像
仍是使用transform属性:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
五、为图片添加单击事件:
imageView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)]; [imageView addGestureRecognizer:singleTap];
必定要先将userInteractionEnabled置为YES,这样才能响应单击事件
6.其余设置
imageView.hidden = YES或者NO; // 隐藏或者显示图片
imageView.alpha = (CGFloat) al; // 设置透明度
imageView.highlightedImage = (UIImage *)hightlightedImage; // 设置高亮时显示的图片
imageView.image = (UIImage *)image; // 设置正常显示的图片
[imageView sizeToFit]; // 将图片尺寸调整为与内容图片相同