我有以下代码。它是菜单的第三方库(名为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")
}
}
最佳答案
如果第三方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()
}
关于ios - UITapGestureRecognizer不适用于特定的[UIView]数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40510319/