iOS手势操作实现图片的缩放

jopen 11年前

首先引入一个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  
</div> </div>
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  
</div> </div>
使用方法(需要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];  
</div> </div>