uiscrollview放大和缩小功能无法正常工作 [英] uiscrollview zoom in and zoom out doesn't work properly

查看:180
本文介绍了uiscrollview放大和缩小功能无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个uiscrollview,里面有一个uiimageview。我希望习惯能够放大图像,因为它是一个大图像。 scrollview只需要垂直滚动而不是水平滚动。

I have a uiscrollview and there is a uiimageview inside it. I want the used to be able to zoom in out of the image since it's a big image. the scrollview only needs to scroll vertically not horizontally.

在添加缩放效果之前,我有以下代码,它可以按照我想要的方式工作,

before adding the zooming effect I had the following code and it worked as I wanted,

-(void)viewDidLoad
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(320, 1690)];
}

所以我做了一些研究,最后得到以下代码来启用捏缩放进出,

So I did some research and ended up with the following code to enable pinch zoom in and out,

-(void)viewDidLoad
{
scrollView.scrollEnabled = YES;

[scrollView setContentSize:CGSizeMake(320, 1690)];
scrollView.delegate = self;
//    scrollView.contentSize = instImage.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / instImage.frame.size.width;
scrollView.maximumZoomScale = 2.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
[[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate:[NSDate distantFuture]];

 }


 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

return instImage;
}


 - (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {

CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;

// center horizontally
if (frameToCenter.size.width < boundsSize.width) {

    frameToCenter.origin.x = ((boundsSize.width - frameToCenter.size.width) / 2);

}else {

    frameToCenter.origin.x = 0;
 }

return frameToCenter;
 }


 - (void)scrollViewDidZoom:(UIScrollView *)scrollV {

instImage.frame = [self centeredFrameForScrollView:scrollV andUIView:instImage];;
 }

现在我遇到以下问题,我无法弄清楚出了什么问题,

Now I have the following problems and I can't figure out what's wrong,


  1. 当视图刚刚在任何夹点之前加载时,scrollview不会一直滚动到图像的末尾。

  1. when the view is just loaded before any pinch the scrollview doesn't scroll all the way to the end of the image.

当收缩放大时,它会左右滚动并向上和向下滚动图像,但它仍然不会一直向下滚动到最后图像,但仍然比它最初在开始时做的更多。

when pinched to zoom in, it scrolls left and right and up and down the image, but still it doesn't scroll down all the way to the end of the image, but still more than what it originally did at the begining.

当我缩小时,整个过程停止了。你无法放大或缩小,它甚至不再滚动。完全锁定。

when I zoom out, the whole thing stops. you can't zoom in or out, it doesn't even scroll anymore. totally locked.

我做了一些研究,发现了以下帖子。
https://stackoverflow.com/a/6384834/1103257

I did some reseach and found the following post. https://stackoverflow.com/a/6384834/1103257

但我不知道在哪里可以找到NSDefaultRunLoopMode,甚至不知道如何搜索它以确定是否可以解决它。

but I don't know where would I find NSDefaultRunLoopMode or even how to search for it to figure out whether this can solve it or not.

推荐答案

我终于明白了。此解决方案完美无缺!

I finally got it. This solution works perfectly!

在您的.h文件中

@interface scrollViewController : UIViewController <UIScrollViewDelegate>{

      float oldScale;

      IBOutlet UIScrollView *scrollView;
      IBOutlet UIImageView  *image;
}

@end

并将图片连接到包含要放大和缩小的照片的uiimageview,位于您的滚动视图中。然后将滚动视图连接到界面上的uiscrollview。

and just connect the image to the uiimageview that has the photo that you want to zoom in and out and is inside your scrollview. then connect the scrollview to your uiscrollview on your interface.

在.m文件中

@implementation scrollViewController


-(void)viewDidLoad
{
[super viewDidLoad];

scrollView.scrollEnabled = YES;
scrollView.minimumZoomScale=1.0;
scrollView.maximumZoomScale=4.0;
[scrollView setContentSize:CGSizeMake(320, 1700)];
scrollView.delegate=self;

 }

 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
 {
     return image;
 }

 - (void)scrollViewDidEndZooming:(UIScrollView *)scrollV withView:(UIView *)view atScale:(float)scale
 {
     [scrollView setContentSize:CGSizeMake(scale*320, scale*1700)];
 }

  @end

希望这有帮助。

这篇关于uiscrollview放大和缩小功能无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆