知识点:
1.UIButton使用和事件机制
2.UIImage
3.自定义UIButton
==================
UIButton
1.创建方式
按钮类型
UIButtonTypeCustom 用户自定义按钮
UIButtonTypeRoundedRect 系统按钮
UIButtonTypeDetailDisclosure 更多信息按钮 i
UIButtonTypeInfoLight 高亮信息按钮 i
UIButtonTypeInfoDark 暗色信息按钮 i
UIButtonTypeContactAdd 加号按钮 +
UIButton *b2 = [UIButton buttonWithType:UIButtonTypeCustom];
2.设置按钮的标题
-(void)setTitle:(NSString *)title forState:(UIControlState)state;
[b setTitle:@"点击录音" forState:UIControlStateNormal];
UIControlState:
UIControlStateNormal 按钮一般状态
UIControlStateHighlighted 按钮被按下(高亮)状态
UIControlStateDisabled 按钮被禁用状态
UIControlStateSelected 按钮被选中
//正常状态
[b setTitle:@"未选中" forState:UIControlStateNormal];
//选中状态
[b setTitle:@"选中" forState:UIControlStateSelected];
//禁用状态
[b setTitle:@"禁用" forState:UIControlStateDisabled];
//非选中状态==NO
//选中状态==YES
//btn.selected = !btn.selected;
//禁用
btn.enabled = NO;
3.设置按钮的颜色
1)设置按钮颜色
-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
4.事件处理
1)iOS事件分类
触控事件(单点、多点触控以及各种手势操作)
传感器事件(重力、加速度传感器等)
远程控制事件(远程遥控iOS设备多媒体播放等)
2)添加事件
-(void)addTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
[b2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
3)删除事件
-(void)removeTarget:(id)target
action:(SEL)action
forControlEvents:(UIControlEvents)controlEvents
5.常用事件:UIControlEvents
UIControlEventTouchDown 按钮按下
UIControlEventTouchUpInside 按钮按下弹起
UIControlEventTouchUpOutside 按钮按下,按钮外弹
[b addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
6.UIView标签:setTag
view1.tag = 100;
==========================
UIImage
+(UIImage *)imageNamed:(NSString *)name;
使用该方法的前提是图片必须已经存在与项目中,如果图片需要多次显示时使用(会消耗系统缓存 空间)
UIImage *image1 = [UIImage imageNamed:@"3_normal"];
+(UIImage *)UIImage imageWithContentsOfFile:(NSString *);
使用此方法加载文件时,系统不把图像以数据的方式加载到程序中,
如不需要复用该图像或者是一个很大的图像时推荐使用此方法
UIImage *image2 = [UIImage imageWithContentsOfFile:filePath]
获取项目中的资源路径
NSString * path = [[NSBundle mainBundle] pathForResource:@"Logo" ofType:@"png"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"3_selected" ofType:@"png"];
==========================
自定义UIButton
1.图片按钮
1)设置按钮背景图片
-(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//设置btn的背景图片 (正常状态)
[b setBackgroundImage:image1 forState:UIControlStateNormal];
2)设置按钮图片
-(void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置btn显示的图片(正常状态)
[b setImage:image1 forState:UIControlStateNormal];
[b setImage:image2 forState:UIControlStateSelected];
//设置btn显示的图片(高亮状态)
[b setImage:image2 forState:UIControlStateHighlighted];
2.实现过程
1)buttonType类型为 UIButtonTypeCustom
//实例化btn
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
2)设置图片:setImage:forState:image:图片的文件名
UIImage *image1 = [UIImage imageNamed:@"3_normal"];
原文:http://www.cnblogs.com/fcug/p/6291431.html