简体   繁体   English

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

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

What I have Done !! 我做了什么 !!

1) I can swipe between view controllers(View1,and View2 ) as showing in the image below 1)我可以在视图控制器(View1和View2)之间滑动,如下图所示

Image 1 图片1

2) I Created Tow button in ContainerViewController That will allow The user to Click each button to navigate between these two pages [Similar to the swipe but this one with button click] 2)我在ContainerViewController中创建了“拖曳”按钮,该按钮允许用户单击每个按钮在这两个页面之间导航[类似于轻扫,但单击此按钮后便可以进行浏览]

Here's The Big Picture how my Program Looks like in the image below 这是我的程序的大图,如下图所示

Image 2 图片2

What help do I need ? 我需要什么帮助?

1) I want someone to help me to Implement these Two button to navigate between these two pages [Similar to the swipe ]. 1)我希望有人帮助我实现这两个按钮,以在这两个页面之间导航[类似于滑动]。 In addition, The user can either swipe or click the buttons to Navigate between pages. 此外,用户可以滑动或单击按钮在页面之间导航。

It will be my pleasure to find someone here who's willing to help me and others 我很高兴在这里找到愿意帮助我和其他人的人

1-ContainerViewController I just only created two buttons. 1-ContainerViewController我只创建了两个按钮。

2- View1 and View2 I have not done any coding here. 2-View1和View2我在这里没有做任何编码。

3- ViewSwipe Here's The code 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

You can achieve that as Darji said by using UISwipeGestureRecognizer 您可以通过使用UISwipeGestureRecognizer达到Darji所说的

- (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;
    }
}

try this code so you can swipe the tab bar 尝试此代码,以便您可以滑动标签栏

- (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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM