Check how close UILabels are to each other

旧街凉风 提交于 2019-12-08 13:01:49

问题


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)

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!


回答1:


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.

Hope this helped!



来源:https://stackoverflow.com/questions/10957698/check-how-close-uilabels-are-to-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!