【发布时间】:2017-08-10 21:51:19
【问题描述】:
是否可以在屏幕上的任意位置向左或向右滑动以切换 iOS 中的选项卡?谢谢
示例 1:只需向左/向右滑动即可在日历上切换月份 示例 2:从 0:12 开始 http://www.youtube.com/watch?v=5iX4vcsSst8
【问题讨论】:
是否可以在屏幕上的任意位置向左或向右滑动以切换 iOS 中的选项卡?谢谢
示例 1:只需向左/向右滑动即可在日历上切换月份 示例 2:从 0:12 开始 http://www.youtube.com/watch?v=5iX4vcsSst8
【问题讨论】:
如果您使用的是标签栏控制器,您可以在每个标签的视图上设置滑动手势识别器。当手势识别器被触发时,它可以改变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];
}
【讨论】:
UIPercentDrivenInteractiveTransition 和自定义视图控制器转换来实现此效果。基本上,您将拥有一个带有自定义推送/弹出动画的导航控制器来进行滑动。 UIPercentDrivenInteractiveTransition 将允许过渡作为平移而不是滑动来跟踪。
UIPageViewController。
假设您使用的是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。
【讨论】:
试试这个,
- (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"];
}
【讨论】:
我建议将整个内容嵌入到 pageviewcontroller 中,如下所示: https://github.com/cwRichardKim/RKSwipeBetweenViewControllers
【讨论】:
当然,这是可能的。
每个屏幕都需要有一个UISwipeGestureRecognizer用于滑动,然后调用标签栏执行所需的操作。可以是任何东西,从增加或减少活动标签到你想要的任何东西。
为了防止代码重复,您可以创建一个自定义 UIViewController 并让您的所有视图控制器都从那里继承(或其他几种方式)。
【讨论】: