圖像視圖的使用

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

重要的屬性

  • 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];
}