UIImageView,顧名思義,是用來放置圖片的。使用Interface Builder設計界面時,固然能夠直接將控件拖進去並設置相關屬性,這就不說了,這裏講的是用代碼。數組
一、建立一個UIImageView:ui
建立一個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 *)];
比較經常使用的是前邊三個。至於第四個,當這個ImageView的highlighted屬性是YES時,顯示的就是參數highlightedImage,通常狀況下顯示的是第一個參數UIImage。code
二、frame與bounds屬性:orm
上述建立一個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有什麼區別呢?get
個人理解是,frame設置其位置和大小,而bounds只能設置其大小,其參數中的x、y不起做用即使是以前沒有設定frame屬性,控件最終的位置也不是bounds所設定的參數。bounds實現的是將UIImageView控件以原來的中心爲中心進行縮放。例若有以下代碼:animation
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
注意以上幾個常量,凡是沒有帶Scale的,當圖片尺寸超過 ImageView尺寸時,只有部分顯示在ImageView中。UIViewContentModeScaleToFill屬性會致使圖片變形。UIViewContentModeScaleAspectFit會保證圖片比例不變,並且所有顯示在ImageView中,這意味着ImageView會有部分空白。UIViewContentModeScaleAspectFill也會證圖片比例不變,可是是填充整個ImageView的,可能只有部分圖片顯示出來。
前三個效果以下圖:
UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill
四、更改位置
更改一個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方向移動多少,而不是移動到多少。
五、旋轉圖像
imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);
要注意它是按照順時針方向旋轉的,並且旋轉中心是原始ImageView的中心,也就是center屬性表示的位置。
這個方法的參數angle的單位是弧度,而不是咱們最經常使用的度數,因此能夠寫一個宏定義:
#define degreesToRadians(x) (M_PI*(x)/180.0)
用於將度數轉化成弧度。下圖是旋轉45度的狀況:
六、縮放圖像
仍是使用transform屬性:
imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
其中,CGFloat scale_w與CGFloat scale_h分別表示將原來的寬度和高度縮放到多少倍,下圖是縮放到原來的0.6倍的示意圖:
七、播放一系列圖片
imageView.animationImages = imagesArray; // 設定全部的圖片在多少秒內播放完畢 imageView.animationDuration = [imagesArray count]; // 不重複播放多少遍,0表示無數遍 imageView.animationRepeatCount = 0; // 開始播放 [imageView startAnimating];
其中,imagesArray是一些列圖片的數組。以下圖:
八、爲圖片添加單擊事件:
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]; // 將圖片尺寸調整爲與內容圖片相同