iphone - UILabel 设置文本很慢吗?

发布于 2024-11-09 09:54:47 字数 694 浏览 0 评论 0原文

我的代码很简单。我有一个 UIScrollView,它下面有五个 UILabel。我在UIScrollView委托scrollViewDidScroll中设置的是,scrollView每移动150个像素,我就会用NSArray中的NSString更新五个UILabel。

这是代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    int offsetY = scrollView.contentOffset.Y;

    BOOL canUpdate = (offsetY % 150 == 0)?YES:NO;

    if (canUpdate) {

        int index = offsetY / 150;

        for (int i = 0;i < 5; i++) {

            UILabel *label = [labelArray objectAtIndex:i];
            label.text = [stringArray objectAtIndex:index];

        }

    } 
}

它非常慢。我的意思是,如果我滚动得非常快(假设它可能需要在停止之前多次更新标签),我可以看到滚动滞后,就像有东西阻止其移动一样。

UILabel setText 真的那么慢吗?

谢谢

My code is simple. I have a UIScrollView and below it there are five UILabel. What I set in the UIScrollView delegate scrollViewDidScroll is that Every 150 pixel that the scrollView moved, I update the five UILabel with NSStrings in an NSArray.

Here is the codes:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    int offsetY = scrollView.contentOffset.Y;

    BOOL canUpdate = (offsetY % 150 == 0)?YES:NO;

    if (canUpdate) {

        int index = offsetY / 150;

        for (int i = 0;i < 5; i++) {

            UILabel *label = [labelArray objectAtIndex:i];
            label.text = [stringArray objectAtIndex:index];

        }

    } 
}

It is quite slow. I mean, if I scroll quite quickly (let's say it may need update the labels several times before it stops), I can see the scroll lags, just like something is blocking its moving.

Is UILabel setText really that slow??

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若能看破又如何 2024-11-16 09:54:47

不,它并不慢。您应该尝试在scrollViewDidScroll委托方法中设置标签。

更新:
所以你的意思是它仍然不起作用!?据我猜测,您正在尝试检查滚动视图是否滚动了 150px,然后更新一些标签。

在您的代码中,您基本上是在检查精确的 150 px 值,因为您使用了模 (%) 运算。
滚动视图偏移量并不总是恰好位于 150、300、450 等。

No, its not slow. You should try setting your label in scrollViewDidScroll delegate method.

UPDATE:
So you mean it still doesnt work !? As far as i can guess, you are trying to check if your scroll view has scrolled 150px and then update some label.

In your code, you are basically checking for exact 150 px value, as you have used modulo (%) operation.
It will not be always that you have scrolled and the scroll view offset is exactly at 150, 300, 450, etc.

后来的我们 2024-11-16 09:54:47

不,绝对是。如果速度很慢,您将无法有效地使用 iPhone 上的联系人列表。

如果您的代码很慢,您应该查找其他减慢滚动过程的代码段。按照 @pratikshabhisikar 的建议,切换到 scrollViewDidScroll: 方法也可能有所帮助。
尝试在 scrollViewDidEndDecelerating: 中打印日志以查看它被调用了多少次。

No, absolutely. If it was slow, you wouldn't be able to efficiently use the contact list on the iPhone.

If your code is slow, you should look for other pieces of code that are slowing the scroll process. Switching to the scrollViewDidScroll: method, as suggested by @pratikshabhisikar, could also be of help.
Try printing a log in your scrollViewDidEndDecelerating: to see how many times it's been called.

虫児飞 2024-11-16 09:54:47

Pratikshabhisikar 说得对。

仅当您的 contentOffset 恰好是 150 的倍数(作为整数)时,您才更新标签之一。

(offsetY % 150) 

仅当 offsetY 能被 150 整除并且不留余数时才返回 true。因此,如果您滚动得太快,该偏移量可能会跳过“最佳点”(150 的倍数),并且您将无法获得更新。

尝试:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    int index = floor(scrollView.contentOffset.y / 150.0f);
    for (int i = 0;i < 5; i++) {

        UILabel *label = [labelArray objectAtIndex:i];
        label.text = [stringArray objectAtIndex:index];

    }
}

“floor”函数确保您在除法后将得到向下舍入的版本作为 int - 因此 0 - 150 之间的任何位置都将返回 0 等。

您可能还想缓存最后加载的索引(因为您将重新加载当前任意滚动量的文本(即仅在 index != mCurrentIndex 时才进入 for 循环。

pratikshabhisikar has it right.

You are only updating one of your labels when your contentOffset is exactly a multiple of 150 (as an integer).

(offsetY % 150) 

returns true only if offsetY is divisible by 150 and leaves no remainder. So if you are scrolling too fast, that offset could skip the 'sweet spot' (multiples of 150) and you won't get an update.

try:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    int index = floor(scrollView.contentOffset.y / 150.0f);
    for (int i = 0;i < 5; i++) {

        UILabel *label = [labelArray objectAtIndex:i];
        label.text = [stringArray objectAtIndex:index];

    }
}

The 'floor' function ensures you will get the rounded down version as an int after the division - so anywhere from 0 - 150 will return 0 etc.

You might like to cache the last index loaded too (since you will be reloading the text for any amount of scrolling at present (i.e only enter the for loop if index != mCurrentIndex.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文