iOS UIButton按下和松开事件处理

1. 定义按钮对象

@property (nonatomic, strong) UIButton *topControl;

2. 懒加载

 - (UIButton *)topControl
{
    if (!_topControl) {
        _topControl = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_topControl setImage:[UIImage imageNamed:@"0.jpg"] forState:(UIControlStateNormal)];
        [_topControl setImage:[UIImage imageNamed:@"1.jpg"] forState:(UIControlStateHighlighted)];
        // 添加btn按下事件
        [_topControl addTarget:self action:@selector(btnTouchDownClick:) forControlEvents:(UIControlEventTouchDown)];
        // 添加btn松开事件
        [_topControl addTarget:self action:@selector(btnTouchUpClick:) forControlEvents:(UIControlEventTouchUpInside|UIControlEventTouchUpOutside)];     
    }
    return _topControl;
}

3. 实现按钮方法

// 按钮按下事件
- (void)btnTouchDownClick:(UIButton *)sender
{
    NSLog(@"按钮按下事件");
}

// 按钮抬起事件
- (void)btnTouchUpClick:(UIButton *)sender
{
    NSLog(@"按钮抬起事件");
}

你可能感兴趣的:(iOS UIButton按下和松开事件处理)