美文网首页
iOS UIButton按下和松开事件处理

iOS UIButton按下和松开事件处理

作者: 遇见I你 | 来源:发表于2021-08-24 10:17 被阅读0次

    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按下和松开事件处理

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