检查 UILabel 彼此之间的接近程度 [英] Check how close UILabels are to each other

查看:54
本文介绍了检查 UILabel 彼此之间的接近程度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个代数方程混乱的游戏.我为方程的每个组成部分分配了一个出口.例如,如果方程是:

I am trying to make a game which has a jumbled up algebra equation. I have assigned an outlet to each component of the equation. For example, if the equation was:

x2 + y = 2(a+b)

x2 + y = 2(a+b)

那么 x2(即 x 的平方)、+、y、= 和 2(a+b) 都是它自己的出口.但是这个等式会变得混乱,我希望用户将标签出口移动到正确的顺序.我已启用 touchesMoved,但我的问题在于检查方程是否按正确顺序排列.我会将代码包装到 IBAction 按钮操作中,但是如何分析文本?我会检查每个标签之间的偏移量吗?有没有一种简单的方法/API 来做到这一点?谢谢!

Then x2 (which is x squared), +, y, = and 2(a+b) would all be its own outlet. But the equation is going to be jumbled up and I want the user to move the label outlets to the correct order. I have enabled touchesMoved, but my problem lies in checking if the equation is in the correct order. I would wrap the code into an IBAction button action, but how do I analyze the text? Would I check for the offset between each label? Is there an easy way/API to do this? Thanks!

推荐答案

假设你的标签叫做 xLabel,plusLabel, yLabel, equalsLabelabLabel,你可以这样做:

Assuming your labels are called xLabel,plusLabel, yLabel, equalsLabel, and abLabel, you could do something like this:

NSUInteger xLeftBorder = CGRectGetMinX(xLabel.frame);
NSUInteger plusLeftBorder = CGRectGetMinX(plusLabel.frame);
NSUInteger yLeftBorder = CGRectGetMinX(yLabel.frame);
NSUInteger equalsLeftBorder = CGRectGetMinX(equalsLabel.frame);
NSUInteger abLeftBorder = CGRectGetMinX(abLabel.frame);

if(xLeftBorder < plusLeftBorder && plusLeftBorder < yLeftBorder && yLeftBorder < equalsLeftBorder && equalsLeftBorder < abLeftBorder){

    //Correct!
}

else{
    //Incorrect
}

这有点笨拙,但它有效.一个更好的方法是把它放在一个函数中,每个参数都是一个要检查的标签.例如:

This is kind of clumsy, but it works. An even better way to do it would be to put this in a function with each parameter being a label to check. For example:

bool isCorrect = [self checkIf:xLabel isLessThan: plusLabel isLessThan: yLabel isLessThan:equalsLabel isLessThan:abLabel];

这是假设您编写的函数返回一个布尔值.

This is assuming the function you write returns a bool.

希望这有帮助!

这篇关于检查 UILabel 彼此之间的接近程度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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