In my app view I'm detecting if the user has touch within one of 16 defined rectangles;
I've been trying to look at ways to optimize the hit test. I thought of putting the CGRects in an array so that I don't have to do the CGRectFromString conversion in the loop but I can't figure out how to store the rect information in an array.
Can anyone offer a different solution that would be faster than my current method?
As you can see the hit rectangles are in fact all squares of the same size so maybe that fact can be used.
Thanks
Code:
-(int) whichBoxIsTouched:(CGPoint)theTouch
{
NSArray *hitBoxArray = [NSArray arrayWithObjects:
@"{{64,404},{48,48}}",
@"{{112,404},{48,48}}",
@"{{160,404},{48,48}}",
@"{{208,404},{48,48}}",
@"{{64,356},{48,48}}",
@"{{112,356},{48,48}}",
@"{{160,356},{48,48}}",
@"{{208,356},{48,48}}",
@"{{64,308},{48,48}}",
@"{{112,308},{48,48}}",
@"{{160,308},{48,48}}",
@"{{208,308},{48,48}}",
@"{{64,260},{48,48}}",
@"{{112,260},{48,48}}",
@"{{160,260},{48,48}}",
@"{{208,260},{48,48}}",
nil];
CGRect theHitRect;
for (int i = 0; i < [hitBoxArray count]; i++)
{
theHitRect = CGRectFromString([hitBoxArray objectAtIndex:i]);
if (CGRectContainsPoint(theHitRect, theTouch))
{
return i;
}
}
//if not found, return out-of-range value
return [hitBoxArray count];
}
I've been trying to look at ways to optimize the hit test. I thought of putting the CGRects in an array so that I don't have to do the CGRectFromString conversion in the loop but I can't figure out how to store the rect information in an array.
Can anyone offer a different solution that would be faster than my current method?
As you can see the hit rectangles are in fact all squares of the same size so maybe that fact can be used.
Thanks