4
A
回答
5
您可以繼承UITabBarController
並將它添加到UILongPressGestureRecognizer
它的tabBar
。作爲手勢識別器的代表,您可以選擇何時檢測長按。由於一旦用戶觸摸它,標籤欄項目就會被選中,您可以使用selectedItem
屬性來執行此檢查。
@interface TabBarController() <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UILongPressGestureRecognizer *longPressRecognizer;
@end
@implementation TabBarController
- (void)viewDidLoad {
[super viewDidLoad];
self.longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(recognizerFired:)];
self.longPressRecognizer.delegate = self;
[self.tabBar addGestureRecognizer:self.longPressRecognizer];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// This will ensure the long press only occurs for the
// tab bar item which has it's tag set to 1.
// You can set this in Interface Builder or in code
// wherever you are creating your tabs.
if (self.tabBar.selectedItem.tag == 1) {
return YES;
}
else {
return NO;
}
}
- (void)recognizerFired:(UILongPressGestureRecognizer *)recognizer {
// Handle the long press...
}
@end
+2
是否可以識別長按TabBarItem而不切換選項卡? –
1
如果你只需要認識上的TabBar項目之一的長按,就可以在相應的viewController的viewDidLoad
方法做到這一點:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleLongPress:)];
[self.tabBarController.tabBar addGestureRecognizer: longPressGesture];
然後:
- (void)handleLongPress:(UILongPressGestureRecognizer *) recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
UITabBar *tabBar = ((UITabBar*)recognizer.view);
if (tabBar.selectedItem == self.tabBarItem) {
doSomethingVeryExciting();
}
}
}
如果你只是切換標籤,這不會觸發。
2
以下是我做的雨燕採用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
}
}
}
相關問題
- 1. php在一個頁面上處理多個表單處理
- 2. 如何處理一個表單上的兩個提交按鈕?
- 3. 如何處理長按ListView項?
- 4. 如何處理PanResponder長按事件?
- 5. 處理長按媒體按鈕在藍牙設備上播放/暫停
- 6. 按鈕單擊處理
- 7. 如何選擇在UITabBarItem上單擊時顯示哪個UIViewController?
- 8. 在UITABLEVIEW中處理單個單元格上的8個標籤
- 9. 在單個JSP上處理多個表單
- 10. 在Django的單個頁面上處理多個表單
- 11. 多個表單在一個頁面上,如何處理後退按鈕行爲?
- 12. 如何處理iPhone上的每個和每個在tableviewcell上的按鈕按鈕
- 13. JTable單元格 - 處理長文本
- 14. 角2表單按鈕按鈕處理
- 15. BadgeValue未在UITabBarItem上更新
- 16. 在Sencha Touch中處理單選按鈕上的事件
- 17. 在按鈕上處理tapStart事件
- 18. Windows批處理按長度分割長字符串64
- 19. 事件處理程序事件處理程序不調用上單擊按鈕
- 20. Python多處理/線程比虛擬機上的單個處理花費的時間更長
- 21. 如何處理長按媒體按鈕,以啓動活動?
- 22. Win10 UWP應用程序處理長按回退按鈕
- 23. 在Drupal表單中處理多個提交按鈕API
- 24. 如何在一個頁面上處理多個表單?
- 25. 如何在一個頁面上處理多個表單?
- 26. 在一個視圖上處理多個表單操作?
- 27. 如何在按鍵上實現上下文菜單而不是長按/輕按
- 28. Android上下文菜單長按
- 29. 如何處理多個$ _GET的長URL?
- 30. 在Android Widget上處理多個按鈕點擊
請分享您的代碼或IB。 –
我沒有開始正確的代碼。 –