如何将触摸事件发送到Swift中的nextResponder
背景
我试图让一个UICollectionViewCell
像一个button(即,做一个animation昏暗的触摸)行为。 我有一个用UIButton
填充单元的想法。 然后,button将照顾被窃听的视觉效果。 但是,然后我需要将触摸事件传递给父( UICollectionViewCell
),以便button不只是消耗事件。 这样我可以处理它与集合视图的didSelectItemAtPath
。 下面的问题是我试图找出如何通过事件。
我的问题
我遇到了这个Objective-C的答案传递触摸事件:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; [self.nextResponder touchesBegan:touches withEvent:event]; }
不过,当我试图将其转换为Swift时,我陷入了困境:
class MyButton: UIButton { override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesBegan(touches, withEvent: event) self.nextResponder() // ??? } }
nextResponder
方法不接受任何参数,那么如何传递触摸事件呢?
我无法使用相关的SO问题( 这里和这里 )来帮助我解决这个问题。
nextResponder
方法返回一个可选的UIResponder?
,所以你可以简单地在返回的对象上调用touchesBegan
:
self.nextResponder()?.touchesBegan(touches, withEvent: event)