比较触摸坐标 [英] Comparing Touch Coordinates
问题描述
是否可以将用户在UIView上做出的触摸坐标与plist或txt格式的一个商店进行比较?参数看起来像这样;
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.
推荐答案
不确定是否有单线解决方案.
Not sure if there's a one-liner solution.
在UITouch实例上,locationInView:
方法返回CGPoint结构(x和y坐标,均为float类型).因此,您可以将x和y坐标存储在plist中,然后将它们与当前触摸的x和y坐标进行比较.
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.
另外,在比较坐标时,您可能希望使用两点之间的距离来确定何时有命中".
Also, when comparing the coordinates, you probably want to use the distance between the two points to determine when you have a "hit".
下面是用于加载和写入属性列表的示例代码,其中的值基于NSDictionary:
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];
}
用于计算UITouches的两个位置之间的距离的方法:
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"];
这篇关于比较触摸坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!