假設你的標籤被稱爲xLabel
,plusLabel
,yLabel
,equalsLabel
和abLabel
,你可以做這樣的事情:
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
}
這有點笨拙,但它的作品。更好的方法是將它放在一個函數中,每個參數都是一個標籤來檢查。例如:
bool isCorrect = [self checkIf:xLabel isLessThan: plusLabel isLessThan: yLabel isLessThan:equalsLabel isLessThan:abLabel];
這是假設你寫的函數返回一個布爾值。
希望這有助於!
您只會在決定標籤是否在有效位置時檢查偏移量,或者將它們「貼緊」到有效位置。爲了確定正確的順序,您應該抽象出整個概念並創建封裝了一個方程邏輯的組件。然後也是一般的整個方程。也許你可以實現一些方法來檢查你是否有一個有效的排序。根據MVC體系結構考慮問題/解決方案。 – Matt