在我的场景中,我正在尝试实现 UIBarButton
单击以调用以下两个 函数
。下面的代码已经适用于 view
tab 和平移
但现在我正在更改按钮单击以调用下面两个函数。如何修改下面的代码
?
@objc
func handleCardTap(recognzier:UITapGestureRecognizer) {
switch recognzier.state {
case .ended:
animateTransitionIfNeeded(state: nextState, duration: 0.9)
default:
break
}
}
@objc
func handleCardPan (recognizer:UIPanGestureRecognizer) {
switch recognizer.state {
case .began:
startInteractiveTransition(state: nextState, duration: 0.9)
case .changed:
let translation = recognizer.translation(in: self.cardViewController.handleArea)
var fractionComplete = translation.y / cardHeight
fractionComplete = cardVisible ? fractionComplete : -fractionComplete
updateInteractiveTransition(fractionCompleted: fractionComplete)
case .ended:
continueInteractiveTransition()
default:
break
}
}
最佳答案
如果您需要从 UIBarButtonItem
调用这两个点击手势识别器函数,最好的办法是创建自定义 View 栏按钮项并将手势识别器添加到该自定义 View 。这样的事情会起作用:
let tapGestureView = UIView() // initialize & size this as you need
tapGestureView.addGestureRecognizer(UITapGestureRecognizer(action: #selector(handleCardTap(_:)), target: self))
tapGestureView.addGestureRecognizer(UITapGestureRecognizer(action: #selector(handleCardPan(_:)), target: self))
let barButton = UIBarButtonItem(customView: tapGestureView)
以下是 UITapGestureRecognizers 的一些额外资源 和 UIBarButtonItems .不过,为同一 View 设置 2 个不同的点击手势似乎有点奇怪,其中一个是平移 Action (至少从名字上看)似乎很奇怪,但这就是你实现它的方式!
关于ios - 如何创建 UIBarButton 以使用 Swift 调用 UITapGestureRecognizer 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54595227/