Is there a more efficient way to do this?

Posted by garethdn on Stack Overflow See other posts from Stack Overflow or by garethdn
Published on 2012-04-02T23:13:26Z Indexed on 2012/04/02 23:28 UTC
Read the original article Hit count: 188

Filed under:
|

I'm hoping there is a better way to the following. I'm creating a jigsaw-type application and this is the current code i'm using:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == img1) {
    [self animateFirstTouch:img1 withLocation:location];
} else if ([touch view] == img2) {
    [self animateFirstTouch:img2 withLocation:location];
} else if ([touch view] == img3) {
    [self animateFirstTouch:img3 withLocation:location];
} else if ([touch view] == img4) {
    [self animateFirstTouch:img4 withLocation:location];
} else if {
......
......
} else if ([touch view] == img40) {
    [self animateFirstTouch:img40 withLocation:location];
    return;
}
}

I'm hoping that there is a better, more efficieny way to do this, rather than naming every image. I'm thinking something like, if touch view is equal to a UIImageView, then perform some task. The same for touchesEnded:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
//location of current touch
CGPoint location = [touch locationInView:self.view];
if ([touch view] == image1) {
    [self animateReleaseTouch:image1 withLocation:location];
} else if ([touch view] == image2) {
    [self animateReleaseTouch:image2 withLocation:location];
} else if ([touch view] == image3) {
    [self animateReleaseTouch:image3 withLocation:location];
} else if ([touch view] == image4) {
    [self animateReleaseTouch:image4 withLocation:location];
} else if{
......
......
} else if ([touch view] == image40) {
    [self animateReleaseTouch:image40 withLocation:location];
}
    return;
}

Any help please?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios