zoukankan      html  css  js  c++  java
  • IOS开发-UIImageView的使用-UIImageView与UIButton的区别

    一.概述:

    UIImageView根据名字可以知道就是用来显示图片的,本文主要总结了三个知识点

    1.使用UIImageView显示图片的常用的几种方式

    2.UIImageView与UIButton的区别

    二. 使用UIImageView显示图片的常用的几种方式

    1》》》》创建UIImageView对象的几种方式。

    1. UIImageView *icon1 = [[UIImageView alloc] init];创建没有初始化的UIImageView对象

    1       // 方式1
    2       UIImageView *icon1 = [[UIImageView alloc] init];
    3       // 设置位置
    4       icon1.frame = CGRectMake(0, 0, 375, 667);
    5       // setImage设置图片
    6       [icon1 setImage:[UIImage imageNamed:@"ping"]];
    7       [self.view addSubview:icon1];

    2.UIImageView *icon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"fuguizhu"]];

     创建已经初始化图片的UIImageView对象,但需要设置尺寸及位置才可以显示。

        UIImageView *icon2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ping"]];
        // 设置位置及尺寸
        icon2.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
        [self.view addSubview:icon2];

    3.UIImageView *icon3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,300, 400)];

     创建已经初始化了位置和尺寸的UIImageView对象,需要设置要显示的图片

        UIImageView *icon3 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
         // setImage设置图片
        [icon3 setImage:[UIImage imageNamed:@"ping"]];
        [self.view addSubview:icon3];

    4.- (instancetype)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage 

     创建已经初始化了默认状态和高亮状态要显示的图片的UIImageView对象,当highlighted属性为YES时显示highlightedImage 图片。

     UIImageView *icon4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ping"] highlightedImage:[UIImage imageNamed:@"fuguizhu"]];
          // 设置位置
          icon4.frame = CGRectMake(0, 0, 375, 667);
         //显示高亮状态的图片
          icon4.highlighted = YES;
         [self.view addSubview:icon4];

    上面四种方式显示效果如下图,前三种是相同的,第四种由于打开了highlighted所以显示了另外一张图片

    2》》》UIImageView控件的contentMode属性:

    这个属性是设置图片在手机中显示方式,比如是否居中、是否拉伸等对应storyboard中的右侧的视图如下:

        

    此属性的取值通常如下:

     1 typedef NS_ENUM(NSInteger, UIViewContentMode) {
     2     UIViewContentModeScaleToFill,
     3     UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
     4     UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
     5     UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
     6     UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
     7     UIViewContentModeTop,
     8     UIViewContentModeBottom,
     9     UIViewContentModeLeft,
    10     UIViewContentModeRight,
    11     UIViewContentModeTopLeft,
    12     UIViewContentModeTopRight,
    13     UIViewContentModeBottomLeft,
    14     UIViewContentModeBottomRight,
    15 };

    上面枚举常量中,不带Scale的常量值表示,当图片的尺寸超过了UIImageView设定的尺寸时之后显示一部分。

    UIViewContentModeScaleToFill:会填充整个UIImageView区域,会导致图片产生形变除非设定的范围和图片的比例一样。默认是此值。

    UIViewContentModeScaleAspectFit:会保证图片的比例不变全部显示在UIImageView区域但是会出现空白区域。

    UIViewContentModeScaleAspectFill:综合以上两者,即全部填充UIImageView区域也不会产生比例的变化,这样就意味着,不会显示完全.

                        

    UIViewContentModeScaleToFill      UIViewContentModeScaleAspectFit     UIViewContentModeScaleAspectFill

    2.UIImageView与UIButton的区别

    我们知道UIImageView与UIButton都能显示图片

      1)UIButton能够监听点击事件而UIImageView不能。

     原因:UIButton之所以能够添加监听器是因为他继承自UIControl反之只要是继承自UIControl的类都能添加监听事件,UIControl的类里面有个添加监听事件的方法:

    - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

      UIImageView不能添加事件因为它直接继承自最纯洁的UIView。

    2)UIButton既能显示文字又能显示图片,而UIImageView只能显示图片。

       看UIButton的两个属性:

    @property(nonatomic,readonly,retain) UILabel     *titleLabel NS_AVAILABLE_IOS(3_0);
    @property(nonatomic,readonly,retain) UIImageView *imageView  NS_AVAILABLE_IOS(3_0);

    UIButton里面包含了UILabel和UIImageView,他们一个能显示文字一个可现实图片。

    3. UIImageView与UIButton的使用场合

    UIButton:需要显示图片,点击图片后需要做一些特定的操作
    UIImageView:仅仅需要显示图片,点击图片后不需要做任何事
  • 相关阅读:
    Java8基础之native方法
    Java基础之static关键字
    Java基础之继承
    Java之equals和hashCode方法
    Java基础之this关键字
    Java基础之super关键字
    Java基础之Serializable接口
    Java之反射学习
    Python3之多线程学习
    Python3之深拷贝和浅拷贝区别
  • 原文地址:https://www.cnblogs.com/jianghg/p/4482776.html
Copyright © 2011-2022 走看看