【问题标题】:iOS: Swipe left/right between tabs possible?iOS:可以在标签之间向左/向右滑动吗?
【发布时间】:2017-08-10 21:51:19
【问题描述】:

是否可以在屏幕上的任意位置向左或向右滑动以切换 iOS 中的选项卡?谢谢

示例 1:只需向左/向右滑动即可在日历上切换月份 示例 2:从 0:12 开始 http://www.youtube.com/watch?v=5iX4vcsSst8

【问题讨论】:

    标签: iphone ios ipad


    【解决方案1】:

    如果您使用的是标签栏控制器,您可以在每个标签的视图上设置滑动手势识别器。当手势识别器被触发时,它可以改变tabBarController.selectedTabIndex

    此效果不会动画,但会通过滑动手势切换选项卡。当我有一个带有 UITabBar 的应用程序时,这大约是我使用的,左右两侧有按钮,并通过滑动手势来更改活动选项卡。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton:)];
        [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self.view addGestureRecognizer:swipeLeft];
    
        UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)];
        [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
        [self.view addGestureRecognizer:swipeRight];
    }
    
    - (IBAction)tappedRightButton:(id)sender
    {
        NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];
    
        [rootVC.tabBarController setSelectedIndex:selectedIndex + 1];
    } 
    
    - (IBAction)tappedLeftButton:(id)sender
    {
        NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];
    
        [rootVC.tabBarController setSelectedIndex:selectedIndex - 1]; 
    }
    

    【讨论】:

    • 所以不可能像原始帖子中的 Android youtube 示例那样制作动画?
    • 过去我创建了一个更流畅的选项卡控件,它是一个比上面的选项卡栏示例更自定义的解决方案。上次我做这样的事情时,我创建了一个非常长的视图控制器,其中包含并排放置的所有剖面视图控制器(我有 5 个部分,因此 VC 是屏幕宽度的 5 倍)。导航涉及左右滑动容器 VC。这不是最好的方法,因为所有 VC 都一直在记忆中,但它是我能得到的最好的方法,因为我需要它。不幸的是,我无法显示该代码中的任何代码 sn-ps。
    • 从 iOS7 开始,可以使用 UIPercentDrivenInteractiveTransition 和自定义视图控制器转换来实现此效果。基本上,您将拥有一个带有自定义推送/弹出动画的导航控制器来进行滑动。 UIPercentDrivenInteractiveTransition 将允许过渡作为平移而不是滑动来跟踪。
    • 什么是 tappedRightButton 和 tappedLeftButton?我不想要任何按钮,只需要像 Android 那样在页面视图中默认的滑动...
    • @Radu,我相信我从一个示例中提取了该代码,其中我在左右边缘具有滑动手势和按钮以推进选项卡。方法的命名并不重要,重要的部分是在滑动手势识别器上设置动作方法并将手势识别器添加到视图中。 iOS 和 Android 的标签栏并不相同。在 iOS 上,从一个选项卡到另一个选项卡的流畅滑动不一定是预期的行为。如果您正在寻找更多基于页面的布局,这可能不是您想要的解决方案。见UIPageViewController
    【解决方案2】:

    假设您使用的是UITabBarConroller

    您的所有子 ViewController 都可以从一个为您完成所有繁重工作的类继承。

    我就是这样做的

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

    因此,作为 UITabControllers 一部分的所有视图控制器都可以从 SwipableTabVC 继承而不是 UIViewController。

    【讨论】:

      【解决方案3】:

      试试这个,

      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedRightButton:)];
          [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
          [self.view addGestureRecognizer:swipeLeft];
      
          UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tappedLeftButton:)];
          [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
          [self.view addGestureRecognizer:swipeRight];
      }
      
      - (IBAction)tappedRightButton:(id)sender
      {
          NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];
      
          [self.tabBarController setSelectedIndex:selectedIndex + 1];
      
          //To animate use this code
          CATransition *anim= [CATransition animation];
          [anim setType:kCATransitionPush];
          [anim setSubtype:kCATransitionFromRight];
          [anim setDuration:1];
          [anim setTimingFunction:[CAMediaTimingFunction functionWithName:
                                        kCAMediaTimingFunctionEaseIn]];
          [self.tabBarController.view.layer addAnimation:anim forKey:@"fadeTransition"];
      } 
      
      - (IBAction)tappedLeftButton:(id)sender
      {
          NSUInteger selectedIndex = [rootVC.tabBarController selectedIndex];
      
          [self.tabBarController setSelectedIndex:selectedIndex - 1]; 
      
          CATransition *anim= [CATransition animation];
          [anim setType:kCATransitionPush];
          [anim setSubtype:kCATransitionFromRight];
      
          [anim setDuration:1];
          [anim setTimingFunction:[CAMediaTimingFunction functionWithName:
                                    kCAMediaTimingFunctionEaseIn]];
          [self.tabBarController.view.layer addAnimation:anim forKey:@"fadeTransition"];
      }
      

      【讨论】:

      • 谢谢,真的很有帮助。
      【解决方案4】:

      我建议将整个内容嵌入到 pageviewcontroller 中,如下所示: https://github.com/cwRichardKim/RKSwipeBetweenViewControllers

      【讨论】:

      • 很高兴检查问题 Richard KIM
      【解决方案5】:

      当然,这是可能的。

      每个屏幕都需要有一个UISwipeGestureRecognizer用于滑动,然后调用标签栏执行所需的操作。可以是任何东西,从增加或减少活动标签到你想要的任何东西。

      为了防止代码重复,您可以创建一个自定义 UIViewController 并让您的所有视图控制器都从那里继承(或其他几种方式)。

      【讨论】:

        猜你喜欢
        • 2013-02-17
        • 1970-01-01
        • 2014-10-16
        • 1970-01-01
        • 1970-01-01
        • 2011-03-29
        • 1970-01-01
        • 2018-02-01
        • 2010-10-10
        相关资源
        最近更新 更多