2010-06-25 38 views
1

我已经使用Objective-C创建了我的第一批应用程序之一。作为一个noob,我有很多想做的事情,但不知道如何将它应用到Objective-C中。请看下面的方法(我从头开始创建)并告诉我如何让它变得更好。显然,我已经在2个UILabels中重复了代码,但我想简化它(我讨厌重复代码),但我不知道最好的方法是什么。我只是需要的建议,这将有助于我更好地理解做的东西在Objective-C的正确途径.Net开发人员是Objective-C的新成员。需要一些批评和建议

timeText和dateText是类型的UILabel

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    if (isRearranging) 
    { 
     NSLog(@"touchesMoved"); 
     NSLog(@"touches=%@,event=%@",touches,event); 
     //TOUCH INFO 
     UITouch *touch = [[touches allObjects] objectAtIndex:0]; 
     CGPoint currentLocation = [touch locationInView:touch.view]; 
     CGPoint previousLocation = [touch previousLocationInView:touch.view]; 
     //FRAME INFO 
     float timeHalfWidth = timeText.frame.size.width/2; 
     float timeHalfHeight = timeText.frame.size.height/2; 
     CGRect timeTextRect = CGRectMake(timeText.center.x - (timeHalfWidth), timeText.cener.y - (timeHalfHeight), timeText.frame.size.width, timeText.frame.size.height); 
     float dateHalfWidth = dateText.frame.size.width/2; 
     float dateHalfHeight = dateText.frame.size.height/2; 
     CGRect dateTextRect = CGRectMake(dateText.center.x - (dateHalfWidth), dateText.center.y - (dateHalfHeight), dateText.frame.size.width, dateText.frame.size.height); 
     //IF TIME TEXT 
     if(CGRectContainsPoint(timeTextRect,previousLocation)) 
     { 
      CGPoint item = timeText.center; 
      CGPoint diff; 
      diff.x = previousLocation.x - item.x; 
      diff.y = previousLocation.y - item.y; 
      CGPoint newLoc; 
      newLoc.x = currentLocation.x - diff.x; 
      newLoc.y = currentLocation.y - diff.y; 
      if (newLoc.x<timeHalfWidth) 
       newLoc.x = timeHalfWidth; 
      if (newLoc.y<timeHalfHeight) 
       newLoc.y = timeHalfHeight; 
      [timeText setCenter:(newLoc)]; 
     } 
     //IF DATE TEXT 
     if(CGRectContainsPoint(dateTextRect,previousLocation)) 
     { 
      CGPoint item = dateText.center; 
      CGPoint diff; 
      diff.x = previousLocation.x - item.x; 
      diff.y = previousLocation.y - item.y; 
      CGPoint newLoc; 
      newLoc.x = currentLocation.x - diff.x; 
      newLoc.y = currentLocation.y - diff.y; 
      if (newLoc.x<dateHalfWidth) 
       newLoc.x = dateHalfWidth; 
      if (newLoc.y<dateHalfHeight) 
       newLoc.y = dateHalfHeight; 
      [dateText setCenter:(newLoc)];  
     } 
    } 
    touchMoved = YES; 
} 

非常感谢你的帮助!

+0

编辑您的文章,突出显示您的所有代码,然后单击文本编辑器中的代码按钮。 – 2010-06-25 17:09:57

+1

第一步,发布时格式化您的代码。 – 2010-06-25 17:10:04

回答

4

第一步,独立于您正在使用的语言,将遵循DRY - 您的大多数代码对于两个标签都是相同的。
然后在SDK中已经具有用于命中测试的功能,例如, -hitTest:withEvent:-pointInside:withEvent:

NSArray *labels = [NSArray arrayWithObjects:timeText, dateText, nil]; 
for (UILabel *label in labels) { 
    if ([label pointInside:previousLocation withEvent:nil]) { 
     [self relocateLabel:label]; 
     break; 
    } 
} 
+0

DRY是为什么我特别提到我重复的代码。我想看到有人以正确的方式重写我的重复。你给了我我正在寻找的东西。 如果我想在该数组中使用混合类型,该怎么办?即一对UILabel和一对UIImageView。有没有办法比较类型或使用泛型? 这是不良的猜测/示例 为(在对象NSObject的* OBJ) { 如果(label.type ==的UILabel) [自relocateLabel:OBJ]; else if(label.type == UIImageView) [self relocateImage:obj]; } – Ricky 2010-06-25 22:03:09

+0

@Ricky:Objective-C消息传递是动态的,并且这两个示例都是从UIView派生的 - 所以只要你使用'UIView'中的方法使得'for(UIView * view in view)'和'-relocateView :(UIView的*)view'。 – 2010-06-25 22:12:49

1

我会回答提问者的评论提出了一个额外的问题。 Quote:

如果我想在该数组中使用混合类型该怎么办?即一对UILabel和一对UIImageView。有没有办法比较类型或使用泛型?这是一个可怜的猜测/例子(对象的NSObject * obj){if(label.type == UILabel)[self relocateLabel:obj]; else if(label.type == UIImageView)[self relocateImage:obj]; }

正如Georg Fritzsche在那里回答的那样,Objective-C消息传递是动态的。如果对象在运行时支持该消息,它将被“查询”,如果是,它将执行与消息相关的方法。方法/消息名称称为“选择器”。

如果你明确地想弄清楚对象的类,你也可以这样做。

if([view isKindOfClass:[UILabel class]]) 
{ 
    // your code here 
} 

如果你只是想弄清楚如果目标对象响应一个选择器(即实现方法):

if([view respondsToSelector:@selector(relocateView:)]) 
{ 
    // your code here 
} 

选择器从方法名省略参数本身,留下衍生冒号完好无损并紧密附加所有内容。例如,如果您有消息发送(即方法调用):[thing moveTowardsObject:door movementType:XYZ_CRAWL],则其选择器将为:moveTowardsObject:movementType:,您将使用@selector(moveTowardsObject:movementType:)获取它。

在诸如Georg发布的循环中,您通常只需检查目标对象是否响应选择器,否则将引发异常,Objective-C代码很少捕获异常作为正常代码的一部分流(相对于Python开发人员来说)。

相关问题