UILabel导致其他对象在iOS上重置位置 [英] UILabel causing other objects to reset position on iOS

查看:54
本文介绍了UILabel导致其他对象在iOS上重置位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习为iPhone编程,并正在制作一个简单的球反弹游戏,其中球在平台上向上反弹.游戏正常,当球到达屏幕的顶部1/4时,球似乎向上移动(屏幕的平台向下移动,类似于Doodle Jump),但是在尝试实现得分计数器时,我遇到了问题.当球到达屏幕的顶部1/4时,我将比分加起来并运行以下两行代码:

I am learning to program for the iPhone and am making a simple ball bounce game where a ball bounces upward on platforms. The game works and the ball seemingly moves upward when it reaches the top 1/4 of the screen (the platforms on the screen move down, similar to Doodle Jump), however I am having problems when trying to implement a score counter. When the ball reaches the top 1/4 of the screen, I add to the score and run the following two lines of code:

NSString *nssScore = [NSString stringWithFormat:@"%i", score];
lblScore.text = nssScore;

lblScore是一个UILabel,并链接到视图中的文本标签.每次屏幕上文本更新时,球和所有平台都将返回其初始位置,但是球会保持其速度.逐行运行调试器时,我可以同时运行两行代码而不会出现错误,问题出在下次刷新时在屏幕上更改了文本时.主要游戏循环和帧速率使用以下代码行运行:

lblScore is a UILabel and linked to a text label in the view. Every time the text updates on the screen, the ball and all platforms return to their starting positions, however the ball maintains its velocity. When running the debugger line by line, I can run both lines of code without error, the problem lies when the text is changed on the screen in the next refresh. The main game loop and frame-rate is run with the following line of code:

[NSTimer scheduledTimerWithTimeInterval:1.0/60 
   target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];

推荐答案

这是布局约束的问题.问题实际上与标签没有任何关系,只是设置标签的值会触发重绘(或布局,我不确定确切调用哪个方法).最简单的解决方案是关闭IB中的自动布局(从屏幕右侧的最左侧选项卡).另外,您可以通过设置动画约束来移动球,而不是设置框架.

This is a problem with layout constraints. The problem doesn't actually have anything to do with the label, it's just that setting the value of the label triggers a redrawing (or layout, I'm not sure exactly which method is called). The simplest solution is to turn off auto layout in IB (from the left most tab on the right side of the screen). Alternately, you can move the ball by animating its constraints, rather than setting frames.

这篇关于UILabel导致其他对象在iOS上重置位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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