UIImageView控件

  UIImageView是用于显示图像的,在iOS开发中,我们无需专门去写什么代码,不需要检查设备的类型,只需要把1x、2x、3x的图像添加到项目中,图像视图会自动的在正确的时间加载正确的图像。

  (1)UIImageView的创建及简单用法

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"banner01"]];

    imageView.backgroundColor = [UIColor lightGrayColor];

    imageView.frame = CGRectMake(20, 20, 200, 300);

    [self.view addSubview:imageView];

  (2)contentMode属性

typedef NS_ENUM(NSInteger, UIViewContentMode) {

    UIViewContentModeScaleToFill,    //图像填充整个imageView的大小

    UIViewContentModeScaleAspectFit,     //图像保持原宽高比放在视图中间

    UIViewContentModeScaleAspectFill,    //图像保持原宽高比扩充放在视图的中间点上

    UIViewContentModeRedraw,               // redraw on bounds change (calls -setNeedsDisplay)

    UIViewContentModeCenter,                //图像水平居中,竖直居中,图片保持原大小 

    UIViewContentModeTop,         //图像水平居中,竖直居上,图片保持原大小

    UIViewContentModeBottom,       //图像水平居中,竖直居下,图片保持原大小

    UIViewContentModeLeft,        //图像水平居左,竖直居中,图片保持原大小

    UIViewContentModeRight,       //图像水平居右,竖直居中,图片保持原大小

    UIViewContentModeTopLeft,      //图像水平居左,竖直居上,图片保持原大小

    UIViewContentModeTopRight,    //图像水平居左,竖直居上,图片保持原大小

    UIViewContentModeBottomLeft,    //图像水平居中,竖直居下,图片保持原大小

    UIViewContentModeBottomRight,  //图像水平居中,竖直居下,图片保持原大小

};

  (3)更改位置

    a.直接修改frame属性

    b.修改其center属性

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

    c.使用transform属性

       imageView.transform = CGAffineTransformMakeTranslation(<#CGFloat tx#>, <#CGFloat ty#>);

     其中dx与dy表示想要往x或者y方向移动多少,而不是移动到多少。

  (4)旋转图像

    imageView.transform = CGAffineTransformMakeRotation(<#CGFloat angle#>);

    图片是按照顺时针方向旋转的,而且旋转中心是图像的中心,即center值。angle的单位是弧度,而不是度数

  (5)缩放图像

    imageView.transform = CGAffineTransformMakeScale(<#CGFloat sx#>, <#CGFloat sy#>);

    sx表示水平方向缩放的倍数,sy表示垂直方向缩放的倍数

  (6)播放一系列图片

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 200, 400)];

    

    NSMutableArray *imagesArray = [NSMutableArray array];

    for (int i = 2; i<7; i++) {

        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]];

        [imagesArray addObject:image];

    }

    

    imageView.animationImages = imagesArray;

    //设置播放全部图片一次所用的时间

    imageView.animationDuration = 2.0;

    //设置播放图像的次数,0表示无数遍

    imageView.animationRepeatCount = 0;

    //开始播放
    [imageView startAnimating];

 

    [self.view addSubview:imageView];

    

  (7)为图片添加单击事件

    //imageView的用户交互一定要打开

      imageView.userInteractionEnabled = YES;

    //创建点击手势及点击事件

      UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickImageView)];

      //将点击事件加入到imageView中

      [imageView addGestureRecognizer:tap];

  (8)其他设置

     a.alpha属性:设置图像的透明度,在做一些模糊效果时不妨试试

     b.hidden属性:控制图像的显示与否,看情况随意设置

原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/4725140.html