问题描述
我在PageViewController中有一系列VC,用户可以用手指从左到右导航。我需要添加按钮,这些按钮基本上执行与手指滑动相同的操作,通过VC向左或向右移动一个按钮。我怎样才能做到这一点?现在我使用这两种方法动态设置VC作为用户滑动:
I have a series of VCs in a PageViewController that a user navigates left to right through with their fingers. I need to add in buttons that essentially perform the same action as the finger swiping, moving left or right by one through the VCs . How can I do this? Right now I am using these two methods to dynamically setting the VCs as the user swipes:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController;
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController;
如果用户点击按钮,我可以做同样的事情吗?
Is there someway I can do the same thing if a user clicks a button?
推荐答案
您可以使用 setViewControllers:direction:animated:completion:编程方式使用过渡动画设置当前显示的视图控制器code>在您的页面视图控制器上。
You can programmatically set the currently displayed view controller with a transition animation using setViewControllers:direction:animated:completion:
on your page view controller.
这是一个显示具有随机背景颜色的视图控制器的示例。您可以调整此选项以使用特定的视图控制器。
Here is an example that presents view controllers with random background colors. You can adjust this to use your specific view controllers.
- (void)viewDidLoad
{
[super viewDidLoad];
self.pvc = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pvc.view.frame = CGRectInset(self.view.bounds, 200, 200);
[self.view addSubview:self.pvc.view];
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
-(UIViewController*)randomVC
{
UIViewController *vc = [[UIViewController alloc] init];
UIColor *color = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1];
vc.view.backgroundColor = color;
return vc;
}
- (IBAction)previousButtonPressed:(id)sender {
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
}
- (IBAction)nextButtonPressed:(id)sender {
[self.pvc setViewControllers:@[[self randomVC]] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
}
这篇关于iOS:UIPageViewController - 使用按钮跳转到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!