从图像中获取触摸坐标,同时保持平移/缩放功能

时间:2013-09-29 22:15:22

标签: ios uiimage touch uitapgesturerecognizer

我正在开发一个小应用程序,我有一个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);
}

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

你可以帮帮我吗?我已经被困了好几天......

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

1 个答案:

答案 0 :(得分:1)

或许试试这个。

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