iOS:可能在标签之间向左/向右滑动?

use*_*272 15 iphone ipad ios

是否可以在屏幕上的任何位置向左或向右滑动以切换iOS中的标签?谢谢

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

Sea*_*dek 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]; 
}
Run Code Online (Sandbox Code Playgroud)

  • 从iOS7开始,可以使用带有自定义视图控制器转换的`UIPercentDrivenInteractiveTransition`来实现此效果.基本上你会有一个带有自定义推/动画动画的导航控制器来进行滑动.UIPercentDrivenInteractiveTransition将允许转换为平移而不是滑动. (3认同)

Ali*_*Ali 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"];
}
Run Code Online (Sandbox Code Playgroud)


Kau*_*eya 6

假设您正在使用 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,所有属于UITabControllers的视图控制器都可以继承SwipableTabVC而不是UIViewController。


DBD*_*DBD 1

当然,这是可能的。

每个屏幕都需要有一个UISwipeGestureRecognizer用于滑动的按钮,然后调用选项卡栏来执行所需的操作。这可以是从增加或减少活动选项卡到您想要的任何内容。

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