Comparison of touch coordinates

Is it possible to compare touch coordinates made by users in UIView to one repository in 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 link it inside the program?

Thanks in advance and sorry if you find my question a little noobie.

+2


source to share


2 answers


Not sure if there is a one line solution.

On a UITouch instance, the method locationInView:

returns a CGPoint structure (x and y coordinates, both floats). This way, you can store the x and y coordinates in your plist and then compare them with your current x and y coordinates.

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

EDIT: Below is a sample code to load and write to a property list where the values ​​are based on 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];
}

      



Method for calculating distance between two UITouches locations:

-(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 is in a 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"];

      

+5


source


The features you are probably looking for are NSStringFromCGPoint()

and CGPointFromString()

.



But the two touch coordinates will almost certainly never be accurate. You almost never compare CGFloats

to ==

, let alone what you get from analog input like finger touch. You need to compare if they are "close enough". See this blog for a good example of how to measure the distance between two points. You want this result to be less than some value (epsilon or "small number") that suits your purpose.

+3


source







All Articles