gpt4 book ai didi

ios - 如何在 Swift 中将触摸事件发送到 nextResponder

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:46 26 4
gpt4 key购买 nike

背景

我试图让 UICollectionViewCell 的行为像一个按钮(即,在触摸时做一个动画暗淡)。我的想法是用 UIButton 填充单元格。然后按钮会处理被点击的视觉效果。但随后我需要将触摸事件传递给父级(UICollectionViewCell),以便按钮不只是消耗该事件。这样我就可以使用 Collection View 的 didSelectItemAtPath 来处理它。下面的问题是我试图弄清楚如何传递事件。

我的问题

我遇到了 this Objective-C answer用于传递触摸事件:

- (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 问题(herehere)来帮助我解决这个问题。

最佳答案

nextResponder 方法返回一个可选的 UIResponder?,因此您可以简单地在返回的对象上调用 touchesBegan:

self.nextResponder()?.touchesBegan(touches, withEvent: event)

更新:Swift 4.2

self.next?.touchesBegan(touches, with: event)

关于ios - 如何在 Swift 中将触摸事件发送到 nextResponder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32941869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com