简体   繁体   English

如何使用Swift创建UIBarButton来调用UITapGestureRecognizer函数?

[英]How to create UIBarButton to call UITapGestureRecognizer function using Swift?

My scenario, I am trying to implement UIBarButton click to call below two functions . 我的情况是,我正在尝试实现UIBarButton单击以调用以下两个functions Below code already working for view tab and pan but now I am changing button click to call below two functions. 下面的代码已经可以用于view tab and pan但是现在我更改按钮单击以调用以下两个功能。 How to modify below code ? 如何修改下面的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
        }

    }

If you need to call both these tap gesture recognizer functions from a UIBarButtonItem , your best bet is to create a custom view bar button item and add gesture recognizers to that custom view. 如果您需要从UIBarButtonItem调用这两个轻UIBarButtonItem手势识别器函数,则最好的选择是创建一个自定义视图栏按钮项,并将手势识别器添加到该自定义视图。 Something like this would work: 这样的事情会起作用:

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)

Here are some additional resources for UITapGestureRecognizers and UIBarButtonItems . 这是UITapGestureRecognizersUIBarButtonItems的一些其他资源。 It seems a bit odd to set up 2 different tap gestures for the same view, though, and one of them being a pan action (at least by name) seems odd, but this is how you'd accomplish that! 但是,为同一视图设置2个不同的轻击手势似乎有些奇怪,而且其中一个是平移动作(至少是按名称命名)似乎很奇怪,但这就是您要实现的方式!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM