IOS: add imageview in a scrollview to have zoom

后端 未结 4 1846
走了就别回头了
走了就别回头了 2020-12-02 09:39

I want to set a UIImageView with a UIImage and put this imageview inside a UIScrollView to obtain a zoom of this image; and I want this UIIma

4条回答
  •  再見小時候
    2020-12-02 10:13

    Download this and this files. You'll need them to handle touches.

    Add to your view the scrollView delegate and declare the outlets:

     @property (nonatomic, retain) IBOutlet UIScrollView *imageScrollView;
     @property (nonatomic, retain) UIImageView *imageView;
    

    Import the downloaded file inside the screen and do:

    #import "TapDetectingImageView.h"
    
    #define ZOOM_STEP 2.0
    @interface myView (UtilityMethods)
    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;
    @end
    
    
    @implementation myView
    @synthesize imageScrollView, imageView;
    
    
    - (void)viewDidLoad
    {
    
        [super viewDidLoad];
    
        //Setting up the scrollView    
        imageScrollView.bouncesZoom = YES;
        imageScrollView.delegate = self;
        imageScrollView.clipsToBounds = YES;
    
        //Setting up the imageView
        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]];
        imageView.userInteractionEnabled = YES;
        imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin);
    
        //Adding the imageView to the scrollView as subView
        [imageScrollView addSubview:imageView];
        imageScrollView.contentSize = CGSizeMake(imageView.bounds.size.width, imageView.bounds.size.height);
        imageScrollView.decelerationRate = UIScrollViewDecelerationRateFast;
    
        //UITapGestureRecognizer set up
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
        UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)];
    
        [doubleTap setNumberOfTapsRequired:2];
        [twoFingerTap setNumberOfTouchesRequired:2];
    
        //Adding gesture recognizer
        [imageView addGestureRecognizer:doubleTap];
        [imageView addGestureRecognizer:twoFingerTap];
    
        [singleTap release];
        [doubleTap release];
        [twoFingerTap release];
    
        // calculate minimum scale to perfectly fit image width, and begin at that scale
        float minimumScale = 1.0;//This is the minimum scale, set it to whatever you want. 1.0 = default
        imageScrollView.maximumZoomScale = 4.0;
        imageScrollView.minimumZoomScale = minimumScale;
        imageScrollView.zoomScale = minimumScale;
        [imageScrollView setContentMode:UIViewContentModeScaleAspectFit];
        [imageView sizeToFit];
        [imageScrollView setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
    
    
    
    }
    
    - (void)scrollViewDidZoom:(UIScrollView *)aScrollView {
        CGFloat offsetX = (imageScrollView.bounds.size.width > imageScrollView.contentSize.width)? 
        (imageScrollView.bounds.size.width - imageScrollView.contentSize.width) * 0.5 : 0.0;
        CGFloat offsetY = (imageScrollView.bounds.size.height > imageScrollView.contentSize.height)? 
        (imageScrollView.bounds.size.height - imageScrollView.contentSize.height) * 0.5 : 0.0;
        imageView.center = CGPointMake(imageScrollView.contentSize.width * 0.5 + offsetX, 
                                       imageScrollView.contentSize.height * 0.5 + offsetY);
    }
    
    - (void)viewDidUnload {
        self.imageScrollView = nil;
        self.imageView = nil;
    }
    
    
    
    #pragma mark UIScrollViewDelegate methods
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
        return imageView;
    }
    
    #pragma mark TapDetectingImageViewDelegate methods
    
    - (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {
        // zoom in
        float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
    
        if (newScale > self.imageScrollView.maximumZoomScale){
            newScale = self.imageScrollView.minimumZoomScale;
            CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    
            [imageScrollView zoomToRect:zoomRect animated:YES];
    
        }
        else{
    
            newScale = self.imageScrollView.maximumZoomScale;
            CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
    
            [imageScrollView zoomToRect:zoomRect animated:YES];
        }
    }
    
    
    - (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer {
        // two-finger tap zooms out
        float newScale = [imageScrollView zoomScale] / ZOOM_STEP;
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];
        [imageScrollView zoomToRect:zoomRect animated:YES];
    }
    
    #pragma mark Utility methods
    
    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center {
    
        CGRect zoomRect;
    
        // the zoom rect is in the content view's coordinates. 
        //    At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds.
        //    As the zoom scale decreases, so more content is visible, the size of the rect grows.
        zoomRect.size.height = [imageScrollView frame].size.height / scale;
        zoomRect.size.width  = [imageScrollView frame].size.width  / scale;
    
        // choose an origin so as to get the right center.
        zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
        zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
        return zoomRect;
    }
    

    Done!

    Basically what this code do is to add the imageView as subview of the imageScrollView.

    Then, it adds the TapDetecting class methods to the scrollView, in order to recognize the number of taps - the pinch the user do and add zoom functionalities.

    You can set the minimumScale of the image, if you leave 1.0 the image should be displayed as-it-is (if you set it a little bit lower it's being scaled), and the maximumZoomScale, i suggest you to leave it to 4, it's fine!

    Now, you can load images programmatically from there.

    The last thing you have to do is to insert a UIScrollView inside your xib file and link it to imageScrollView. You'll have the image at the perfect center, you can double tap on it to zoom, pinch to zoom as you set up in code.

提交回复
热议问题