如果用户移动太快,UIPageViewController视图会搞乱 [英] UIPageViewController views messed up if user moves too quickly

查看:85
本文介绍了如果用户移动太快,UIPageViewController视图会搞乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIPageViewController ,它使用一个整数来判断它在哪个页面上。它工作正常,但如果用户快速滑动几次以进一步返回页面,整数变化的速度比视图更快,然后整个事情就会崩溃(应用程序认为它在第7页可能是显示第3页)。我究竟做错了什么?我应该用不同的方法来判断我的位置吗?感谢

I have a UIPageViewController that uses an integer to tell which page it is on. It works fine, but if the user quickly swipes several times to get to a page further back, the integer changes more quickly than the views do, and then the whole thing falls apart (the app thinks it is on page 7 when it could be displaying page 3). What am I doing wrong? Is there a different method I should use to tell where I am? Thanks.

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    if (pageNumber == 0) {
        pageNumber++;
        NSLog(@"%i", pageNumber);
        Two *two = [[Two alloc] init];
        return two;
    } else if (pageNumber == 1) {
        pageNumber++;
        NSLog(@"%i", pageNumber);
        Three *three = [[Three alloc] init];
        return three;
    } else if (pageNumber >= 2) {
        return nil;
    } else {
        return nil;
    }
}


推荐答案

问题是您将 pageNumber 视为理所当然。 UIPageViewController 只能请求下一个 viewController 来预加载它并且你已经在增加 pageNumber

The problem is that you're taking pageNumber for granted. UIPageViewController could only be asking for next viewController to preload it and you're already increasing pageNumber.

当你注意到如果用户抓住页面的边缘并开始移动它,也会发生这种情况时,你就会关闭,但是然后把它放在当前页面上。

You were close when you noticed that "this also happens if a user grabs the edge of the page and starts to move it, but then puts it and back stays on the current page."

这个方法得到 UIViewController 参数,那就是只有真实参考

This method gets UIViewController for parameter and that is the only real reference you have.

由于你只有三个页面,一种方法可以解决这个问题:

Since you only have three pages one way to solve this would be:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    if ([viewController isKindOfClass: [One class]])
    {
        Two *two = [[Two alloc] init];
        return two;
    }
    else if ([viewController isKindOfClass: [Two class]])
    {
        Three *three = [[Three alloc] init];
        return three;
    }
    else
    {
        return nil;
    }
}

注意:此代码不是最佳解决方案 - 我我只想指出你不应该只是在 viewControllerAfterViewController中增加/减少 pageNumber / viewControllerBeforeViewController:

Note: this code is not the best solution - i'm just trying to point out that you shouldn't be simply increasing/decreasing pageNumber in viewControllerAfterViewController:/viewControllerBeforeViewController:

您还可以 [[... alloc] init] viewControllers并将 UIPageViewControllerDataSource 中的viewControllers的初始化活动减少到 loadView (如果没有加载)最多。例如(缩写):

You could also [[... alloc ]init] viewControllers before all this and reduce initialization activity of viewControllers in UIPageViewControllerDataSource methods to loadView (if it's not loaded) at most. For example (abbreviated):

    ...
    if ([viewController isKindOfClass: [One class]])
    {
        //Two *two = [[Two alloc] init]; //called before and two is someone's property
        if (![two isViewLoaded]) [two loadView];
        return two;
    }
    else if ...

对于更多页面,您也可以尝试标记viewControllers(在初始化期间)。由于 UIViewController 本身没有标签属性,你可以使用 viewController.view.tag 或sublcass UIViewController 并添加一个 pageNumber 属性(这可能接近于解决方案。

For more pages you could also try tagging viewControllers (during their initialization). Since UIViewController itself doesn't have a tag property you could probably use viewController.view.tag or sublcass UIViewController and add it a pageNumber property (this would probably be close to THE solution.

这篇关于如果用户移动太快,UIPageViewController视图会搞乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆