ios – 禁用UIPageViewController中的反d效果

ios – 禁用UIPageViewController中的反d效果,第1张

概述参见英文答案 > Disable UIPageViewController bounce                                    7个 我已经实现了一个包含两个页面的UIPageViewController.在最右边的页面上,我可以向右滑动,并将页面向后拉,以便当我释放时,它会d回.当我向左滑动时,左侧页面上会出现同样的情况. (d跳就像当你到达safari页面的底部 参见英文答案 > Disable UIPageViewController bounce7个
我已经实现了一个包含两个页面的UIPageVIEwController.在最右边的页面上,我可以向右滑动,并将页面向后拉,以便当我释放时,它会d回.当我向左滑动时,左侧页面上会出现同样的情况. (d跳就像当你到达safari页面的底部时会发生什么)

有没有办法禁用d跳效果?谢谢!

解决方法 到目前为止,没有一个答案实际上完全有效.他们都失败的边缘案例是:

>滚动到第2页.
>使用一根手指拖到第1页.
>将第二根手指放在屏幕上,然后拖到第1页.
>提起第一根手指.
>重复,直到你已经拖过页面0.

在这种情况下,我看到的每个解决方案到目前为止,都超过了第0页的界限.核心问题是底层API被破坏,并开始报告相对于第0页的内容偏移量,而不用调用回调来让我们知道它显示不同的页面.在整个这个过程中,API仍然声称显示第1页,即使在第零页进入第-1页时,页面零.

这种设计缺陷的解决方法非常丑陋,但这里是:

@property (weak,nonatomic) UIPageControl *pageControl;@property (nonatomic,assign) BOol shouldBounce;@property (nonatomic,assign) CGfloat lastposition;@property (nonatomic,assign) NSUInteger currentIndex;@property (nonatomic,assign) NSUInteger nextIndex;- (voID)vIEwDIDLoad {    [super vIEwDIDLoad];...    self.shouldBounce = NO;    for (ID testVIEw in self.pageController.vIEw.subvIEws) {        UIScrollVIEw *scrollVIEw = (UIScrollVIEw *)testVIEw;        if ([scrollVIEw isKindOfClass:[UIScrollVIEw class]]) {            scrollVIEw.delegate = self;            // scrollVIEw.bounces = self.shouldBounce;        }    }}- (NSInteger)presentationIndexForPageVIEwController:(UIPageVIEwController *)pageVIEwController{    return (NSInteger)self.currentIndex;}- (voID)pageVIEwController:(UIPageVIEwController *)pageVIEwController willTransitionToVIEwControllers:(NSArray *)pendingVIEwControllers{    ID controller = [pendingVIEwControllers firstObject];    self.nextIndex = [vIEwControllers indexOfObject:controller];}- (voID)pageVIEwController:(UIPageVIEwController *)pageVIEwController dIDFinishAnimating:(BOol)finished prevIoUsVIEwControllers:(NSArray *)prevIoUsVIEwControllers TransitionCompleted:(BOol)completed{    if(completed) {        // At this point,we can safely query the API to ensure        // that we are fully in sync,just in case.        self.currentIndex = [vIEwControllers indexOfObject:[pageVIEwController.vIEwControllers objectAtIndex:0]];        [self.pageControl setCurrentPage:self.currentIndex];    }    self.nextIndex = self.currentIndex;}- (voID)scrollVIEwDIDScroll:(UIScrollVIEw *)scrollVIEw{    /* The iOS page vIEw controller API is broken.  It lIEs to us and tells us       that the currently presented vIEw hasn't changed,but under the hood,it       starts giving the contentOffset relative to the next vIEw.  The only       way to detect this brain damage is to notice that the content offset is       discontinuous,and pretend that the page changed.     */    if (self.nextIndex > self.currentIndex) {        /* Scrolling forwards */        if (scrollVIEw.contentOffset.x < (self.lastposition - (.9 * scrollVIEw.bounds.size.wIDth))) {            self.currentIndex = self.nextIndex;            [self.pageControl setCurrentPage:self.currentIndex];        }    } else {        /* Scrolling backwards */        if (scrollVIEw.contentOffset.x > (self.lastposition + (.9 * scrollVIEw.bounds.size.wIDth))) {            self.currentIndex = self.nextIndex;            [self.pageControl setCurrentPage:self.currentIndex];        }    }    /* Need to calculate max/min offset for *every* page,not just the first and last. */    CGfloat minXOffset = scrollVIEw.bounds.size.wIDth - (self.currentIndex * scrollVIEw.bounds.size.wIDth);    CGfloat maxXOffset = (([vIEwControllers count] - self.currentIndex) * scrollVIEw.bounds.size.wIDth);    NSLog(@"Page: %ld NextPage: %ld X: %lf MinOffset: %lf MaxOffset: %lf\n",(long)self.currentIndex,(long)self.nextIndex,(double)scrollVIEw.contentOffset.x,(double)minXOffset,(double)maxXOffset);    if (!self.shouldBounce) {        CGRect scrollBounds = scrollVIEw.bounds;        if (scrollVIEw.contentOffset.x <= minXOffset) {            scrollVIEw.contentOffset = CGPointMake(minXOffset,0);            // scrollBounds.origin = CGPointMake(minXOffset,0);        } else if (scrollVIEw.contentOffset.x >= maxXOffset) {            scrollVIEw.contentOffset = CGPointMake(maxXOffset,0);            // scrollBounds.origin = CGPointMake(maxXOffset,0);        }        [scrollVIEw setBounds:scrollBounds];    }    self.lastposition = scrollVIEw.contentOffset.x;}- (voID)scrollVIEwWillEndDragging:(UIScrollVIEw *)scrollVIEw withVeLocity:(CGPoint)veLocity targetContentOffset:(inout CGPoint *)targetContentOffset{    /* Need to calculate max/min offset for *every* page,not just the first and last. */    CGfloat minXOffset = scrollVIEw.bounds.size.wIDth - (self.currentIndex * scrollVIEw.bounds.size.wIDth);    CGfloat maxXOffset = (([vIEwControllers count] - self.currentIndex) * scrollVIEw.bounds.size.wIDth);    if (!self.shouldBounce) {        if (scrollVIEw.contentOffset.x <= minXOffset) {            *targetContentOffset = CGPointMake(minXOffset,0);        } else if (scrollVIEw.contentOffset.x >= maxXOffset) {            *targetContentOffset = CGPointMake(maxXOffset,0);        }    }}

基本上,它记录每个滚动事件的偏移量.如果滚动位置与滚动方向相反的方向移动了一个不可能的距离(我任意挑选屏幕宽度的90%),代码假定iOS正在向我们说谎,并且表现得像转换正确完成,将偏移量视为相对于新页面而不是旧页面.

总结

以上是内存溢出为你收集整理的ios – 禁用UIPageViewController中的反d效果全部内容,希望文章能够帮你解决ios – 禁用UIPageViewController中的反d效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1113071.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存