没有设置触摸手势,但是tabBar只响应长按。为什么?
No touch gestures were set, but tabBar only responds to long press. Why?
我有一个视图,顶部有一个 tabBar,下面是一个 tableView。 tableView 具有受限于它的触摸手势(点击、双击和平移手势)。但是,tabBar 没有安装触摸手势,仅响应长按触摸。
一些有用的信息:
- tabBar 委托设置为 self
只有标签条码:
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 101 { //tag number of first tab bar item
print("tableView")
}
if item.tag == 102 { //tag number of second tab bar item
print("collectionView") //going to add collection view eventually
}
}
如有任何帮助,我们将不胜感激。谢谢!
由于我想不通(可能与它下面的tableView内容有关),我决定使用触摸手势来解决问题:
@IBOutlet weak var tabBar: UITabBar!
override func viewDidLoad() {
let tabBarTap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.switchSelectedTabItem(tapGestureRecognizer:)))
tabBarTap.delegate = self
tabBarTap.numberOfTapsRequired = 1
tabBarTap.numberOfTouchesRequired = 1
tabBar.addGestureRecognizer(tabBarTap)
}
@objc func switchSelectedTabItem(tapGestureRecognizer: UITapGestureRecognizer) {
let touchPoint = tapGestureRecognizer.location(in: self.view)
let split = tabBar.frame.width/2 //because i have two tab bar items. divide it by how many tab bar items you have
if touchPoint.x > split { //again, since i have two tab bar items, i can just divide the location of the tap into two sections (tab bar item 1 and tab bar item 2)
tabBar.selectedItem = tabBar.items?[1]
//run code for when second item is selected
} else {
tabBar.selectedItem = tabBar.items?[0]
//run code for when second item is selected
}
}
我有一个视图,顶部有一个 tabBar,下面是一个 tableView。 tableView 具有受限于它的触摸手势(点击、双击和平移手势)。但是,tabBar 没有安装触摸手势,仅响应长按触摸。
一些有用的信息: - tabBar 委托设置为 self
只有标签条码:
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 101 { //tag number of first tab bar item
print("tableView")
}
if item.tag == 102 { //tag number of second tab bar item
print("collectionView") //going to add collection view eventually
}
}
如有任何帮助,我们将不胜感激。谢谢!
由于我想不通(可能与它下面的tableView内容有关),我决定使用触摸手势来解决问题:
@IBOutlet weak var tabBar: UITabBar!
override func viewDidLoad() {
let tabBarTap = UITapGestureRecognizer(target: self, action: #selector(MyViewController.switchSelectedTabItem(tapGestureRecognizer:)))
tabBarTap.delegate = self
tabBarTap.numberOfTapsRequired = 1
tabBarTap.numberOfTouchesRequired = 1
tabBar.addGestureRecognizer(tabBarTap)
}
@objc func switchSelectedTabItem(tapGestureRecognizer: UITapGestureRecognizer) {
let touchPoint = tapGestureRecognizer.location(in: self.view)
let split = tabBar.frame.width/2 //because i have two tab bar items. divide it by how many tab bar items you have
if touchPoint.x > split { //again, since i have two tab bar items, i can just divide the location of the tap into two sections (tab bar item 1 and tab bar item 2)
tabBar.selectedItem = tabBar.items?[1]
//run code for when second item is selected
} else {
tabBar.selectedItem = tabBar.items?[0]
//run code for when second item is selected
}
}