具有多个图像的 UIView 的 PinthToZoom 和 Pan

发布于 2024-12-28 19:43:23 字数 1481 浏览 5 评论 0原文

我正在开发一个带有 UIView 的 iphone 应用程序,该应用程序有一个图像作为背景和多个以图像作为背景的按钮。该图像是一个国家的图像,按钮是该国家的地区/州。

问题是屏幕太小,无法显示所有这些数据,所以我想启用 UIView 的捏合缩放功能,这样用户可以更好地查看该国家/地区。

在我的 UIViewController 的“viewDiDLoad”内部,我添加了:

UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] 
                                             initWithTarget:self 
                                             action:@selector(twoFingerPinch:)] 
                                            autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];

并添加了twoFingerPinch 方法:

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer {
   if (recognizer.scale > minValue){
      CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
      self.view.transform = transform;
   }
}

并且它工作正常,问题是 UIView 变大但用户无法在内部导航。

因此,我将 UIView 切换为 UIScrollView,并在“twoFingerPinch”方法中添加了:

if (recognizer.state == UIGestureRecognizerStateEnded){

    float scaleForView = mLastScale;

    UIScrollView *tempScrollView=(UIScrollView *)self.view;

    int newWidth = self.view.frame.size.width * scaleForView;
    int newHeight = self.view.frame.size.height * scaleForView;

    tempScrollView.contentSize=CGSizeMake(newWidth,newHeight);

    mLastScale = 1.0;
}

但效果不佳:UISrollView 变得更大,但随着图像缩放,它并没有变大,而且也没有在捏ToZoom 开始的位置居中。

我该如何解决这个问题?

谢谢

I'm developing an iphone application with an UIView that have an image as a background and multiple buttons with image as background. The image is an image of a country and the buttons are the regions/states of that country.

The problem is that the screen is too much little to display all this data, so I want to enable pinch-to-zoom of the UIView which allows user to have a better view of the country.

Inside 'viewDiDLoad' of my UIViewController, I added:

UIPinchGestureRecognizer *twoFingerPinch = [[[UIPinchGestureRecognizer alloc] 
                                             initWithTarget:self 
                                             action:@selector(twoFingerPinch:)] 
                                            autorelease];

[[self view] addGestureRecognizer:twoFingerPinch];

and I added twoFingerPinch method:

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer {
   if (recognizer.scale > minValue){
      CGAffineTransform transform = CGAffineTransformMakeScale(recognizer.scale, recognizer.scale);
      self.view.transform = transform;
   }
}

and it work correctly, the problem is that the UIView becomes bigger but user can't navigate inside.

So I switched my UIView to a UIScrollView and inside 'twoFingerPinch' method I added:

if (recognizer.state == UIGestureRecognizerStateEnded){

    float scaleForView = mLastScale;

    UIScrollView *tempScrollView=(UIScrollView *)self.view;

    int newWidth = self.view.frame.size.width * scaleForView;
    int newHeight = self.view.frame.size.height * scaleForView;

    tempScrollView.contentSize=CGSizeMake(newWidth,newHeight);

    mLastScale = 1.0;
}

but it doesn't work well: the UISrollView becomes much bigger but it's not bigger as the image scaled and also is not centered where pinchToZoom started.

How can i solve this problem?

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文