2017-08-04 154 views
-1

,当我把一些代码在我的自定义按钮自定义按钮touchUpInside事件不火

注意我的自定义按钮touchupinside事件不火:当我在按键敲击这种方法很火,但touchUpInside方法不火

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

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

上面的代码原因:我没有在touchupinside处处放点击声音。 我只想在整个项目中使用一行代码。

+0

的touchesBegan呼吁每UIcontrols那么你将如何处理这个 –

+0

@ Anbu.Karthik请检查下面的答案,这将沃金对我很好,所以我的问题已经解决了。 – Vivek

回答

1

你忘了调用super

[super touchesBegan:touches withEvent:event]; 

它并没有调用超工作的原因,是一个按钮,需要注册触摸事件(touchesBegantouchesEndedtouchesMovedtouchesCanceled)是能够将它们映射到UIControlEvent,然后才用sendAction发起行动。

的完整代码

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

但是,当我删除上面的代码,然后工作正常, – Vivek

+0

是的,因为那么你的自定义按钮使用其父功能。 –

+0

我在哪里放代码'[超级touchesBegan:触及事件:事件];' – Vivek

0

请检查该代码并再次测试,我希望它会正常工作。

[super touchesBegan:touches withEvent:event];

相关问题