UIImageView,顧名思義,是用來放置圖片的。使用Interface Builder設(shè)計(jì)界面時(shí),當(dāng)然可以直接將控件拖進(jìn)去并設(shè)置相關(guān)屬性,這就不說了,這里講的是用代碼。
1、創(chuàng)建一個(gè)UIImageView:
創(chuàng)建一個(gè)UIImageView對象有五種方法:
UIImageView *imageView1 = [[UIImageView alloc] init];UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)];UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)];UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *) highlightedImage:(UIImage *)];UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)];
比較常用的是前邊三個(gè)。至于第四個(gè),當(dāng)這個(gè)ImageView的highlighted屬性是YES時(shí),顯示的就是參數(shù)highlightedImage,一般情況下顯示的是第一個(gè)參數(shù)UIImage。
2、frame與bounds屬性:
上述創(chuàng)建一個(gè)UIImageView的方法中,第二個(gè)方法是在創(chuàng)建時(shí)就設(shè)定位置和大小。
當(dāng)之后想改變位置時(shí),可以重新設(shè)定frame屬性:
imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
注意到UIImageView還有一個(gè)bounds屬性
imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat heigth);
那么這個(gè)屬性跟frame有什么區(qū)別呢?
我的理解是,frame設(shè)置其位置和大小,而bounds只能設(shè)置其大小,其參數(shù)中的x、y不起作用即便是之前沒有設(shè)定frame屬性,控件最終的位置也不是bounds所設(shè)定的參數(shù)。bounds實(shí)現(xiàn)的是將UIImageView控件以原來的中心為中心進(jìn)行縮放。例如有如下代碼:
imageView.frame = CGRectMake(0, 0, 320, 460);imageView.bounds = CGRectMake(100, 100, 160, 230);
執(zhí)行之后,這個(gè)imageView的位置和大小是(80, 115, 160, 230)。
3、contentMode屬性:
這個(gè)屬性是用來設(shè)置圖片的顯示方式,如居中、居右,是否縮放等,有以下幾個(gè)常量可供設(shè)定:
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
注意以上幾個(gè)常量,凡是沒有帶Scale的,當(dāng)圖片尺寸超過 ImageView尺寸時(shí),只有部分顯示在ImageView中。UIViewContentModeScaleToFill屬性會(huì)導(dǎo)致圖片變形。UIViewContentModeScaleAspectFit會(huì)保證圖片比例不變,而且全部顯示在ImageView中,這意味著ImageView會(huì)有部分空白。UIViewContentModeScaleAspectFill也會(huì)證圖片比例不變,但是是填充整個(gè)ImageView的,可能只有部分圖片顯示出來。 前三個(gè)效果如下圖:

UIViewContentModeScaleToFill

UIViewContentModeScaleAspectFit

UIViewContentModeScaleAspectFill
4、更改位置:
更改一個(gè)UIImageView的位置,可以
4.1 直接修改其frame屬性
4.2 修改其center屬性:
imageView.center = CGPointMake(CGFloat x, CGFloat y);
center屬性指的就是這個(gè)ImageView的中間點(diǎn)。
4.3 使用transform屬性
imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
其中dx與dy表示想要往x或者y方向移動(dòng)多少,而不是移動(dòng)到多少。
5、旋轉(zhuǎn)圖像:
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
要注意它是按照順時(shí)針方向旋轉(zhuǎn)的,而且旋轉(zhuǎn)中心是原始ImageView的中心,也就是center屬性表示的位置。
這個(gè)方法的參數(shù)angle的單位是弧度,而不是我們最常用的度數(shù),所以可以寫一個(gè)宏定義:
#define degreesToRadians(x) (M_PI*(x)/180.0)
用于將度數(shù)轉(zhuǎn)化成弧度。下圖是旋轉(zhuǎn)45度的情況:

正常顯示

旋轉(zhuǎn)45°
6、縮放圖像:
還是使用transform屬性:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
其中,CGFloat scale_w與CGFloat scale_h分別表示將原來的寬度和高度縮放到多少倍,下圖是縮放到原來的0.6倍的示意圖:

正常顯示

縮放0.6倍
7、播放一系列圖片:
imageView.animationImages = imagesArray;
// 設(shè)定所有的圖片在多少秒內(nèi)播放完畢
imageView.animationDuration = [imagesArray count];
// 不重復(fù)播放多少遍,0表示無數(shù)遍
imageView.animationRepeatCount = 0;
// 開始播放
[imageView startAnimating];
其中,imagesArray是一些列圖片的數(shù)組。如下圖:

播放圖片1

播放圖片2

播放圖片3
8、為圖片添加單擊事件:
imageView.userInteractionEnabled = YES;UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];[imageView addGestureRecognizer:singleTap];
一定要先將userInteractionEnabled置為YES,這樣才能響應(yīng)單擊事件。
9、其他設(shè)置:
imageView.hidden = YES或者NO; // 隱藏或者顯示圖片
imageView.alpha = (CGFloat) al; // 設(shè)置透明度
imageView.highlightedImage = (UIImage *)hightlightedImage; // 設(shè)置高亮?xí)r顯示的圖片
imageView.image = (UIImage *)image; // 設(shè)置正常顯示的圖片
[imageView sizeToFit]; // 將圖片尺寸調(diào)整為與內(nèi)容圖片相同