用户可以滑动或单击按钮在视图控制器之间导航

The user can either swipe or click the buttons to Navigate between view Controllers

我做了什么!!

1) 我可以在视图控制器(View1 和 View2)之间滑动,如下图所示

Image 1

2) 我在 ContainerViewController 中创建了 Tow 按钮,这将允许用户单击每个按钮以在这两个页面之间导航[类似于滑动,但这个按钮需要单击]

下面是我的程序在下图中的样子

Image 2

我需要什么帮助?

1) 我希望有人帮我实现这两个按钮以在这两个页面之间导航[类似于滑动]。此外,用户可以滑动或单击按钮在页面之间导航。

很高兴在这里找到愿意帮助我和其他人的人

1-ContainerViewController 我只创建了两个按钮。

2- View1 和 View2 我没有在这里做任何编码。

3- ViewSwipe 这是代码

#import "ScrollViewController.h"
#import "View1.h"
#import "View2.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    View1 * V1 = [[View1 alloc]initWithNibName:@"View1" bundle:nil];
    View2 * V2 = [[View2 alloc]initWithNibName:@"View2" bundle:nil];



    [self addChildViewController:V1];
    [self.scrollView addSubview:V1.view];
    [V1 didMoveToParentViewController:self];


    [self addChildViewController:V2];
    [self.scrollView addSubview:V2.view];
    [V2 didMoveToParentViewController:self];

    CGRect V2Frame = V2.view.frame;
    V2Frame.origin.x= self.view.frame.size.width;
    V2.view.frame = V2Frame;

    self.scrollView.contentSize=CGSizeMake(self.view.frame.size.width * 2, self.view.frame.size.height);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

试试这个代码,这样你就可以滑动标签栏了

- (void)viewDidLoad {
    [super viewDidLoad];
    UISwipeGestureRecognizer *leftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftToRightSwipeDidFire)];
    leftToRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.tabBarController.tabBar addGestureRecognizer:leftToRightGesture];

    UISwipeGestureRecognizer *rightToLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightToLeftSwipeDidFire)];
    rightToLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.tabBarController.tabBar addGestureRecognizer:rightToLeftGesture];
}


- (void)leftToRightSwipeDidFire {
    UITabBar *tabBar = self.tabBarController.tabBar;
    NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem];
    if (index > 0) {
        self.tabBarController.selectedIndex = index - 1;
    } else {
        return;
    }
}
- (void)rightToLeftSwipeDidFire {
    UITabBar *tabBar = self.tabBarController.tabBar;
    NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem];
    if (index < tabBar.items.count - 1) {
        self.tabBarController.selectedIndex = index + 1;
    } else {
        return;
    }
}

你可以像 Darji 所说的那样通过使用 UISwipeGestureRecognizer

来实现
- (void)viewDidLoad {
    [super viewDidLoad];
    UISwipeGestureRecognizer *leftToRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftToRightSwipeDidFire)];
    leftToRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [self.tabBarController.tabBar addGestureRecognizer:leftToRightGesture];

    UISwipeGestureRecognizer *rightToLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightToLeftSwipeDidFire)];
    rightToLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.tabBarController.tabBar addGestureRecognizer:rightToLeftGesture];
}


- (void)leftToRightSwipeDidFire {
    UITabBar *tabBar = self.tabBarController.tabBar;
    NSInteger index = [tabBar.items indexOfObject:tabBar.selectedItem];
    if (index > 0) {
        self.tabBarController.selectedIndex = index - 1;
    } else {
        return;
    }
}