UIButton 解析

来源:互联网 发布:淘宝上有好看衣服吗 编辑:程序博客网 时间:2024/05/29 18:55

               一下是总结UIButton的基本用法


UIButton在视图创建过程中用处很广,下面先创建一个UIButton,并给UIButton一个按钮类型

UIButton *button1  = [UIButton buttonWithType:UIButtonTypeRoundedRect];  

//button的按钮类型主要有一下六种:{

UIButtonTypeCustom = 0, //自定义类型

UIButtonTypeRoundedRect,//圆角矩形

UIButtonTypeDetailDisclosure, //箭头类型,用于具体说明

UIButtonTypeInfoLight,//亮色感叹号

UIButtonTypeInfoDark,//暗色感叹号

UIButtonTypeContactAdd,//加号按钮 }


button1.frame  = CGRectMake(50,50,50,50);  //给button在View上定义一个位置;

button1.backgroundcolor = [UIColor redColor]; //添加背景颜色;

[button1 setTitle:@"确认"forState:UIControlStateNormal];  //设置标题;

[button1 setImage:[UIImage imageNamed:@"1.jpg"]forState:UIControlStateNormal]; //添加图片,定义显示状态;


forState表示在何种状态下显示定义的button的文字和图片:{

UIControlStateNormal = 0, //正常状态下显示;

UIControlStateDisabled = 1<<1, //禁用状态下显示;

UIControlStateHighlighted = 1<<0, // 高亮状态下显示;

UIControlStateSelected = 1<<2, // 选中状态下显示;

UIControlStateApplication = 0x00FF0000, //应用程序标志状态下显示;

UIControlStateReserved = 0xFF000000,  //内部框架预留(不用);};


为button添加响应事件:

{

[button1 addTarget:self action(SEL) forControlEvents:UIControlEventTouchUpInside];//self本身调用action对应的方法, forControlEvents:UIControlEventTouchUpInside  控件事件处理的消息;

}


button内存释放问题:(button有自己的四种初始化方法,也可以用父类的初始化方法,选用的初始化方法不同,对内存空间处理方法也不同)

[UIButton buttonWithType:UIButtonTypeRoundedRect]; //这种属于button的初始化类方法(不需要release,否则会crash);

[UIButton alloc]init]; //这种属于button的实例初始化方法(需要手动release);


UIButton的事件:{

UIControlEventTouchDown  //单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候;

UIControlEventTouchDownRepeat  //  多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候;

UIControlEventTouchDragInside //当一次触摸在控件窗口内拖动时。

UIControlEventTouchDragOutside //当一次触摸在控件窗口之外拖动时。

UIControlEventTouchDragEnter //当一次触摸从控件窗口之外拖动到内部时。

UIControlEventTouchDragExit //当一次触摸从控件窗口内部拖动到外部时。

UIControlEventTouchUpInside //所有在控件之内触摸抬起事件。

UIControlEventTouchUpOutside //所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)

UIControlEventTouchCancel //所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。

UIControlEventTouchChanged //当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。

UIControlEventEditingDidBegin // 当文本控件中开始编辑时发送通知。

UIControlEventEditingChanged //当文本控件中的文本被改变时发送通知。

UIControlEventEditingDidEnd //当文本控件中编辑结束时发送通知。

UIControlEventEditingDidOnExit //当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。

UIControlEventAllTouchEvents // 通知所有触摸事件。

UIControlEventAllEditingEvents //通知所有关于文本编辑的事件。

UIControlEventAllEvents //通知所有事件。}




0 0
原创粉丝点击