UIPageViewController バウンスを無効にする 質問する

UIPageViewController バウンスを無効にする 質問する

これについてはいろいろ検索しましたが、まだ適切な解決策を見つけることができませんでした。

のバウンス効果を無効にしてUIPageViewController、 を使用することは可能ですかUIPageViewControllerTransitionStyleScroll?

ベストアンサー1

UIPageViewControllerのバウンスを無効にする

  1. <UIScrollViewDelegate>UIPageViewControllerのヘッダーにデリゲートを追加する

  2. UIPageViewController の基盤となる UIScrollView のデリゲートを親に設定しますviewDidLoad

    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            ((UIScrollView *)view).delegate = self;
            break;
        }
    }
    
  3. 実装スクロールビューがスクロールしましたcontentOffsetを原点にリセットすることです((0,0) ではなく、(bound.size.width, 0)) は、ユーザーが境界の外に手を伸ばしたときに次のように表示されます。

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        if (_currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        } else if (_currentPage == totalViewControllersInPageController-1 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
            scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        }
    }
    
  4. 最後に、scrollViewWillEndDraggingユーザーが最初のページで左から右に素早くスワイプすると、最初のページは左にバウンドせず (上記の関数のため)、スワイプの速度 (おそらく) によって右にバウンドするバグ シナリオに対処するためです。そして最後にバウンドすると、UIPageViewController は 2 番目のページへのページ フリップをトリガーします (もちろん、これは想定外です)。

    - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
        if (_currentPage == 0 && scrollView.contentOffset.x <= scrollView.bounds.size.width) {
            *targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        } else if (_currentPage == totalViewControllersInPageController-1 && scrollView.contentOffset.x >= scrollView.bounds.size.width) {
            *targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        }
    }
    

スウィフト4.0

入力するコードviewDidLoad:

for subview in self.view.subviews {
    if let scrollView = subview as? UIScrollView {
        scrollView.delegate = self
        break;
    }
}

実装スクロールビューがスクロールしました:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if (currentPage == 0 && scrollView.contentOffset.x < scrollView.bounds.size.width) {
        scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0);
    } else if (currentPage == totalViewControllersInPageController - 1 && scrollView.contentOffset.x > scrollView.bounds.size.width) {
        scrollView.contentOffset = CGPoint(x: scrollView.bounds.size.width, y: 0);
    }
}

実装scrollViewWillEndDragging:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    if (currentPage == 0 && scrollView.contentOffset.x <= scrollView.bounds.size.width) {
        targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0);
    } else if (currentPage == totalViewControllersInPageController - 1 && scrollView.contentOffset.x >= scrollView.bounds.size.width) {
        targetContentOffset.pointee = CGPoint(x: scrollView.bounds.size.width, y: 0);
    }
}

おすすめ記事