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