美文网首页
UIButton和UIImageView的区别

UIButton和UIImageView的区别

作者: 飞天小猪 | 来源:发表于2016-02-24 19:14 被阅读0次

1.显示图片

1> UIImageView只能显示一种图片,并且图片默认会填充整个UIImageView    ( .image \ setImage: )

2> UIButton能显示2种图片  (image和backgroundImage)

image:覆盖在背景上面的图片, 按本身尺寸显示 ( setImage: forState: )

backgroundImage:会填充整个UIButton (setBackgroundImage: forState: )

并且UIButton能显示文字

2.点击事件

1> UIImageView默认不能响应点击事件

2> UIButton能响应点击事件 (addTarget : action : forControlEvents: )

3.继承结构

1> UIButton之所以能通过添加监听器来监听事件是因为它继承自UIControl, 而UIControl又继承自UIView

2> UIImageView不能是因为它直接继承自UIView

4.补充 UIImage加载图片的两种方式

1> 有缓存的

UIImage *image = [UIImage imageNamed:@“a.png”];

2> 无缓存的

// 全路径

NSString *path = [[NSBundle mainBundle] pathForResource:@“a.png” ofType:nil];

// path 是a.png得全路径

UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];

开发中使用哪个就显而易见了吧.

相关文章

网友评论

      本文标题:UIButton和UIImageView的区别

      本文链接:https://www.haomeiwen.com/subject/lrsikttx.html