UIButton基础以及使用block+UIButton处理点击事件

本文介绍了UIButton的自定义设置方法,包括按钮样式、状态、颜色及图片等,并详细讲解了如何通过添加目标和动作来处理按钮点击事件,还提供了一个使用Block重写的点击事件示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//    UIButton * btn = [[UIButton alloc] init];//custom类型,一般不使用

    //UIButtonTypeSystem 只能得到图片的轮廓,不能得到纹理
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //不能这么设置按钮颜色
//    btn.titleLabel.text = @"123";
    /*

     ypedef NS_OPTIONS(NSUInteger, UIControlState) {
     UIControlStateNormal       = 0,                // 常规状态显现
     UIControlStateHighlighted  = 1 << 0,      // 高亮状态显现
     UIControlStateDisabled     = 1 << 1,        // 禁用的状态才会显现
     UIControlStateSelected     = 1 << 2,         //  选中状态
     UIControlStateApplication  = 0x00FF0000,   // 当应用程序标志时
     UIControlStateReserved     = 0xFF000000     //为内部框架预留
     // 后两个可以不管他
     };
     */


    [btn setTitle:@"正常按钮" forState:UIControlStateNormal];
    [btn setTitle:@"高亮状态" forState:UIControlStateHighlighted];

    [btn setTitle:@"禁用状态" forState:UIControlStateDisabled];
    [btn setTitle:@"选中状态" forState:UIControlStateSelected];


    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateDisabled];
    [btn setTitleColor:[UIColor magentaColor] forState:UIControlStateSelected];

    [btn setImage:[UIImage imageNamed:@"apply_sex_normal"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"apply_sex_selected"] forState:UIControlStateSelected];

    [btn setBackgroundImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateSelected];

    btn.backgroundColor = [UIColor grayColor];
    //设置字体
    btn.titleLabel.font = [UIFont systemFontOfSize:20];

//    NSDictionary * dict = @{NSFontAttributeName:[UIFont systemFontOfSize:30]};
//    NSAttributedString * att = [[NSAttributedString alloc] initWithString:@"Attributed" attributes:dict];
//    
//    [btn setAttributedTitle:att forState:UIControlStateNormal];

    //tintColor只有在systemtype时有效,tintColor是子视图及以上视图的颜色。
//    btn.tintColor = [UIColor orangeColor];

    //开启禁用状态
//    btn.enabled = NO;

    //开启选中状态
//    btn.selected = YES;
    /*

     iOS UIButton事件:
     UIControlEventTouchDown
     单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
     UIControlEventTouchDownRepeat
     多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
     UIControlEventTouchDragInside
     当一次触摸在控件窗口内拖动时。
     UIControlEventTouchDragOutside
     当一次触摸在控件窗口之外拖动时。
     UIControlEventTouchDragEnter
     当一次触摸从控件窗口之外拖动到内部时。
     UIControlEventTouchDragExit
     当一次触摸从控件窗口内部拖动到外部时。
     UIControlEventTouchUpInside
     所有在控件之内触摸抬起事件。
     UIControlEventTouchUpOutside
     所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
     UIControlEventTouchCancel
     所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
     UIControlEventTouchChanged
     当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
     UIControlEventEditingDidBegin
     当文本控件中开始编辑时发送通知。
     UIControlEventEditingChanged
     当文本控件中的文本被改变时发送通知。
     UIControlEventEditingDidEnd
     当文本控件中编辑结束时发送通知。
     UIControlEventEditingDidOnExit
     当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
     UIControlEventAlltouchEvents
     通知所有触摸事件。
     UIControlEventAllEditingEvents
     通知所有关于文本编辑的事件。
     UIControlEventAllEvents
     通知所有事件。


     */
    //添加响应事件
    [btn addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];

//    [btn removeTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];


//    [btn addTarget:self action:@selector(doAction2) forControlEvents:UIControlEventTouchUpOutside];
//    [btn addTarget:self action:@selector(doAction3) forControlEvents:UIControlEventAllTouchEvents];


//    btn.contentEdgeInsets = UIEdgeInsetsMake(50, 50, 50, 50);
    btn.titleEdgeInsets = UIEdgeInsetsMake(50, 0, 0, 100);
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);

    //点击按钮有光照效果
//    btn.showsTouchWhenHighlighted = YES;
    //高亮状态按钮颜色是否变深
    btn.adjustsImageWhenHighlighted = NO;

    btn.frame = CGRectMake(100, 100, 200, 200);
    [self.view addSubview:btn];

UIButton使用block重写点击事件

1 buttonBlock.h

#import <UIKit/UIKit.h>

typedef void(^Block)(UIButton * button);

@interface BlockButton : UIButton

@property (nonatomic, copy) Block block;

@end

2 buttonBlock

#import "BlockButton.h"

@implementation BlockButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)doAction:(UIButton *)button {

    self.block(button);
}

@end

3 在ViewController.m里面调用

#import "ViewController.h"
#import "BlockButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    BlockButton * btn = [BlockButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn setTitle:@"点我" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    btn.block = ^(UIButton * button) {

        NSLog(@"%@",button);

    };
    [self.view addSubview:btn];

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值