2015-08-05 3 views
1

У меня есть контроллер TabBar с добавленным 3 ViewController. Теперь все вкладки переключаются должным образом, когда мы нажимаем на вкладку. Я хочу, та же самая функциональность при прокрутке TabBar слева направо или справа налево. Так что, пожалуйста, дайте мне предложение. Как я могу это сделать?Swipe viewControllers на контроллере TabBar в iOS

ответ

1

Вы можете добавить UISwipeGestureRecognizer к вашему UITabBar

и обрабатывать салфетки жесты, установив в соответствии с направлением жеста

- (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; 
    } 
} 
+0

Я попробовал, но все еще не работает –

+0

Вы правы, я забыл, что нам нужно управлять UITabBarController вместо панели вкладок. См. Обновленный ответ –

+0

Я думаю, что это не обнаружение моего жеста –

1

Добавить 2 (один для левого и других правого) салфетки жест для просмотра каждого контроллера представления и сделать 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]; 


} 
0

Весь ваш ребенок ViewControllers может наследовать от класса, который выполняет весь тяжелый подъем для тебя.

Это, как я сделал это

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) 
    } 
} 

Теперь все viewcontrollers, которые являются частью UITabBarController могут наследовать от SwipableTabVC вместо UIViewController.

Смежные вопросы