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

时间:2016-04-28 21:42:51

标签: ios objective-c uiscrollview swipe

我做了什么!!

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

2 个答案:

答案 0 :(得分:2)

你可以通过使用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;
    }
}

答案 1 :(得分:0)

尝试此代码,以便您可以滑动标签栏

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