如何创建 UIBarButton 以使用 Swift 调用 UITapGestureRecognizer 函数?
How to create UIBarButton to call UITapGestureRecognizer function using Swift?
我的方案,我正在尝试实现UIBarButton
点击调用下面两个functions
。下面的代码已经适用于 view
tab and pan
但现在我正在更改按钮单击以调用以下两个函数。 code
下面如何修改?
@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
调用这两个点击手势识别器函数,最好的办法是创建一个自定义视图栏按钮项并将手势识别器添加到该自定义视图。像这样的东西会起作用:
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。不过,为同一个视图设置 2 个不同的点击手势似乎有点奇怪,其中一个是平移动作(至少从名字上看)似乎很奇怪,但这就是你实现它的方式!
我的方案,我正在尝试实现UIBarButton
点击调用下面两个functions
。下面的代码已经适用于 view
tab and pan
但现在我正在更改按钮单击以调用以下两个函数。 code
下面如何修改?
@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
调用这两个点击手势识别器函数,最好的办法是创建一个自定义视图栏按钮项并将手势识别器添加到该自定义视图。像这样的东西会起作用:
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。不过,为同一个视图设置 2 个不同的点击手势似乎有点奇怪,其中一个是平移动作(至少从名字上看)似乎很奇怪,但这就是你实现它的方式!