iOS手勢操作實現圖片的縮放

jopen 10年前發布 | 44K 次閱讀 IOS iOS開發 移動開發

首先引入一個h文件和m文件,用來做圖片縮放的view。

MRZoomScrollView.h

    //  
    //  MRZoomScrollView  
    //  PhoneFax  
    //  
    //  Created by WHY on 13-12-3.  
    //  Copyright (c) 2013年 WHY. All rights reserved.  
    //  
    #import <UIKit/UIKit.h>  


    @interface MRZoomScrollView : UIScrollView <UIScrollViewDelegate>  
    {  
        UIImageView *imageView;  
    }  

    @property (nonatomic, strong) UIImageView *imageView;  


    @end  

MRZoomScrollView.m
    #import "MRZoomScrollView.h"  

    #define MRScreenWidth      CGRectGetWidth([UIScreen mainScreen].applicationFrame)  
    #define MRScreenHeight     CGRectGetHeight([UIScreen mainScreen].applicationFrame)  

    @interface MRZoomScrollView (Utility)  

    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center;  

    @end  

    @implementation MRZoomScrollView  

    @synthesize imageView;  

    - (id)initWithFrame:(CGRect)frame  
    {  
        self = [super initWithFrame:frame];  
        if (self) {  

            self.delegate = self;  
            self.frame = CGRectMake(0, 0, MRScreenWidth, MRScreenHeight);  

            [self initImageView];  
        }  
        return self;  
    }  

    - (void)initImageView  
    {  
        imageView = [[UIImageView alloc]init];  

        // The imageView can be zoomed largest size  
        imageView.frame = CGRectMake(0, 0, MRScreenWidth * 2.5, MRScreenHeight * 2.5);  
        imageView.userInteractionEnabled = YES;  
        [self addSubview:imageView];  

        // Add gesture,double tap zoom imageView.  
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  
                                                                                    action:@selector(handleDoubleTap:)];  
        [doubleTapGesture setNumberOfTapsRequired:2];  
        [imageView addGestureRecognizer:doubleTapGesture];  

        float minimumScale = self.frame.size.width / imageView.frame.size.width;  
        [self setMinimumZoomScale:minimumScale];  
        [self setZoomScale:minimumScale];  
    }  


    #pragma mark - Zoom methods  

    - (void)handleDoubleTap:(UIGestureRecognizer *)gesture  
    {  
        float newScale = self.zoomScale * 1.5;  
        CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];  
        [self zoomToRect:zoomRect animated:YES];  
    }  

    - (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center  
    {  
        CGRect zoomRect;  
        zoomRect.size.height = self.frame.size.height / scale;  
        zoomRect.size.width  = self.frame.size.width  / scale;  
        zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);  
        zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);  
        return zoomRect;  
    }  


    #pragma mark - UIScrollViewDelegate  

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

    - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale  
    {  
        [scrollView setZoomScale:scale animated:NO];  
    }  
    @end  

使用方法(需要SDWebImage加載網絡圖片):
MRZoomScrollView *mrzoom = [[MRZoomScrollView alloc] init];  
   mrzoom = [[MRZoomScrollView alloc]init];  
   CGRect frame = self.view.frame;  
   NSLog(@"%f",frame.size.width);  
   mrzoom.frame = frame;  
   [mrzoom.imageView setImageWithURL:[NSURL URLWithString:@"圖片的url路徑"]];  
   [self.view addSubview:mrzoom];  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!