您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

UIImageView 常用操作全整理:建立、縮放、形變與動畫

編輯:iOS 開發綜合

UIImageView 顧名思義就是拿來放圖片的。用 Interface Builder 拉界面時,拖進來點幾下屬性面板就好,沒什麼可說的。真正值得整理的是純程式碼操作的部分——因為一旦你開始自訂控件、做動畫、寫可複用的元件,這些 API 就得熟到不用查。

1. 五種建立方式

UIImageView *imageView1 = [[UIImageView alloc] init];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:(CGRect)frame];
UIImageView *imageView3 = [[UIImageView alloc] initWithImage:(UIImage *)image];
UIImageView *imageView4 = [[UIImageView alloc] initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage];
UIImageView *imageView5 = [[UIImageView alloc] initWithCoder:(NSCoder *)coder];

日常最常用的是前三種。第四種有點意思:當 highlighted 屬性為 YES 時顯示 highlightedImage,否則顯示第一個 image——預設高亮狀態琢磨的是「按下時換圖」這種效果。第五種是走 Storyboard/xib 反序列化時系統內部呼叫的,自己寫通常用不到。

2. frame 與 bounds 的差別

第二個初始化方法可以一步到位設定位置與大小;之後想調整,重新賦值 frame 即可:

imageView.frame = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

但 UIImageView 還有一個 bounds:

imageView.bounds = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);

兩者有什麼不同?我的理解是:frame 同時管位置和大小,bounds 只管子大小,參數裡的 x、y 實際上不起作用——即使你從沒設過 frame,控件的最終位置也不會跑到 bounds 指定的座標去。bounds 做的是「以原本的中心為中心進行縮放」。

看段程式碼就很直觀:

imageView.frame = CGRectMake(0, 0, 320, 460);
imageView.bounds = CGRectMake(100, 100, 160, 230);

執行完之後,這個 imageView 的位置與大小是 (80, 115, 160, 230)——中心點沒動,尺寸縮了一半,x/y 被忽略。這也是為什麼做「原地放大縮小」的動畫時,改 bounds.size 比改 frame.size 更省事。

3. contentMode:決定圖片怎麼填進框裡

這個屬性控制圖片的對齊與縮放策略,可用的常數不少:

UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFit
UIViewContentModeScaleAspectFill
UIViewContentModeRedraw
UIViewContentModeCenter
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight

記法很簡單:凡是沒有帶 Scale 的,就是「不縮放」,圖片尺寸超過 ImageView 時只有一部分看得到,靠 Center、Top、Bottom 這些來決定露出哪一塊。

帶 Scale 的三個則是實戰常客:

  • ScaleToFill:硬撐滿整個框,會變形。除非你确定圖片比例剛好吻合,否則不建議。
  • ScaleAspectFit:比例不變,完整顯示,代價是 ImageView 內會留白。這也是 UIImageView 的預設值。
  • ScaleAspectFill:比例不變,鋪滿整個框,代價是超出部分會被裁掉。做頭像、封面圖時最常用。

前三種的效果對比如下:

依序為 ScaleToFill、ScaleAspectFit、ScaleAspectFill。

4. 移動位置的三種寫法

4.1 直接改 frame——最直白的做法,位置和大小一起設。

4.2 改 center:

imageView.center = CGPointMake(CGFloat x, CGFloat y);

顧名思義,設的是這個 ImageView 的中心點座標。當你只想挪位置、不想碰尺寸時,語意比 frame 清楚。

4.3 用 transform 做位移:

imageView.transform = CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);

這裡的 dx、dy 是「往 x/y 方向移動多少」,不是「移動到哪裡」——相對位移,別搞錯。

5. 旋轉圖片

imageView.transform = CGAffineTransformMakeRotation(CGFloat angle);

兩個要點:它是順時針旋轉,而且旋轉中心是原本的 center,也就是圍繞視圖中心轉。

參數 angle 的單位是弧度而不是我們習慣的度數,實作時通常會寫個巨集來換算:

#define degreesToRadians(x) (M_PI * (x) / 180.0)

下面分別是原圖與旋轉 45 度的樣子:

6. 縮放圖片

同樣靠 transform:

imageView.transform = CGAffineTransformMakeScale(CGFloat scale_w, CGFloat scale_h);

兩個參數分別是寬與高的縮放倍數。下圖是縮到原本 0.6 倍的對比:

小提醒:CGAffineTransformMakeScale 是「基於初始狀態」建立形變,會直接覆蓋原本的 transform;如果想在既有基礎上疊加,要改用 CGAffineTransformScale(existing, w, h)。

7. 播放一串圖片(frame animation)

imageView.animationImages = imagesArray;      // 要播放的圖片陣列
imageView.animationDuration = [imagesArray count];  // 幾秒內播完
imageView.animationRepeatCount = 0;           // 重複次數,0 代表無限次
[imageView startAnimating];

imagesArray 是一組 UIImage。這套 API 做 loading 動畫、簡簡單單的逐格動畫很夠用;記得不用時呼叫 stopAnimating,isAnimating 則可用來查詢目前狀態。

8. 給圖片掛上單擊事件

imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
[imageView addGestureRecognizer:singleTap];

關鍵在於 userInteractionEnabled 必須先設為 YES。這一步漏掉,手勢怎麼掛都不會有反應——這是新手最常踩的坑之一,因為 UILabel 和 UIImageView 預設都是關閉使用者互動的。

9. tag:用標籤找回子視圖

一個視圖通常只有一個父視圖、可以有很多子視圖。想從深層結構裡取出特定子視圖時,tag 是最省事的辦法:

UIView *view = [self.view viewWithTag:100];

提示一個容易忽略的細節:在 xib 裡設定 tag 時,不要填 0。因為 xib 中所有物件的 tag 預設值就是 0,填 0 等於讓所有物件都長得一樣,viewWithTag: 自然取不到你要的那一個。

10. 在 ImageView 裡塞一個按鈕

先釐清一個常見誤解:Button 內部可以放多張圖(普通、高亮、禁用等狀態各一張),而 ImageView 一次只能顯示一張圖。但 UIView 本身就是容器,所有 UI 控件都繼承自它,所以「往 ImageView 裡加子控件」在技術上完全可行——只是要在 Storyboard 上拖,會發現拖不進去。這時就換成手寫程式碼。

- (void)viewDidLoad {
    [super viewDidLoad];

    // UIImageView 預設不允許使用者互動,必須手動打開
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 100, 100)];
    imageView.backgroundColor = [UIColor redColor];
    imageView.userInteractionEnabled = YES;
    [self.view addSubview:imageView];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [imageView addSubview:btn];

    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}

- (void)click {
    NSLog(@"摸我了");
}

效果如下(加上「+」按鈕後點擊):

整段程式碼的重點只有一行:imageView.userInteractionEnabled = YES;。預設情況下它是關閉的,可以在介面產生器右側的屬性面板看到這個選項:

順帶一提:這個「預設關閉互動」的設計其實很合理。滾動視圖裡放滿幾百張圖片,如果每張都參與命中測試,效能負擔會很可觀。

11. 其他零散但常用的設定

imageView.hidden = YES;              // 隱藏/顯示
imageView.alpha = 0.5;               // 透明度
imageView.highlightedImage = image;  // 高亮狀態顯示的圖
imageView.image = image;             // 一般狀態顯示的圖
[imageView sizeToFit];               // 讓尺寸貼合圖片實際大小

以上就是 UIImageView 開發中最常翻回來查的一批操作。把 frame/bounds/center 三者關係和 contentMode 的三個 Scale 選項記牢,再加上 transform 的兩大類 API,日常的圖片顯示需求基本都能應付。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved