uiscrollview 放大和缩小无法正常工作

发布于 2025-01-07 15:47:18 字数 1828 浏览 0 评论 0原文

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

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

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

所以我做了一些研究并最终得到以下代码来启用捏合放大和缩小,

-(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];;
 }

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

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

  2. 当捏合放大时,它会左右、上下滚动图像,但仍然不会一直向下滚动到图像的末尾,但仍然比最初开始时滚动的要多。

  3. 当我缩小时,整个事情都停止了。您无法放大或缩小,它甚至不再滚动。完全锁定。

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

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

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. when the view is just loaded before any pinch the scrollview doesn't scroll all the way to the end of the image.

  2. 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.

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

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

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.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

兔姬 2025-01-14 15:47:18

我终于明白了。这个解决方案非常有效!

在您的 .h 文件中

@interface scrollViewController : UIViewController <UIScrollViewDelegate>{

      float oldScale;

      IBOutlet UIScrollView *scrollView;
      IBOutlet UIImageView  *image;
}

@end

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

在你的 .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

希望这有帮助。

I finally got it. This solution works perfectly!

in your .h file

@interface scrollViewController : UIViewController <UIScrollViewDelegate>{

      float oldScale;

      IBOutlet UIScrollView *scrollView;
      IBOutlet UIImageView  *image;
}

@end

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.

in your .m file

@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

hope this helps.

め可乐爱微笑 2025-01-14 15:47:18

以下是我如何实现滚动视图以在 viewDidLoad 中滚动大图像

UIImage *img = [UIImage imageNamed:@"image_name.png"];
largeImageView = [[UIImageView alloc] initWithImage:img];

[self.view insertSubview: largeImageView atIndex:0];
[(UIScrollView *)self.view setContentSize:[img size]];
[(UIScrollView *)self.view setMaximumZoomScale:2.2];
[(UIScrollView *)self.view setMinimumZoomScale:0.3];
[(UIScrollView *)self.view setCanCancelContentTouches:YES];
[(UIScrollView *)self.view setBounces:YES];
[mapImageView setUserInteractionEnabled:YES];

然后我还有 viewForZoomingInScrollView 方法:

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

此时,不需要使用scrollViewDidZoom 函数。我实现了类似的方法,以在整个图像可见时保持图像居中,但现在如果我是你,我就不会担心这一点。

Here is how I implement a scroll view to scroll around a big image in viewDidLoad

UIImage *img = [UIImage imageNamed:@"image_name.png"];
largeImageView = [[UIImageView alloc] initWithImage:img];

[self.view insertSubview: largeImageView atIndex:0];
[(UIScrollView *)self.view setContentSize:[img size]];
[(UIScrollView *)self.view setMaximumZoomScale:2.2];
[(UIScrollView *)self.view setMinimumZoomScale:0.3];
[(UIScrollView *)self.view setCanCancelContentTouches:YES];
[(UIScrollView *)self.view setBounces:YES];
[mapImageView setUserInteractionEnabled:YES];

Then I also have the viewForZoomingInScrollView method:

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

You scrollViewDidZoom function is unnecessary at this point. I implement something similar to keep the image centered when the entire image is visible, but right now I wouldn't worry about that if I were you.

疏忽 2025-01-14 15:47:18

同样的问题,但有一个简单的解决方法,只需将图像视图设置为启用用户交互即可:

[self.imageView setUserInteractionEnabled:YES];

Same issue but got a simple workaround, just set the image view to user interaction enabled:

[self.imageView setUserInteractionEnabled:YES];
白况 2025-01-14 15:47:18

对我来说,问题是我正在使用自动布局,并且没有为 UIScrollView 设置缩放委托。我使用的是自定义 UIViewController,它也是 UIScrollViewDelegate。我发现您可以在代码中设置委托:

func viewDidLoad() { scrollView.delegate = self }

或者在情节提要中通过选择 UIScrollView 设置委托,然后在连接检查器中拖动委托以指向 UIViewController。

The issue for me was I was using auto-layout and it wasn't setting the zoom delegate for the UIScrollView. I was using a custom UIViewController that was also a UIScrollViewDelegate. I found you can either in code set the delegate:

func viewDidLoad() { scrollView.delegate = self }

Or in the storyboard set the delegate by select the UIScrolView and in the connections inspector drag the delegate to point to the UIViewController.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文