gpt4 book ai didi

objective-c - 处理单个 UITabBarItem 上的长按

转载 作者:太空狗 更新时间:2023-10-30 03:40:03 25 4
gpt4 key购买 nike

我在标签栏上使用长按手势。但我只需要一个特定标签栏项目的长按手势。

我该如何解决这个问题?我可以自定义标签栏中的长按手势吗?

最佳答案

下面是我如何使用 Swift 3 实现的:

protocol MyTabControllerProtocol: class {
func tabLongPressed()
}

class MyTabController: UITabBarController {
func viewDidLoad() {
super.viewDidLoad()

viewControllers = [
// add your view controllers for each tab bar item
// NOTE: if you want view controller to respond to long press, then it should extend MyTabControllerProtocol
]

let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(astroButtonItemLongPressed(_:)))
tabBar.addGestureRecognizer(longPressRecognizer)
}

func astroButtonItemLongPressed(_ recognizer: UILongPressGestureRecognizer) {
guard recognizer.state == .began else { return }
guard let tabBar = recognizer.view as? UITabBar else { return }
guard let tabBarItems = tabBar.items else { return }
guard let viewControllers = viewControllers else { return }
guard tabBarItems.count == viewControllers.count else { return }

let loc = recognizer.location(in: tabBar)

for (index, item) in tabBarItems.enumerated() {
guard let view = item.value(forKey: "view") as? UIView else { continue }
guard view.frame.contains(loc) else { continue }

if let nc = viewControllers[index] as? UINavigationController {
if let vc = nc.viewControllers.first as? MyTabControllerProtocol {
vc.tabLongPressed()
}
} else if let vc = viewControllers[index] as? MyTabControllerProtocol {
vc.tabLongPressed()
}

break
}
}
}

关于objective-c - 处理单个 UITabBarItem 上的长按,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32968513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com