Zoom in Scroll view with UIImage

試著忘記壹切 提交于 2019-12-11 18:53:33

问题


Im trying to zoom in a scroll view with multiple images, the scroll view only scrolls vertical, i tried to implement many ways, in many examples but none seens to work. Heres the code im using to create the images and the scroll view:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    for (int i = 0; i < 6; i++)
    {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"amap1%i.png",i+1]];
        // create imageView
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, _scrollView.frame.size.height*i, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        // set scale to fill
        _imageView.contentMode = UIViewContentModeScaleToFill;
        // set image
        [_imageView setImage:image];
        // add to scrollView
        [self.scrollView addSubview:_imageView];
    }

    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, _scrollView.frame.size.height * 6);
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
}

回答1:


Looks like you'll need to do a UIScrollView (to contain all the images - horizontal scrolling) with a UIScrollView for each image (to zoom in).

There's a WWDC video which talks about how to do this (WWDC 2010 - Session 104; link to iTunes)




回答2:


If you want to implement zoom in an UIScrollView you have to do the following things

  1. Set zoomEnabled to YES
  2. Implement a scrollView delegate

Please check the Apple documentation which says:

For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.



来源:https://stackoverflow.com/questions/12073940/zoom-in-scroll-view-with-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!