views:

77

answers:

1

I've created one of my first apps using Objective-C. Being a noob, there's a lot of stuff I want to do, but don't know how to apply it in Objective-C. Please take a look at the method below (which I created from scratch) and tell me what you would do to make it better. Obviously I've duplicated code across 2 UILabels, but I'd like to simplify that (I hate duplicating code) but I'm unaware what the best way to do it is. I just need suggestions which will help me better understand the right way to do stuff in Objective-C

timeText and dateText are of type 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;
}

Thanks so much for your help!

+2  A: 

A first step, independent from the language you are working in, would be to follow DRY - most of your code is the same for both labels.
Then there is already functionality for hit-testing in the SDK, e.g. -hitTest:withEvent: or -pointInside:withEvent::

NSArray *labels = [NSArray arrayWithObjects:timeText, dateText, nil];
for (UILabel *label in labels) {
    if ([label pointInside:previousLocation withEvent:nil]) {
        [self relocateLabel:label]; 
        break;
    }
}
Georg Fritzsche
DRY is why I specifically mentioned that I duplicated code. I wanted to see someone rewrite my duplication in the correct way. You gave me exactly what I was looking for.What if I wanted to use mixed types in that array? i.e. a couple UILabel and a couple UIImageView. Is there a way to compare types or use generics?This is a poor guess/example for (NSObject *obj in objects) { if (label.type==UILabel) [self relocateLabel:obj]; else if (label.type==UIImageView) [self relocateImage:obj]; }
Ricky
@Ricky: Objective-C message passing is dynamic and both examples derive from `UIView` - so as long as you only use methods from `UIView` make that `for(UIView *view in views)` and `-relocateView:(UIView*)view`.
Georg Fritzsche