美文网首页
IOS开发UIButton基础&事件

IOS开发UIButton基础&事件

作者: 奔跑的小小鱼 | 来源:发表于2017-04-19 20:48 被阅读119次

本节学习内容:

1.UIButton的控件基本概念

2.UIButton的创建方法

3.UIButton的类型

4.可显示图片的UIButton

【viewController.m】

-(void)createUIRectButton{

//创建一个btn对象,根据类型来创建button

//圆角类型btn:UIButtonTypeRoundedRect

//通过类方法来创建ButtonWithType:类名+方法

UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

//设置button按钮的位置

btn.frame=CGRectMacke(100,100,100,40);

//设置按钮的文字内容

// @parameter p1:字符串类型,显示到按钮上的文字,

//P2:设置文字显示的状态类型:UICountrolStateNormal,正常状态

btn setTitle:@"按钮01" forState:UICountrolStateNormal];

//P1显示文字

//P2显示状态:UICountrolStateHighLighted

btn setTitle:@"按钮按下" forState:UICountrolStateHighLighted];

//设置button背景颜色

btn.backgroundColor=[UIColor grayColor];

//设置文字颜色

//p1 颜色,p2状态

[btn setTitleColor:[UIColor redColor] forState:UIControlStaNromal];

//设置按下状态的颜色

[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHiglighted];

//设置按钮风格颜色

[btn setTintColor:[UIColor whitecolor]];

//注:setTitleColor 优先级高于setTintColor

//titleLabel:UILabel空控

btn,titleLable.font=[UIFount systemFontOfSize:12];

//添加到试图中并显示

[self.view addSubview:btn]

}

//创建一个可以显示图片btn

-(void)createImageBtn{

//创建一个自定义类型的btn

UIButton *btnImage=[Button vuttonWithType:UIButtonTypeCustom];

//图片Btn位置

btnImage.frame=CGRectMake(100,200,100,100);

//默认图片

UIImage *icon01=[UIImage imageNamed:@"btn02.jpg"];

//点击后的图片

UIImage *icon02=[UIImage imageNamed:@"btn03.jpg"];

//设置按钮图片方法设置 p1:显示的图片对象,p1控件状态

[btnImage setImage:icon01 forState:UIControlStateNormal];

[btnImage setImage:icon02 forState:UIControlStateHighlighted]; 

[self.view addSubview:btnImage];

}


【UIButton事件】

本节学习内容:

1.UIButton的事件的概念

2.UIButton的添加方法

3.UIButton的响应函数

4.多按钮使用同一事件函数

【viewController.h】

-(void)createBtn{

//创建圆角按钮

UIButton *btn=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];

btn.fram=CGRectMake(100,100,80,40);

[btn setTitle:@"按钮" froState:UIControlStateNormal];

//给按钮事件函数

//参数P1:谁来实现事件函数,实现者对像都就是“谁|"

//参数P2:@selector(pressBtn)函数对像,当按钮满足P3事件类型时,庙用函数

//参数p3:UIControlEvent:事件处理函数类型

//UIControlEventTouchUpinside:当手指离开屏幕时并且手指的位置在按钮范围内触发事件函数

//UIControlEventTouchDowninside:当手指触摸屏幕上触发

/*

调用不带参数事件

btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];

*/

/*

调用不带参数事件

btn addTarget:self acion:@selector(pressBtn)forCountrolEvents:UIControlEventTouchUpinside];

*/

//带参数事件

btn addTarget:self acion:@selector(pressBtn:)forCountrolEvents:UIControlEventTouchUpinside];

//确摸时调用事件函数

[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown]

[self.veiw addSubView:Btn];

UIButton *btn02=[UIButton buttonWithTpye:UIButtonTypeRoundedRect];

btn02.fram=CGRectMake(100,200,80,40);

[btn02 setTitle:@"按钮" froState:UIControlStateNormal];

//可以多个按钮使用同一个事函数来处理不同按钮事件

[btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchDown];

[self.veiw addSubView:Btn02];

//设置按钮标记值

btn.tag=101;

btn02.tag=102;

-(void)pressBtn02{

if(btn.tag==101){

NSLog(@"btn 01 pressed ");

}

if(btn.tag==102){

NSLog(@"btn 02 pressed");

}

-(void)touchDown

{

NSLog(@"按钮被触摸!");

}

-(void)pressBtn{

NSLog(@"按钮被按了一下!");

}

//参数为按钮本身

-(void)pressBtn:(UIButton*)btn{

NSLog(@"tbn pressed");

}


相关文章

网友评论

      本文标题:IOS开发UIButton基础&事件

      本文链接:https://www.haomeiwen.com/subject/rmihzttx.html