[英]The user can either swipe or click the buttons to Navigate between view Controllers
我做了什么 !!
1)我可以在視圖控制器(View1和View2)之間滑動,如下圖所示
2)我在ContainerViewController中創建了“拖曳”按鈕,該按鈕允許用戶單擊每個按鈕在這兩個頁面之間導航[類似於輕掃,但單擊此按鈕后便可以進行瀏覽]
這是我的程序的大圖,如下圖所示
我需要什么幫助?
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
您可以通過使用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;
}
}
嘗試此代碼,以便您可以滑動標簽欄
- (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.