UITapGestureRecognizer不适用于特定的[UIView]数组

时间:2016-11-09 15:17:21

标签: ios swift uiview uigesturerecognizer uitapgesturerecognizer

我有以下代码。它是菜单的第三方库(名为CarbonKit)。当我尝试选择特定的片段(标签)并添加手势识别器时,它无法正常工作。我有什么想法,我做错了什么?

为了清楚起见,我在handleTap中放置了一个断点,它甚至没有输入该函数。

    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 个答案:

答案 0 :(得分:0)

如果第三方UISegmentedControl与通用控件相似,那么您已经拥有了所需的一切。这是我的一些代码。如果您正在使用IB,请将控件连接到IBAction。

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()
}