gpt4 book ai didi

ios - 禁用 UIPageViewController 反弹

转载 作者:IT老高 更新时间:2023-10-28 11:38:30 26 4
gpt4 key购买 nike

为此搜索了很多,但还没有找到合适的解决方案。

是否可以禁用 UIPageViewController 的反弹效果并仍然使用 UIPageViewControllerTransitionStyleScroll

最佳答案

禁用 UIPageViewController 的反弹

  1. 添加<UIScrollViewDelegate>委托(delegate)给你的 UIPageViewController 的标题

  2. viewDidLoad 中将 UIPageViewController 的底层 UIScrollView 的委托(delegate)设置为其父级:

    for (UIView *view in self.view.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
    ((UIScrollView *)view).delegate = self;
    break;
    }
    }
  3. scrollViewDidScroll的实现是将contentOffset重置为原点(NOT (0,0), but (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的实现是为了处理用户在第一页从左向右快速滑动,第一页不会向左弹跳的bug场景(由于上述功能),但会因(可能)滑动速度而在右侧反弹。最后,当反弹回来时,UIPageViewController 将触发翻页到第二页(这当然不是预期的)。

    - (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);
    }
    }

swift 4.0

要放入 viewDidLoad 的代码:

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

scrollViewDidScroll的实现:

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);
}
}

关于ios - 禁用 UIPageViewController 反弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21798218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com