UIButton和UIImageView的区别

2016-02-24  本文已影响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];

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

上一篇 下一篇

猜你喜欢

热点阅读