1.创建⼀一个 button :buttonWithType:UIButton*button=[UIButton
buttonWithType:UIButtonTypeRoundedRect];//加方法创建,不需要 release
//创建⼀一个什么样的 button 用 buttonWithType:
typedef enum {
UIButtonTypeCustom = 0, //自定义(常加载图片) no
button type
UIButtonTypeRoundedRect, //圆角按钮UIButtonTypeDetailDisclosure, //尖叫号按钮UIButtonTypeInfoLight, 信息按钮(浅)UIButtonTypeInfoDark, 信息按钮(深)UIButtonTypeContactAdd, 加号按钮
} UIButtonType;
2.设置坐标和大小
button.frame = CGRectMake(10, 30, 300, 30);
3.// 设置按钮上的文字
// 在按钮默认状态(正常状态)下显示:圆角按钮
[btn1 setTitle:@"圆角按钮" forState:UIControlStateNormal];
UIControlStateNormal = 0, 正常状态UIControlStateHighlighted = 1 << 0, 高亮状态(点击按钮时)UIControlStateDisabled 按钮无效未激活时(enabled==NO)
// 在按钮点住状态(高亮状态下)下显示:按钮被点击
[btn1 setTitle:@"按钮被点击"forState:UIControlStateHighlighted];3.//更改按钮文字大小
btn.titleLabel.font = [UIFont systemFontOfSize:25] ;
4.// 设置点住时按钮变化颜色(高亮状态)
[btn1 setTintColor:[UIColor redColor]];
// 设置背景颜色后可以看出其实还是⼀一个矩形的View
[btn1 setBackgroundColor:[UIColor orangeColor]];
5. // 设置按钮的点击事件
/*
target:执行哪个对象中的方法
action:执行的方法controlEvents:触发的方式
*/
[btn1 addTarget:self action:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
UIControlEventTouchDown
UIControlEventTouchDown
单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat
多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside
当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside
当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter
当一次触摸从控件窗口之外拖动到内部时。
UIControlEventTouchDragExit
当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside
所有在控件之内触摸抬起事件。
UIControlEventTouchUpOutside
所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel
所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventTouchChanged
当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin
当文本控件中开始编辑时发送通知。
UIControlEventEditingChanged
当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd
当文本控件中编辑结束时发送通知。
UIControlEventEditingDidOnExit
当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAlltouchEvents
通知所有触摸事件。
UIControlEventAllEditingEvents
通知所有关于文本编辑的事件。
UIControlEventAllEvents
通知所有事件。
6.//生成⼀一个定时器,时间每隔0.02秒调用⼀一次updateTimer 函数,函数声
明在self-本类内部 repeats:Yes代表重复调用
第二个参数可以是任意类型的对象指针
[NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES] ;
NSTimer * timer; if(!timer)
timer = [NSTimer scheduledTimerWithTimeInterval:0.02target:self selector:@selector(setLabel) userInfo:nilrepeats:YES];
//启动⼀一个定时器,第⼀一个参数表示延迟时间,是浮点数,单位是秒.最后⼀一个参数表示是否重复,添NO表示这个事情只做⼀一次。
//启动新的线程0.02秒钟后,让self 调用 setLabel
//定时器不需要启动,从创建时就自动启动了,不需要释放,停下来时,
自动释放。
else{
[timer invalidate];//终止定时器,定时器会释放
timer = nil;}
7.tag 标签:可以区分是哪⼀一个控件;