若是你感受累,那就對了那是由於你在走上坡路。。這句話彷佛有點道理的樣子,時常提醒本身不管走到哪都不要忘記本身當初爲何出發。有時想一想感受有的東西能夠記錄一下,就把它記錄下來吧,此次想寫一下關於單張圖片點擊全屏預覽的問題,網上查了一些大神寫的有的功能確實很強大但本身暫時想要的只是簡單的功能就好,還有些方法本身也沒弄出想要的效果,最後寫了一個比較簡單的點擊單張圖片的全屏預覽和雙指捏合縮小放大,可能有時要對圖片作一些處理,這裏放大後只是顯示同一張圖片並未作處理,下面直接貼出代碼atom
1 // 2 // ViewController.m 3 // XWZoomImageView 4 // 5 // Created by xiao on 15/11/11. 6 // Copyright © 2015年 xiao. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController ()<UIScrollViewDelegate> 12 @property (weak, nonatomic) IBOutlet UIImageView *picView; 13 @property (weak, nonatomic) UIScrollView *scrollView; 14 @property (weak, nonatomic) UIImageView *lastImageView; 15 @property (nonatomic, assign)CGRect originalFrame; 16 @end 17 18 @implementation ViewController 19 20 - (void)viewDidLoad { 21 [super viewDidLoad]; 22 23 self.picView.userInteractionEnabled = YES; 24 //添加單擊手勢 25 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)]; 26 27 [self.picView addGestureRecognizer:tap]; 28 29 } 30 31 -(void)showZoomImageView:(UITapGestureRecognizer *)tap 32 { 33 if (![(UIImageView *)tap.view image]) { 34 return; 35 } 36 //scrollView做爲背景 37 UIScrollView *bgView = [[UIScrollView alloc] init]; 38 bgView.frame = [UIScreen mainScreen].bounds; 39 bgView.backgroundColor = [UIColor blackColor]; 40 UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)]; 41 [bgView addGestureRecognizer:tapBg]; 42 43 UIImageView *picView = (UIImageView *)tap.view; 44 45 UIImageView *imageView = [[UIImageView alloc] init]; 46 imageView.image = picView.image; 47 imageView.frame = [bgView convertRect:picView.frame fromView:self.view]; 48 [bgView addSubview:imageView]; 49 50 [[[UIApplication sharedApplication] keyWindow] addSubview:bgView]; 51 52 self.lastImageView = imageView; 53 self.originalFrame = imageView.frame; 54 self.scrollView = bgView; 55 //最大放大比例 56 self.scrollView.maximumZoomScale = 1.5; 57 self.scrollView.delegate = self; 58 59 [UIView animateWithDuration:0.5 animations:^{ 60 CGRect frame = imageView.frame; 61 frame.size.width = bgView.frame.size.width; 62 frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width); 63 frame.origin.x = 0; 64 frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5; 65 imageView.frame = frame; 66 }]; 67 } 68 69 -(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer 70 { 71 self.scrollView.contentOffset = CGPointZero; 72 [UIView animateWithDuration:0.5 animations:^{ 73 self.lastImageView.frame = self.originalFrame; 74 tapBgRecognizer.view.backgroundColor = [UIColor clearColor]; 75 } completion:^(BOOL finished) { 76 [tapBgRecognizer.view removeFromSuperview]; 77 self.scrollView = nil; 78 self.lastImageView = nil; 79 }]; 80 } 81 82 //返回可縮放的視圖 83 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 84 { 85 return self.lastImageView; 86 }
最後一樣帶上一張圖片吧,大體是這樣子spa