Issue #308
If you don’t want to use https://github.com/onmyway133/EasyClosure yet, it’s easy to roll out a closure based UIButton
. The cool thing about closure is it captures variables
final class ClosureButton: UIButton {
var didTouch: (() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
addTarget(self, action: #selector(buttonTouched(_:)), for: .touchUpInside)
}
required init?(coder aDecoder: NSCoder) {
fatalError()
}
@objc private func buttonTouched(_: UIButton) {
didTouch?()
}
}
Then in cellForItem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: UserCell = collectionView.dequeue(for: indexPath)!
let user = users[indexPath.item]
cell.powerButton.didTouch = { [weak self] in
self?.openPowerView(user)
}
return cell
}
With this we can even forward touch event to another button
func forwardTouchEvent(button: ClosureButton) {
didTouch = { [weak button] in
button?.didTouch?()
}
}
Another benefit is that we can apply debouncing to avoid successive tap on button
let debouncer = Debouncer(timeInterval: 0.2)
@objc private func buttonTouched(_: UIButton) {
debouncer.run { [weak self] in
self?.didTouch?()
}
}