UILabel 更新缓慢

发布于 2024-12-23 02:07:58 字数 1835 浏览 1 评论 0原文

我有一个非常奇怪的问题。我正在制作一个 UIImageView 网格(当前为 16x16),每个网格上都有一个只能包含一个文本字符的 UILabel。

我已经明白了,所以当您单击它时,该文本会发生变化,当您释放它时,它会再次发生变化。

这些 UILabel 会以适当的速度正确更新。

除非我还要更新我的应用程序的“状态”UILabel(只有一个)。

这个 UILabel 明显比其他 UILabel 大,而且只有一个实例。它位于屏幕的顶部,其大小为屏幕的宽度和高度 40 像素。

现在,当我尝试 [label setText:] 这个标签时,如果我单击屏幕顶部附近的 UIImageView 之一,它会以适当的速度执行良好的操作。然而,当我点击屏幕底部的一个时,需要的时间有明显的差异。

我已经删除了用于检索 UIImageViews 的列表的任何迭代。它计算我单击 (x, y) 整数坐标的点,然后通过 [[array objectAtIndex:x] objectAtIndex:y] 从数组中选择正确的 UIImageView。

我已将其范围缩小到导致速度减慢的主要“状态”UILabel。如果我注释掉该标签的 [label setText:] 并用 NSLog 打印输出替换,则一切都会以正确的速度进行。再一次,它以 UIImageViews 的正确速度移向屏幕顶部。

有人有什么建议吗?我知道我绝对不会在这里迭代任何类型的列表,所以我有点困惑为什么这种减速只发生在网格底部。

感谢您的帮助!

一些代码:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView: imageView];
    int x = 0;
    int y = 0;
    x = touchPoint.x/(screenWidth/width);
    y = (touchPoint.y - OFFSET)/(screenHeight/height);

    if ( [self outOfBoundsWithX:x Y:y] ) {

        if ( !isMoving && lastBead != nil ) {
            [self releaseBead:lastBead];
        }

        lastPoint = CGPointZero;
        lastBead = nil;
    }
    else {

        if ( !isMoving && lastBead != nil ) {
            NSLog(@"TEST");
            [self releaseBead:lastBead];
        }
        else {
            Bead * b = [self getBeadAtX:x Y:y];
            [self releaseBead:b];
        }

    }
}

- (void) releaseBead:(Bead*)b
{
    [b setGlyph:'c'];
    if (statusText == "Test") {
        [self setStatus:"Woohoo!"];
    else {
        [self setStatus:"Text"];
    }
}

- (void) setStatus:(char*)status
{
    label.text = [NSString stringWithCString:status encoding:NSASCIIStringEncoding];
    statusText = status;
}

I've got a very strange issue. I'm making a grid of UIImageViews (currently 16x16) which each have a UILabel on them that can contain only one character of text.

I've got it so when you click on it, this text changes and when you release it changes again.

These UILabels update correctly and with the proper speed.

Unless I'm also updating the "status" UILabel (of which there is only one) for my app.

This UILabel is significantly larger than the others, and again, only has one instance. It is located at the top of the screen, and its size is the width of the screen and 40 pixels in height.

Now when I try to [label setText:] this label, it performs fine and with proper speed... if I'm clicking on one of my UIImageViews near the top of the screen. However, when I go to click on one towards the bottom of the screen, it takes a noticeable difference in time.

I've removed any iteration through lists for retrieving the UIImageViews. It calculates the point of where I've clicked into an (x, y) integer coordinate, and then selects the proper UIImageView out of an array via [[array objectAtIndex:x] objectAtIndex:y].

I have narrowed it down to the main "status" UILabel as causing the slow-down. If I comment out the [label setText:] for that label and replace with with a NSLog printout, everything goes at the correct speed. And once again, it goes at the correct speed for UIImageViews towards the top of the screen.

Does anyone have any suggestions? I know I'm definitely not iterating through any sort of list here, so I'm a bit confused as to why this slowdown only occurs towards the bottom of the grid.

Thanks for the help!

Some code:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint touchPoint = [[touches anyObject] locationInView: imageView];
    int x = 0;
    int y = 0;
    x = touchPoint.x/(screenWidth/width);
    y = (touchPoint.y - OFFSET)/(screenHeight/height);

    if ( [self outOfBoundsWithX:x Y:y] ) {

        if ( !isMoving && lastBead != nil ) {
            [self releaseBead:lastBead];
        }

        lastPoint = CGPointZero;
        lastBead = nil;
    }
    else {

        if ( !isMoving && lastBead != nil ) {
            NSLog(@"TEST");
            [self releaseBead:lastBead];
        }
        else {
            Bead * b = [self getBeadAtX:x Y:y];
            [self releaseBead:b];
        }

    }
}

- (void) releaseBead:(Bead*)b
{
    [b setGlyph:'c'];
    if (statusText == "Test") {
        [self setStatus:"Woohoo!"];
    else {
        [self setStatus:"Text"];
    }
}

- (void) setStatus:(char*)status
{
    label.text = [NSString stringWithCString:status encoding:NSASCIIStringEncoding];
    statusText = status;
}

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

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

发布评论

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

评论(2

幸福不弃 2024-12-30 02:07:58

我不太确定。唯一看起来可能有问题的一行是:

CGPoint touchPoint = [[touches anyObject] locationInView: imageView];

但这并不能解释其余的内容。导致问题的标签是否有可能在后台调用某些内容?也许走过去看看发生了什么。

另外,这可能听起来很奇怪,但这可能是模拟器问题。尝试从模拟器中删除该应用程序,然后再次运行它。

I'm not quite sure. The only line that looks like it could be an issue is:

CGPoint touchPoint = [[touches anyObject] locationInView: imageView];

but that does not explain the rest. Is it possible that the label causing an issue is calling something in the background? Maybe step through it and see what is going on.

Also, this may sound odd but it may be a simulator issue. Try deleting the application from the simulator, then run it again.

晒暮凉 2024-12-30 02:07:58

最终通过使用 NSString 的 drawAtPoint 函数解决了这个问题,该函数成功地解决了这个问题,没有任何延迟。

Ended up solving this issue by using NSString's drawAtPoint function, which managed to go through this with no lag.

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