菜鸟启航:UIButton基本操作

来源:互联网 发布:js cookie存储时间 编辑:程序博客网 时间:2024/05/28 23:11

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= [UIFontsystemFontOfSize:25] ;

4.//设置点住时按钮变化颜色(高亮状态)
[btn1 setTintColor:[UIColorredColor]];
//设置背景颜色后可以看出其实还是⼀一个矩形的View
[btn1 setBackgroundColor:[UIColororangeColor]];

5. //设置按钮的点击事件

/*
target:
执行哪个对象中的方法

action:执行的方法controlEvents:触发的方式

*/

[btn1 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

UIControlEventTouchDown

鼠标左键按下触发

鼠标按下,在button内抬起鼠标按下,在button外抬起

UIControlEventTouchUpInside

触发

   UIControlEventTouchUpOutside

触发

6.//生成⼀一个定时器,时间每隔0.02秒调用⼀一次updateTimer函数,函数声

明在self-本类内部repeats:Yes代表重复调用

第二个参数可以是任意类型的对象指针
   [NSTimer scheduledTimerWithTimeInterval:0.02target:self selector:@selector(updateTimer) userInfo:nilrepeats:YES] ;
   NSTimer * timer;   if(!timer)

timer = [NSTimerscheduledTimerWithTimeInterval:0.02target:selfselector:@selector(setLabel)userInfo:nilrepeats:YES];

//启动⼀一个定时器,第⼀一个参数表示延迟时间,是浮点数,单位是秒.最后⼀一个参数表示是否重复,添NO表示这个事情只做⼀一次。

//启动新的线程0.02秒钟后,让self调用setLabel

//定时器不需要启动,从创建时就自动启动了,不需要释放,停下来时,

自动释放。

else{
[timer
invalidate];//终止定时器,定时器会释放

timer = nil;}

7.tag标签:可以区分是哪⼀一个控件; 

0 0
原创粉丝点击