菜鸟教程小白 发表于 2022-12-11 16:59:44

ios - 在 uipageviewcontroller 中禁用第一个 View 和最后一个 View 的滑动


                                            <p><p>我正在使用 <code>PageViewController</code>。当页面是第一个或最后一个时,我想禁用滑动。 </p>

<p>如果显示第一页,则应禁用向左滑动,当页面在最后一页时,应禁用向右滑动手势。</p>

<p>如果我使用以下代码,则滑动不起作用</p>

<pre><code>if(index == 0)
for (UIScrollView *view in pageViewController.view.subviews) {

      if (]) {

            view.scrollEnabled = NO;
      }
    }}
else if(index ==){
   for (UIScrollView *view in pageViewController.view.subviews) {

      if (]) {

            view.scrollEnabled = NO;
      }
    } }
</code></pre>

<p>我如何做到这一点?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您需要添加以下方法,当您滚动到 ViewController 时将调用该方法。然后您可以检查它是否在正确的索引处,然后禁用滚动。</p>

<pre><code>func pageViewController(pageViewController: UIPageViewController, willTransitionToViewControllers pendingViewControllers: ) {
    if pendingViewControllers.count &gt; 0 {
      let vc = pendingViewControllers as! ViewController
      // Check index of vc


    }
}
</code></pre>

<p>在 Objective-C 中:</p>

<pre><code>- (void)pageViewController:(UIPageViewController *)pageViewController
willTransitionToViewControllers:(NSArray&lt;UIViewController *&gt; *)pendingViewControllers {
    if ( &gt; 0) {
      ViewController *vc = pendingViewControllers
      // Check index of vc


    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 uipageviewcontroller 中禁用第一个 View 和最后一个 View 的滑动,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38376663/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38376663/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 uipageviewcontroller 中禁用第一个 View 和最后一个 View 的滑动