自定义按钮touchUpInside事件不火
问题描述:
,当我把一些代码在我的自定义按钮自定义按钮touchUpInside事件不火
注意我的自定义按钮touchupinside事件不火:当我在按键敲击这种方法很火,但touchUpInside方法不火
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
NSLog(@"touchBegan");
[Singleton playSoundForEvent:Sound_ButtonTap];
}
如果我删除上面的代码,然后工作正常。
上面的代码原因:我没有在touchupinside处处放点击声音。 我只想在整个项目中使用一行代码。
答
你忘了调用super
[super touchesBegan:touches withEvent:event];
它并没有调用超工作的原因,是一个按钮,需要注册触摸事件(touchesBegan
,touchesEnded
,touchesMoved
,touchesCanceled
)是能够将它们映射到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];
的touchesBegan呼吁每UIcontrols那么你将如何处理这个 –
@ Anbu.Karthik请检查下面的答案,这将沃金对我很好,所以我的问题已经解决了。 – Vivek