gpt4 book ai didi

iphone - 如何从缩放图像中的 UITapGestureRecognizer 获取 UIImage 的坐标?

转载 作者:行者123 更新时间:2023-12-03 19:05:04 25 4
gpt4 key购买 nike

我有一个 UIScrollView,里面有一个带有图像的 UIImageView。我希望能够缩放并且仍然能够获取 UIImage 的坐标。我的图像的坐标大于 iPhone 屏幕 600x800,使用当前方法,我只能获取 iPhone 屏幕上的坐标。如何获取 UIImage 上被点击的实际位置的坐标?

这是我到目前为止所得到的:

- (void) loadView {

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


UIImage *mapImage = [UIImage imageNamed:@"Map1.png"];
imageV = [[UIImageView alloc] initWithImage:mapImage];
//imageV.userInteractionEnabled = YES;

//[imageV addGestureRecognizer:tapGesture];

//CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
scrollV = [[UIScrollView alloc] initWithFrame:imageV.frame];
scrollV.contentSize = CGSizeMake(imageV.frame.size.width, imageV.frame.size.height);

[scrollV addGestureRecognizer:tapGesture];

[scrollV addSubview:imageV];
scrollV.userInteractionEnabled = YES;
scrollV.maximumZoomScale = 5;
scrollV.minimumZoomScale = 0.5;
scrollV.bounces = NO;
scrollV.bouncesZoom = NO;
scrollV.delegate = self;

self.view = scrollV;

}



-(void)tapDetected:(UIGestureRecognizer*)recognizer{
NSLog(@"tap detected.");
CGPoint point = [recognizer locationInView:nil];

NSLog(@"x = %f y = %f", point.x, point.y );
}

-(UIView*) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [[self.view subviews] objectAtIndex:0];
}

最佳答案

我刚刚发现应该将 uitapgesture 识别器添加到 UIImageView 中,并且当调用方法 tapDetected 时,获取位置的正确方法是为其提供正确的 View ,如下所示

CGPoint point = [recognizer locationInView:self.imageV];

然后就可以从图像中获取坐标。

关于iphone - 如何从缩放图像中的 UITapGestureRecognizer 获取 UIImage 的坐标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5362720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com