如何给组件UIButton添加事件并传递参数

如何给组件UIButton添加事件并传递参数,第1张

以UIButton为demo

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, width, height)]

//给button添加点击事件,action参数中写入事件执行方法

[button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside]

//在button的tag中添加你需要传递的参数,目前资料中只有这种方法

//你可以传入任意类型的参数

[button setTag:100]

//下面是action方法

-(void)action:(id)sender{

//这个sender其实就是UIButton,因此通过sender.tag就可以拿到刚才的参数

int i = [sender tag]

}

本节学习内容:

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")

}

UI给的图有时候很小,或者有个需求需要我们扩大button的点击区域

我们一般的 *** 作是在button 上添加一个view 增加点击事件,但是我们还有其他更方便的方法去扩大button 的点击区域。

怎样来实现这个功能呢?又有多少种方式可以实现呢?下面一一来讲。

当用户点击屏幕后:

如下图所示:

然而事件的响应链条是事件链条的逆向,根据视图层级的添加顺序从后往前的

继承与UIButton,重写下面的方法:

其实我们上面所做的变化其实如果仔细看点击区域还是个矩形,如果需要我们将点击区域规定在圆形范围 内,我们可以这样做:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/7906918.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-11
下一篇2023-04-11

发表评论

登录后才能评论

评论列表(0条)

    保存