views:

860

answers:

2

Is it possible to compare touch coordinates made by the users on the UIView to the one store in a plist or txt format? The argument looks like this;

  if (user touch coordinate == touch coordinate stored in plist or text)
  then
    (do something)
  else
    (do something)

If possible in what format should i write the coordinates in the list and how to associate it inside the program?

thanks in advance and sorry if you find my question a bit noobie.

+1  A: 

Not sure if there's a one-liner solution.

On a UITouch instance, the locationInView: method returns a CGPoint struct (x and y coordinates, both of type float). So you can store the x and y coordinates in your plist, then compare them with your current touch's x and y coordinates.

EDIT: Also, when comparing the coordinates, you probably want to use the distance between the two points to determine when you have a "hit".

EDIT: Below is sample code for loading and writing to a property list, where the values are based on a NSDictionary:

- (NSMutableDictionary *)loadDictionaryFromPList: (NSString *)plistName
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSDictionary *immutableDictionary = [NSDictionary dictionaryWithContentsOfFile: plistPath];
    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary: immutableDictionary];
    return mutableDictionary;
}


- (void)saveDictionary: (NSDictionary *)mySettings toPList: (NSString *)plistName
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    [mySettings writeToFile: plistPath atomically: YES];
}

The method to calculate the distance between the two locations of the UITouches:

-(CGFloat) distanceBetween: (CGPoint) point1 and: (CGPoint)point2
{
    CGFloat dx = point2.x - point1.x;
    CGFloat dy = point2.y - point1.y;
    return sqrt(dx*dx + dy*dy );
}

And finally, the code that uses the values in the property list to determine if the user hit the previous location:

CGPoint currentTouchLocation = [currentTouch locationInView:self];

// Lookup last Touch location from plist, and handle case when current Touch matches it:
NSMutableDictionary *mySettings = [self loadDictionaryFromPList: @"MySettings"];
NSNumber *lastXCoordinate = [mySettings objectForKey:@"lastXCoordinate"];
NSNumber *lastYCoordinate = [mySettings objectForKey:@"lastYCoordinate"];
if (lastXCoordinate && lastYCoordinate)
{
    CGPoint lastTouchLocation = CGPointMake([lastXCoordinate floatValue], [lastYCoordinate floatValue]);
    CGFloat distanceBetweenTouches = [self distanceBetween: currentTouchLocation and: lastTouchLocation];
    if (distanceBetweenTouches < 25) // 25 is just an example
    {
        // Handle case where current touch is close enough to "hit" previous one
        NSLog(@"You got a hit!");
    }
}

// Save current touch location to property list:
[mySettings setValue: [NSNumber numberWithFloat: currentTouchLocation.x] forKey: @"lastXCoordinate"];
[mySettings setValue: [NSNumber numberWithFloat: currentTouchLocation.y] forKey: @"lastYCoordinate"];
[self saveDictionary:mySettings toPList: @"MySettings"];
pythonquick
thanks pythonquick. Hope you dont mind but could give me a sample how to find the difference between the two and in what format should i write my coordinates on a plist. thanks so much.
Clyde
sure - added some sample code to the answer
pythonquick
Thanks dude you rock!! im gonna try this later after work.
Clyde
+2  A: 

The functions you're probably looking for are NSStringFromCGPoint() and CGPointFromString().

But two touch coordinates will almost certainly never be the exact same. You should almost never be comparing CGFloats with ==, let alone ones you get from such an analog input as a finger touch. You need to compare whether they are "close enough." See this blog for a good example of how to measure the distance between two points. You want that result to be less than some value (epsilon, or "a small number") that is appropriate for your purposes.

Rob Napier
Thanks guys. So is there a (easy) possible way for comparing these coordinates other than finding the close difference between the two? can i store boudaries as well within the center of the coordinates (like 32x32 or 64x64 pix) that is save on the list so that when the user's touch a bit off it can still be detected compared? I dont know if im pulling the imposible here im just trying to look for a manageable solution for this. Thanks again.
Clyde
What's unmanageable about checking distance? Remember, points are doubles (real numbers), not integers, so you can't make a list of them. But checking distance is trivial.
Rob Napier