Reputation: 97
I have TabBar Controller with 3 ViewController added on it. Now all tabs switched properly when we click on perticular tab. I want, the same functionality on swiping the TabBar from left to right or righ to left. So please give me suggestion. How can I do this?
Upvotes: 2
Views: 3703
Reputation: 3090
All your child ViewControllers can Inherit from a class that does all the heavy lifting for you.
This is how I have done it
class SwipableTabVC : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
left.direction = .left
self.view.addGestureRecognizer(left)
let right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
right.direction = .right
self.view.addGestureRecognizer(right)
}
func swipeLeft() {
let total = self.tabBarController!.viewControllers!.count - 1
tabBarController!.selectedIndex = min(total, tabBarController!.selectedIndex + 1)
}
func swipeRight() {
tabBarController!.selectedIndex = max(0, tabBarController!.selectedIndex - 1)
}
}
Now all your viewcontrollers that are part of UITabBarController can inherit from SwipableTabVC
instead of UIViewController.
Upvotes: 0
Reputation: 9354
You can add UISwipeGestureRecognizer
to your UITabBar
And handle swipe gestures by setting according to gesture direction
- (void)viewDidLoad {
[super viewDidLoad];
UISwipeGestureRecognizer *leftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftToRightSwipeDidFire)];
leftToRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.tabBarController.tabBar addGestureRecognizer:leftToRightGesture];
UISwipeGestureRecognizer *rightToLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightToLeftSwipeDidFire)];
rightToLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.tabBarController.tabBar addGestureRecognizer:rightToLeftGesture];
}
- (void)leftToRightSwipeDidFire {
UITabBar *tabBar = self.tabBarController.tabBar;
NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem];
if (index > 0) {
self.tabBarController.selectedIndex = index - 1;
} else {
return;
}
}
- (void)rightToLeftSwipeDidFire {
UITabBar *tabBar = self.tabBarController.tabBar;
NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem];
if (index < tabBar.items.count - 1) {
self.tabBarController.selectedIndex = index + 1;
} else {
return;
}
}
Upvotes: 2
Reputation: 8782
Add 2(One for left and Other for right) swipe gesture to each view controller's view and make IBAction
- (IBAction)swipeRightAction:(UIGestureRecognizer *)sender {
UISwipeGestureRecognizerDirection directions=[(UISwipeGestureRecognizer *)sender direction];
switch (directions) {
case UISwipeGestureRecognizerDirectionRight:
indexNumberItem--;
break;
case UISwipeGestureRecognizerDirectionLeft:
indexNumberItem++;
break;
default:
break;
}
if (indexNumberItem<3) {
indexNumberItem=0;
}
if (indexNumberItem>=3) {
indexNumberItem=2;
}
self.tabBarController.selectedViewController= [self.tabBarController.viewControllers objectAtIndex:indexNumberItem];
}
Upvotes: 1