Ios 以编程方式控制UIPageViewController的移动
我有两个按钮,可用于在Ios 以编程方式控制UIPageViewController的移动,ios,uipageviewcontroller,Ios,Uipageviewcontroller,我有两个按钮,可用于在UIPageViewController中启动页面转换。我以编程方式启动从一个页面到另一个页面的转换,如下所示: //to go left [_pageVC setViewControllers:@[[self pageViewController:_pageVC viewControllerBeforeViewController:[_pageVC.viewControllers lastObject]]] direction:
UIPageViewController
中启动页面转换。我以编程方式启动从一个页面到另一个页面的转换,如下所示:
//to go left
[_pageVC setViewControllers:@[[self pageViewController:_pageVC viewControllerBeforeViewController:[_pageVC.viewControllers lastObject]]]
direction:UIPageViewControllerNavigationDirectionReverse
animated:YES
completion:^(BOOL finished) { }];
问题在于,按钮的位置使得快速点击按钮非常容易,这会导致不必要的行为,甚至会导致应用程序崩溃。所以我想在页面转换时停用它们
为此,我创建了一个BOOL
,当动画开始时,我将其设置为YES
,但我不知道在哪里再次将其设置为NO
。上述函数中的completion块调用得太早,并且如果转换是以编程方式启动的,则不会调用pageViewController:didffinishanimating:previousViewControllers:transitionCompleted:
(来自文档:,在手势驱动的转换完成后调用
)
如何在执行转换时停用按钮?我使用了pbasdf的解决方案来禁用按钮,但它可能已经正常工作了。我意识到问题不在于按钮的禁用,而是刷卡仍然处于活动状态,导致了奇怪的行为 I在以下情况下启用/禁用按钮时禁用和启用刷卡:
添加一个超时。禁用按钮,例如0.5秒:
self.buttonNext.enabled = NO;
self.buttonPrev.enabled = NO;
[_pageVC setViewControllers:@[[self pageViewController:_pageVC viewControllerBeforeViewController:[_pageVC.viewControllers lastObject]]]
direction:UIPageViewControllerNavigationDirectionReverse
animated:YES
completion:nil];
__weak YourClass *weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
weakSelf.buttonNext.enabled = YES;
weakSelf.buttonPrev.enabled = YES;
});
您是否可以从正在转换到的视图控制器的
视图显示
中触发某些内容?我认为只有当VC完全在屏幕上时,该方法才会启动。@pbasdf是的,但我更愿意使包含的VCs实现独立于页面控制器。但是现在,你的主意是最好的。谢谢。添加超时是一个非常糟糕的解决方案,您无法知道需要多长时间。我更喜欢用VIEWDID显示的解决方案。
self.buttonNext.enabled = NO;
self.buttonPrev.enabled = NO;
[_pageVC setViewControllers:@[[self pageViewController:_pageVC viewControllerBeforeViewController:[_pageVC.viewControllers lastObject]]]
direction:UIPageViewControllerNavigationDirectionReverse
animated:YES
completion:nil];
__weak YourClass *weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
weakSelf.buttonNext.enabled = YES;
weakSelf.buttonPrev.enabled = YES;
});