iOS - Image View(圖像視圖)

使用圖像視圖

圖像視圖,用於顯示一個單一的圖像或動畫的圖像序列。

 

重要的屬性

  • image

  • highlightedImage

  • userInteractionEnabled

  • animationImages

  • animationRepeatCount

 

重要的方法

- (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

這種方法解釋了如何在ImageView 操作顯示動畫圖像。

-(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 - Tab bar(標籤欄) 下一篇: iOS - Scroll View(滾動視圖)