iOS-图片的预览功能(放大,缩小,等手势)的轮子

关于图片的预览在很多的地方都可以用到,去年做的一个商城类的项目中就仿照淘宝预览图片效果写了一个对于UIWebview上图片的预览功能的工具类,其实就是一个view,使用起来也很方便,前两天做项目时也又有用到,在此分享给有需要的人,以下是代码:

#import <UIKit/UIKit.h>
@protocol PhotoShowViewDelegate;
@interface PhotoShowView : UIView
@property(nonatomic,weak,nullable)id<PhotoShowViewDelegate> delegate;

@property(nonatomic,assign)BOOL enableTapTwo;       //是否允许双击,默认YES
@property(nonatomic,assign)BOOL enableRotation;     //是否允许旋转,默认YES
@property(nonatomic,assign)BOOL enablePinch;        //是否允许绽放,默认YES
@property(nonatomic,assign)BOOL enablePan;          //是否允许平移,默认YES
@property(nonatomic,assign)BOOL enableReset;        //是否自动复位,默认YES
@property(nonatomic,assign)NSInteger panFingerNum;  //平移时用的手指数,默认为2
@property(nonatomic,assign)CGFloat minScale;      //最小绽放比例,默认0.3
@property(nonatomic,assign)CGFloat maxScale;      //最大绽放比例,默认5.0
@property(nonatomic,assign)CGFloat doubleTapScale;//双击时的绽放比例,默认为2;
@property(nonatomic,strong,nullable)UIImageView* imageView; //要显示的图片
-(nonnull instancetype)initWithFrame:(CGRect)frame NS_DEPRECATED_IOS(2_0, 3_0);
-(nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder NS_DEPRECATED_IOS(2_0, 3_0);
-(void)resetTransform;
@end
@protocol PhotoShowViewDelegate <NSObject>
@optional
-(void)photoShowViewTouchBegan:(nullable PhotoShowView*)psv;
-(void)photoShowViewTouchEnd:(nullable PhotoShowView*)psv;
-(void)photoShowViewPhotoReduction:(nullable PhotoShowView*)psv;//相片被还原时的回调
@end
#import "PhotoShowView.h"
@interface PhotoShowView ()<UIGestureRecognizerDelegate>{
    UITapGestureRecognizer *_gesTapTwo;
    UIRotationGestureRecognizer *_gesRotation;
    UIPinchGestureRecognizer *_gesPinch;
    UIPanGestureRecognizer *_gesPan;
    
    BOOL _hadGesEndPan;
    BOOL _hadGesEndPinch;
    BOOL _hadGesEndRotation;
    
    BOOL _animating;
    
    CGFloat _scale;
    CGFloat _theScale;
    CGFloat _rotation;
    CGFloat _theRotation;
    CGFloat _pointX;
    CGFloat _thePointX;
    CGFloat _pointY;
    CGFloat _thePointY;
    
    BOOL _touchBegin;
    BOOL _hadAfterReset;
}
@end
@implementation PhotoShowView
-(instancetype)init{
    self=[super init];
    if (self){
        [self initializeVariable];
        [self createUI];
        [self addUI];
    }
    return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
    return [super initWithFrame:frame];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    return [super initWithCoder:aDecoder];
}
-(void)initializeVariable{
    _theTag=0;
    _theTagString=@"";
    _theTagUserInfo=[NSMutableDictionary dictionaryWithCapacity:0];
    
    _enableTapTwo=YES;
    _enableRotation=YES;
    _enablePinch=YES;
    _enablePan=YES;
    _enableReset=YES;
    _panFingerNum=2;
    _minScale=0.2;
    _maxScale=5.0;
    _doubleTapScale=2.0;
    
    [self initializeTouch];
    
    _animating=NO;
    
    _hadGesEndPan=YES;
    _hadGesEndPinch=YES;
    _hadGesEndRotation=YES;
    
    _touchBegin=NO;
    _hadAfterReset=NO;
}
-(void)initializeTouch{
    _scale=1.0;
    _theScale=1.0;
    _rotation=0.0;
    _theRotation=0.0;
    _pointX=0;
    _thePointX=0;
    _pointY=0;
    _thePointY=0;
}
-(void)createUI{
    _imageView=[[UIImageView alloc] init];
    _imageView.userInteractionEnabled=NO;
    _imageView.backgroundColor=[UIColor clearColor];
    _imageView.contentMode=UIViewContentModeScaleAspectFit;
    
    _gesTapTwo=[[UITapGestureRecognizer alloc] init];
    [_gesTapTwo addTarget:self action:@selector(gesBeTouchInPhotoShowView:)];
    _gesTapTwo.numberOfTapsRequired=2;
    _gesTapTwo.delegate=self;
    
    _gesRotation=[[UIRotationGestureRecognizer alloc] init];
    [_gesRotation addTarget:self action:@selector(gesBeTouchInPhotoShowView:)];
    _gesRotation.delegate=self;
    
    _gesPinch=[[UIPinchGestureRecognizer alloc] init];
    [_gesPinch addTarget:self action:@selector(gesBeTouchInPhotoShowView:)];
    _gesPinch.delegate=self;
    
    _gesPan=[[UIPanGestureRecognizer alloc] init];
    [_gesPan addTarget:self action:@selector(gesBeTouchInPhotoShowView:)];
    _gesPan.delegate=self;
    _gesPan.minimumNumberOfTouches=_panFingerNum;
}
-(void)addUI{
    [self addSubview:_imageView];
    
    [self addGestureRecognizer:_gesTapTwo];
    [self addGestureRecognizer:_gesRotation];
    [self addGestureRecognizer:_gesPinch];
    [self addGestureRecognizer:_gesPan];
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self layoutAuto];
}
-(void)layoutAuto{
    if (!_animating) {
        _imageView.frame=self.bounds;
    }
}
#pragma mark - 手势
-(void)gesBeTouchInPhotoShowView:(UIGestureRecognizer*)ges{
    if (ges==_gesTapTwo) {
        if (_enableTapTwo) {
            if (_gesTapTwo.state==UIGestureRecognizerStateEnded) {
                if (_theScale==1.0) {
                    _theScale=_doubleTapScale;
                    _scale=1.0;
                }else{
                    _theScale=1.0;
                    _scale=1.0;
                }
                [self imageTransformWithAnimate:YES];
            }
        }
    }
    if(ges==_gesRotation){
        if (_enableRotation) {
            _hadGesEndRotation=NO;
            _rotation=_gesRotation.rotation;
            [self imageTransformWithAnimate:NO];
            if (_gesRotation.state==UIGestureRecognizerStateEnded) {
                _theRotation+=_rotation;
                _rotation=0.0;
                _hadGesEndRotation=YES;
                [self afterReduction];
            }
        }
    }
    if(ges==_gesPinch){
        if (_enablePinch) {
            _hadGesEndPinch=NO;
            _scale=_gesPinch.scale;
            [self imageTransformWithAnimate:NO];
            if (_gesPinch.state==UIGestureRecognizerStateEnded) {
                _theScale*=_scale;
                _scale=1.0;
                _hadGesEndPinch=YES;
                [self afterReduction];
            }
        }
    }
    if(ges==_gesPan){
        if (_enablePan) {
            _hadGesEndPan=NO;
            CGPoint point=[_gesPan translationInView:self];
            _pointX=point.x;
            _pointY=point.y;
            [self imageTransformWithAnimate:NO];
            if (_gesPan.state==UIGestureRecognizerStateEnded) {
                _thePointX+=_pointX;
                _thePointY+=_pointY;
                _pointX=0.0;
                _pointY=0.0;
                _hadGesEndPan=YES;
                [self afterReduction];
            }
        }
    }
    if (_gesPan.state==UIGestureRecognizerStateChanged||
        _gesPinch.state==UIGestureRecognizerStateChanged||
        _gesRotation.state==UIGestureRecognizerStateChanged) {
        
        if (!_touchBegin) {
            _touchBegin=YES;
            [self delegatePhotoShowViewTouchBegan:self];
        }
        
    }else{
        if (_touchBegin) {
            _touchBegin=NO;
            [self delegatePhotoShowViewTouchEnd:self];
        }
    }
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
#pragma mark - 图片形变
-(void)imageTransformWithAnimate:(BOOL)ani{
    _animating=YES;
    CGFloat newScale=_scale*_theScale;
    if (newScale>=_maxScale) {
        newScale=_maxScale;
        _scale=1.0;
        _theScale=_maxScale;
    }
    if (newScale<=_minScale) {
        newScale=_minScale;
        _scale=1.0;
        _theScale=_minScale;
    }
    CGAffineTransform t=CGAffineTransformMakeScale(newScale, newScale);
    t=CGAffineTransformRotate(t, _rotation+_theRotation);
    t=CGAffineTransformTranslate(t, _pointX+_thePointX, _pointY+_thePointY);
    if (ani) {
        [UIView animateWithDuration:0.26 animations:^{
            _imageView.transform=t;
        }];
    }else{
        _imageView.transform=t;
    }
//    NSLog(@"_scale==%.2f",_scale);
//    NSLog(@"_theScale==%.2f",_theScale);
//    NSLog(@"_rotation==%.2f",_rotation);
//    NSLog(@"_theRotation==%.2f",_theRotation);
//    NSLog(@"%@",NSStringFromCGAffineTransform(t));
//    NSLog(@"_imageView.frame==%@",NSStringFromCGRect(_imageView.frame));
}
-(void)afterReduction{
    if (_enableReset) {
        _hadAfterReset=NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)1000*NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
            if (_hadGesEndPan&&_hadGesEndPinch&&_hadGesEndRotation) {
                if (!_hadAfterReset) {
                    _hadAfterReset=YES;
                    [self transformToZero];
                }
                
            }
        });
    }
}
-(void)resetTransform{
    [self transformToZero];
}
-(void)transformToZero{
    [self initializeTouch];
    [self imageTransformWithAnimate:YES];
    _animating=NO;
    [self setNeedsLayout];
    [self delegatePhotoShowViewPhotoReduction:self];
}
#pragma mark - set get
-(void)setPanFingerNum:(NSInteger)panFingerNum{
    _panFingerNum=panFingerNum;
    _gesPan.minimumNumberOfTouches=_panFingerNum;
}
#pragma mark - delegate
-(void)delegatePhotoShowViewTouchBegan:(nullable PhotoShowView*)psv{
    if ([_delegate respondsToSelector:@selector(photoShowViewTouchBegan:)]) {
        [_delegate photoShowViewTouchBegan:psv];
    }
}
-(void)delegatePhotoShowViewTouchEnd:(nullable PhotoShowView*)psv{
    if ([_delegate respondsToSelector:@selector(photoShowViewTouchEnd:)]) {
        [_delegate photoShowViewTouchEnd:psv];
    }
}
-(void)delegatePhotoShowViewPhotoReduction:(nullable PhotoShowView*)psv{
    if ([_delegate respondsToSelector:@selector(photoShowViewPhotoReduction:)]) {
        [_delegate photoShowViewPhotoReduction:psv];
    }
}
@end

使用起来也很方便,就直接当做一个view来使用,内部已经实现了对图片的各种手势操作,因为当时本来就是当做自己使用的轮子,所以代码中的注释不多,大家都是成人了,就直接自己看就好了吧.

希望以上代码对大家有所帮助.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,937评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,503评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,712评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,668评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,677评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,601评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,975评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,637评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,881评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,621评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,710评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,387评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,971评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,947评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,189评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,805评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,449评论 2 342

推荐阅读更多精彩内容