【问题标题】:Comparing Touch Coordinates比较触摸坐标
【发布时间】:2010-12-06 21:45:59
【问题描述】:

是否可以将用户在 UIView 上制作的触摸坐标与 plist 或 txt 格式的一个商店进行比较?参数看起来像这样;

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

如果可能的话,我应该以什么格式将坐标写在列表中以及如何在程序中关联它?

在此先感谢,如果您觉得我的问题有点菜,我们深表歉意。

【问题讨论】:

标签: objective-c plist coordinates touch


【解决方案1】:

不确定是否有单线解决方案。

在 UITouch 实例上,locationInView: 方法返回一个 CGPoint 结构(x 和 y 坐标,均为浮点类型)。因此,您可以将 x 和 y 坐标存储在 plist 中,然后将它们与当前触摸的 x 和 y 坐标进行比较。

编辑: 此外,在比较坐标时,您可能希望使用两点之间的距离来确定何时“命中”。

编辑: 下面是加载和写入属性列表的示例代码,其中的值基于 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];
}

UITouch的两个位置的距离计算方法:

-(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 );
}

最后,使用属性列表中的值来确定用户是否点击了上一个位置的代码:

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"];

【讨论】:

【解决方案2】:

您可能正在寻找的函数是NSStringFromCGPoint()CGPointFromString()

但几乎可以肯定,两个触摸坐标永远不会完全相同。您几乎不应该将CGFloats== 进行比较,更不用说您从诸如手指触摸这样的模拟输入中获得的那些了。您需要比较它们是否“足够接近”。请参阅this blog 了解如何测量两点之间距离的一个很好的示例。您希望该结果小于适合您目的的某个值(epsilon 或“小数”)。

【讨论】:

  • 谢谢大家。那么除了找到两者之间的密切差异之外,是否有一种(简单的)可能的方法来比较这些坐标?我可以在保存在列表中的坐标中心(如 32x32 或 64x64 pix)中存储边界,以便当用户稍微触摸它时仍然可以检测到它吗?我不知道我是否在这里拉不可能,我只是想为此寻找一个可管理的解决方案。再次感谢。
  • 检查距离有什么不好的地方?请记住,点是双精度数(实数),而不是整数,因此您无法列出它们。但是检查距离是微不足道的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多