0

我正在开发一个小应用程序,在该应用程序上我有一个 UIScrollView - 在其中,一个图像,两倍宽,显示地图。UIScrollView 是在 Storyboard Editor 中制作的,图像是动态加载的。

它部分工作 - 我可以平移、缩放和检测点击,但我需要它返回 x/y 坐标,反映触摸位置。

这是我的代码的相关部分:

    UIImage *worldMapImage = [UIImage imageNamed:@"worldmapBig.png"];
    imageView = [[UIImageView alloc] initWithImage:worldmapImage];
    imageView.userInteractionEnabled = YES;
    imageView.multipleTouchEnabled = YES;

    [_worldmap addSubview:imageView];
    _worldmap.userInteractionEnabled = YES;
    [_worldmap setContentSize:[worldMapImage size]];
    [_worldmap setMinimumZoomScale:.5];
    [_worldmap setMaximumZoomScale:1.0];
    [self performSelectorOnMainThread:@selector(didLoadImageInBackground:) withObject:imageView waitUntilDone:YES]; // After loading, initially zoom out

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    [imageView addGestureRecognizer:tapGesture];
}

-(void)tapDetected:(UIGestureRecognizer*)recognizer{
    UIView *myView = [recognizer view]; <--
CGPoint touch = [recognizer locationInView:myView]; <--
NSLog(@"%f", touch.x);
}

我搜索了很多网页,解决了类似的问题 - 还尝试用按钮替换图像,使用地图作为背景。它返回坐标,但是我不能再平移/缩放了;无法在同一代码中同时获得坐标和平移/缩放功能。

你能帮帮我吗?我已经卡了好几天了...

更新:事件处理程序已更改 - 现在可以工作了:-)

4

1 回答 1

1

也许试试这个。

-[UIGestureRecognizer locationInView:] 方法计算手势中所有触摸点的中心点。

于 2013-10-01T19:41:08.810 回答