iOS開發:UIImageView常用操作

來源:互聯網
上載者:User

標籤:des   http   使用   os   width   art   

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屬性:

這個屬性是用來設定圖片的顯示方式,如置中、居右,是否縮放等,有以下幾個常量可供設定:

UIViewContentModeScaleToFillUIViewContentModeScaleAspectFitUIViewContentModeScaleAspectFillUIViewContentModeRedrawUIViewContentModeCenterUIViewContentModeTopUIViewContentModeBottomUIViewContentModeLeftUIViewContentModeRightUIViewContentModeTopLeftUIViewContentModeTopRightUIViewContentModeBottomLeftUIViewContentModeBottomRight

 

 

注意以上幾個常量,凡是沒有帶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];    // 將圖片尺寸調整為與內容圖片相同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.