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(滚动视图)