15

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

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

4

5 回答 5

20

如果您使用的是标签栏控制器,您可以在每个标签的视图上设置滑动手势识别器。当手势识别器被触发时,它可以改变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]; 
}
于 2012-09-21T16:24:21.017 回答
9

假设您正在使用UITabBarConroller

您所有的子 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)
    }
}

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

于 2017-08-10T13:51:19.003 回答
7

尝试这个,

- (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"];
}
于 2015-08-25T05:21:08.427 回答
3

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

于 2014-07-26T07:53:23.167 回答
1

当然,这是可能的。

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

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

于 2012-09-21T16:03:06.177 回答