UITapGestureRecognizer not working for specific [UIView] array

孤街浪徒 提交于 2020-01-05 04:24:10

问题


I have the following piece of code. It's a third party library for a menu (named CarbonKit). When I try to select a specific segment (tab) and add a gesture recognizer, it doesn't work. Any ideas what I'm doing wrong?

To be clear, I placed a breakpoint in the handleTap, it it doesn't even enter the function.

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.userInteractionEnabled = true
        let tgr : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(OverviewFolder.handleTap(_:)))
        // segment 2 (categories)
        carbonTabSwipeNavigation.carbonSegmentedControl?.segments![2].userInteractionEnabled = true
        carbonTabSwipeNavigation.carbonSegmentedControl?.segments![2].addGestureRecognizer(tgr)
}

 // tap
    func handleTap(gestureRecognizer : UITapGestureRecognizer){
        let test = carbonTabSwipeNavigation.currentTabIndex
        if test == 2 {
            print("second item tapped")
        }
    }

回答1:


If the 3rd party UISegmentedControl is like the generic one, you already have everything you need. Here's some of my code. If you are using IB, wire the control up to an IBAction instead.

let imageSegments = UISegmentedControl (items: ["Original","Stained"])

override func viewDidLoad() {
    super.viewDidLoad()
    imageSegments.tintColor = UIColor.yellow
    imageSegments.selectedSegmentIndex = 1
    imageSegments.addTarget(self, action: #selector(changeImageView), for: .valueChanged)
    view.addSubview(imageSegments)
}

func changeImageView() {
    switch imageSegments.selectedSegmentIndex {
    case 0:
        imageView.image = imgOriginal
    case 1:
        imageView.image = imgEdited
    default:
        break
    }
    imageView.setNeedsDisplay()
}


来源:https://stackoverflow.com/questions/40510319/uitapgesturerecognizer-not-working-for-specific-uiview-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!