iOS開發26:UIImageView常用操作

UIImageView,顧名思義,是用來放置圖片的。使用Interface Builder設計界面時,當然可以直接將控件拖進去並設置相關屬性,這就不說了,這裏講的是用代碼。

1、創建一個UIImageView:

創建一個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。

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
注意以上幾個常量,凡是沒有帶Scale的,當圖片尺寸超過 ImageView尺寸時,只有部分顯示在ImageView中。UIViewContentModeScaleToFill屬性會導致圖片變形。UIViewContentModeScaleAspectFit會保證圖片比例不變,而且全部顯示在ImageView中,這意味着ImageView會有部分空白。UIViewContentModeScaleAspectFill也會證圖片比例不變,但是是填充整個ImageView的,可能只有部分圖片顯示出來。

前三個效果如下圖:

      

   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)
用於將度數轉化成弧度。下圖是旋轉45度的情況:
   

6、縮放圖像

還是使用transform屬性:

imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);
其中,CGFloat scale_w與CGFloat scale_h分別表示將原來的寬度和高度縮放到多少倍,下圖是縮放到原來的0.6倍的示意圖:

   

7、播放一系列圖片

imageView.animationImages = imagesArray;
// 設定所有的圖片在多少秒內播放完畢
imageView.animationDuration = [imagesArray count];
// 不重複播放多少遍,0表示無數遍
imageView.animationRepeatCount = 0;
// 開始播放
[imageView startAnimating];
其中,imagesArray是一些列圖片的數組。如下圖:
      

8、爲圖片添加單擊事件:

imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:singleTap];
一定要先將userInteractionEnabled置爲YES,這樣才能響應單擊事件。

9、其他設置

imageView.hidden = YES或者NO;    // 隱藏或者顯示圖片
imageView.alpha = (CGFloat) al;    // 設置透明度
imageView.highlightedImage = (UIImage *)hightlightedImage;     // 設置高亮時顯示的圖片
imageView.image = (UIImage *)image;    // 設置正常顯示的圖片
[imageView sizeToFit];    // 將圖片尺寸調整爲與內容圖片相同
 iOS開發26:UIImageView常用操作
更多0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章