iOS - 影像檢視



影像檢視使用

影像檢視用於顯示單個影像或影像的動畫序列。

重要屬性

  • 影像
  • 高亮影像
  • userInteractionEnabled
  • 動畫影像
  • 動畫重複計數

重要方法

- (id)initWithImage:(UIImage *)image
- (id)initWithImage:(UIImage *)image highlightedImage: (UIImage *)highlightedImage
- (void)startAnimating
- (void)stopAnimating

新增自定義方法 addImageView

-(void)addImageView {
   UIImageView *imgview = [[UIImageView alloc]
   initWithFrame:CGRectMake(10, 10, 300, 400)];
   [imgview setImage:[UIImage imageNamed:@"AppleUSA1.jpg"]];
   [imgview setContentMode:UIViewContentModeScaleAspectFit];
   [self.view addSubview:imgview];
}

新增另一個自定義方法 addImageViewWithAnimation

此方法說明如何在影像檢視中設定影像動畫。

-(void)addImageViewWithAnimation {
   UIImageView *imgview = [[UIImageView alloc]
   initWithFrame:CGRectMake(10, 10, 300, 400)];
   
   // set an animation
   imgview.animationImages = [NSArray arrayWithObjects:
   [UIImage imageNamed:@"AppleUSA1.jpg"],
   [UIImage imageNamed:@"AppleUSA2.jpg"], nil];
   imgview.animationDuration = 4.0;
   imgview.contentMode = UIViewContentModeCenter;
   [imgview startAnimating];
   [self.view addSubview:imgview];
}

注意

我們必須將命名為“AppleUSA1.jpg”和“AppleUSA2.jpg”的影像新增到我們的專案中,這可以透過將影像拖到導航區域(我們專案的列出的檔案)中來完成。

在 ViewController.m 中更新 viewDidLoad 如下 −

(void)viewDidLoad {
   [super viewDidLoad];
   [self addImageView];
}

輸出

當我們執行該應用程式時,我們將獲得以下輸出 −

iOS Tutorial

你可以嘗試呼叫 addImageViewWithAnimation 方法,而不是 addImageView 方法,來看看影像檢視的動畫效果。

ios_ui_elements.htm
廣告
© . All rights reserved.