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

标签: ios
2486人阅读 评论(0) 收藏 举报
分类:

首先引入一个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];




1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2869823次
    • 积分:20062
    • 等级:
    • 排名:第402名
    • 原创:272篇
    • 转载:15篇
    • 译文:14篇
    • 评论:1131条
    不再维护
    各位实在抱歉,不再继续维护 CSDN 博客。 新博客地址: http://blog.callmewhy.com 感谢 CSDN 陪我走过的三年时光。
    博客专栏
    文章分类
    最新评论