IOS: Проведите пальцем влево/вправо между вкладками?
Можно ли прокручивать влево или вправо в любом месте экрана для переключения вкладок в iOS? Благодаря
Пример 1: Переключение между месяцами на каландр путем простого прокрутки влево/вправо
Пример 2: начало в 0:12 http://www.youtube.com/watch?v=5iX4vcsSst8
Ответы
Ответ 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];
}
Ответ 2
Попробуйте это,
- (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"];
}
Ответ 3
Я бы порекомендовал внедрить всю вещь в элемент управления pageview:
https://github.com/cwRichardKim/RKSwipeBetweenViewControllers
Ответ 4
Предполагая, что вы используете 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)
}
}
Таким образом, все ваши контроллеры view, которые являются частью UITabControllers, могут наследовать от SwipableTabVC
вместо UIViewController.
Ответ 5
Конечно, возможно.
Каждый экран должен иметь UISwipeGestureRecognizer
для проверок, а затем сделать вызов на панели вкладок выполнить требуемое действие. Это может быть что угодно: от увеличения или уменьшения активной вкладки до всего, что вы хотите.
Для предотвращения дублирования кода вы можете создать пользовательский UIViewController
и унаследовать от него все ваши контроллеры вида (или пару других способов).