iOS:可以在选项卡之间向左/向右滑动吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12533976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: Swipe left/right between tabs possible?
提问by user1689272
Is it possible to swipe left or right anywhere on the screen to switch tabs in iOS? Thanks
是否可以在屏幕上的任意位置向左或向右滑动以切换 iOS 中的选项卡?谢谢
Example 1: Switching between months on a calender by simply swiping left/right Example 2: start at 0:12 http://www.youtube.com/watch?v=5iX4vcsSst8
示例 1:只需向左/向右滑动即可在日历上的月份之间切换示例 2:从 0:12 开始http://www.youtube.com/watch?v=5iX4vcsSst8
回答by Sean Kladek
If you are using a tab bar controller, you could set up a swipe gesture recognizer on each tab's view. When the gesture recognizer is triggered, it can change the tabBarController.selectedTabIndex
如果您使用标签栏控制器,您可以在每个标签的视图上设置一个滑动手势识别器。当手势识别器被触发时,它可以改变tabBarController.selectedTabIndex
This effect will not be animated, but it will switch the tabs with a swipe gesture. This was approximately what I used when I had an app with a UITabBar with buttons on the left and right side and swipe gestures to change the active tab.
此效果不会动画化,但会通过滑动手势切换选项卡。当我有一个带有 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];
}
回答by Ali
Try this,
尝试这个,
- (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"];
}
回答by Kaunteya
Assuming you are using UITabBarConroller
假设您正在使用 UITabBarConroller
All your child ViewControllers can Inherit from a class that does all the heavy lifting for you.
您所有的子 ViewController 都可以从为您完成所有繁重工作的类继承。
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)
}
}
So all your viewcontrollers that are part of UITabControllers can inherit from SwipableTabVC
instead of UIViewController.
因此,作为 UITabControllers 一部分的所有视图控制器都可以继承SwipableTabVC
而不是 UIViewController。
回答by cwRichardKim
I would recommend embedding the entire thing into a pageviewcontroller like so: https://github.com/cwRichardKim/RKSwipeBetweenViewControllers
我建议将整个内容嵌入到 pageviewcontroller 中,如下所示:https: //github.com/cwRichardKim/RKSwipeBetweenViewControllers
回答by DBD
Sure, it's possible.
当然,这是可能的。
Every screen would need to have a UISwipeGestureRecognizer
for the swipes, then make a call to the tab bar perform the desired action. Which could be anything from incrementing or decrementing the active tab to anything you want.
每个屏幕都需要有一个UISwipeGestureRecognizer
用于滑动,然后调用选项卡栏执行所需的操作。这可以是从增加或减少活动选项卡到您想要的任何内容。
For the sake of code duplication prevention, you could create a custom UIViewController
and have all your view controllers inherit from there (or a couple other ways).
为了防止代码重复,您可以创建一个自定义UIViewController
并从那里继承所有视图控制器(或其他几种方式)。