自定义按钮 touchUpInside 事件不触发



当我在自定义按钮中放置一些代码时,我的自定义按钮 touchupinside 事件没有触发

注意:当我点击按钮时,此方法是触发的,但touchUpInside方法不会触发

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}

如果我删除上面的代码,那么工作正常。

上面的代码原因:我不会在 touchup inside上到处放点击声音。 我想要整个项目中只有一行代码。

你忘了打电话给super

[super touchesBegan:touches withEvent:event];

如果没有对 super 的调用,它就无法工作的原因是按钮需要注册触摸事件(touchesBegantouchesEndedtouchesMovedtouchesCanceled(才能将它们映射到UIControlEvent,然后才使用sendAction触发操作。

完整代码

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
NSLog(@"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}

请检查此代码并再次测试 我希望它能正常工作。

[super touchesBegan:touches withEvent:event];

最新更新