touchesBegan 和 touchesEnded 未在 UIButton 上调用

Posted

技术标签:

【中文标题】touchesBegan 和 touchesEnded 未在 UIButton 上调用【英文标题】:touchesBegan and touchesEnded not called on a UIButton 【发布时间】:2016-04-16 11:05:05 【问题描述】:

当我触摸UIButton 时,不会调用touchesBegantouchesMovedtouchesEnded 方法。我想在发生连续触摸时执行一个事件,并在触摸结束时取消该事件。

【问题讨论】:

touchesbegan/moved/ended not working with xcode 6.3的可能重复 检查 userInteractionEnabled 在按钮及其所有超级视图上设置为 true。对于大多数UIViews,默认情况下为 true,但也有一些例外。 对于 uibutton 这不是调用 你设置的是delegate吗? 【参考方案1】:

您可以使用 UIButton 控件事件跟踪 UIButton 触摸开始和触摸结束。

- (void)viewDidLoad 
    [super viewDidLoad];

    [self.button addTarget:self action:@selector(touchStarted:) forControlEvents:UIControlEventTouchDown];
    [self.button addTarget:self action:@selector(touchEnded:) forControlEvents:UIControlEventTouchUpInside];
    [self.button addTarget:self action:@selector(touchEnded:) forControlEvents:UIControlEventTouchUpOutside];



- (void)touchStarted:(id)sender 
    NSLog(@"Touch started");


- (void)touchEnded:(id)sender 
    NSLog(@"Touch ended");

【讨论】:

以上是关于touchesBegan 和 touchesEnded 未在 UIButton 上调用的主要内容,如果未能解决你的问题,请参考以下文章