【发布时间】:2010-08-23 22:12:25
【问题描述】:
我在一个 UIScrollView 上有四个 UIView(屏幕分为四分位数)
在四分位数上,我在每个四分位数上都有一些对象 (UIImageViews)。
当用户点击屏幕时,我想找到离给定CGPoint最近的物体?
有什么想法吗?
我有每个四分位数内对象的 CGPoint 和框架 (CGRect)。
更新:
(来源:skitch.com)
红色引脚是 UIImageViews。
// UIScrollView
NSLog(@" UIScrollView: %@", self);
// Here's the tap on the Window in UIScrollView's coordinates
NSLog(@"TapPoint: %3.2f, %3.2f", tapLocation.x, tapLocation.y);
// Find Distance between tap and objects
NSArray *arrayOfCGRrectObjects = [self subviews];
NSEnumerator *enumerator = [arrayOfCGRrectObjects objectEnumerator];
for (UIView *tilesOnScrollView in enumerator) {
// each tile may have 0 or more images
for ( UIView *subview in tilesOnScrollView.subviews ) {
// Is this an UIImageView?
if ( [NSStringFromClass([subview class]) isEqualToString:@"UIImageView"]) {
// Yes, here are the UIImageView details (subView)
NSLog(@"%@", subview);
// Convert CGPoint of UIImageView to CGPoint of UIScrollView for comparison...
// First, Convert CGPoint from UIScrollView to UIImageView's coordinate system for reference
CGPoint found = [subview convertPoint:tapLocation fromView:self];
NSLog(@"Converted Point from ScrollView: %3.2f, %3.2f", found.x, found.y);
// Second, Convert CGPoint from UIScrollView to Window's coordinate system for reference
found = [subview convertPoint:subview.frame.origin toView:nil];
NSLog(@"Converted Point in Window: %3.2f, %3.2f", found.x, found.y);
// Finally, use the object's CGPoint in UIScrollView's coordinates for comparison
found = [subview convertPoint:subview.frame.origin toView:self]; // self is UIScrollView (see above)
NSLog(@"Converted Point: %3.2f, %3.2f", found.x, found.y);
// Determine tap CGPoint in UIImageView's coordinate system
CGPoint localPoint = [touch locationInView:subview];
NSLog(@"LocateInView: %3.2f, %3.2f",localPoint.x, localPoint.y );
//Kalle's code
CGRect newRect = CGRectMake(found.x, found.y, 32, 39);
NSLog(@"Kalle's Distance: %3.2f",[self distanceBetweenRect:newRect andPoint:tapLocation]);
}
调试控制台
这就是问题所在。每个瓷砖是 256x256。第一个 UIImageView 的 CGPoint 转换为 UIScrollView 的坐标系 (53.25, 399.36) 应该与 tapPoint (30,331) 保持一致。为什么不一样??点击点右侧的另一个点正在计算更近(距离)??
<CALayer: 0x706a690>>
[207] TapPoint: 30.00, 331.00
[207] <UIImageView: 0x7073db0; frame = (26.624 71.68; 32 39); opaque = NO; userInteractionEnabled = NO; tag = 55; layer = <CALayer: 0x70747d0>>
[207] Converted Point from ScrollView: 3.38, 3.32
[207] Converted Point in Window: 53.25, 463.36
[207] Converted Point: 53.25, 399.36 *** Looks way off!
[207] LocateInView: 3.38, 3.32
[207] Kalle's Distance: 72.20 **** THIS IS THE TAPPED POINT
[207] <UIImageView: 0x7074fb0; frame = (41.984 43.008; 32 39); opaque = NO; userInteractionEnabled = NO; tag = 55; layer = <CALayer: 0x7074fe0>>
[207] Converted Point from ScrollView: -11.98, 31.99
[207] Converted Point in Window: 83.97, 406.02
[207] Converted Point: 83.97, 342.02
[207] LocateInView: -11.98, 31.99
207] Kalle's Distance: 55.08 ***** BUT THIS ONE's CLOSER??????
【问题讨论】:
-
这基本上需要大量的数学运算。你的 UIImageView 大小一样吗?
-
是的,他们是。 CGRect 26.624 71.68 32 39。大小都一样。
-
添加了 Image 和 Kalle 代码的更新。当我将 UIImageView 的 CGPoint 转换为 UIScrollViews 坐标系时,发生了一些奇怪的事情,这会抛出坐标和 Kalle 的代码。谁能看到我错过了什么?
-
嗯。对于初学者,尝试在 distanceBetweenRect:andPoint: 中添加一个 NSLog 来打印出正在比较的两个点?即: NSLog(@"计算 %f,%f 和 %f,%f. 之间的距离。", point.x,point.y, nearest.x,closest.y);就在回报之上。如果这看起来很奇怪并且输入是正确的,那么我的答案一定搞砸了:O
-
我尝试将您的 tapLocation 和框架手动放入,并添加了调试信息。我得到:计算 30.000000,332.000000 和 30.000000,110.680000 之间的距离。 || distance => 221.320007 -- 对我来说看起来是正确的。