我已经使用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;
}
非常感谢你的帮助!
编辑您的文章,突出显示您的所有代码,然后单击文本编辑器中的代码按钮。 – 2010-06-25 17:09:57
第一步,发布时格式化您的代码。 – 2010-06-25 17:10:04