如果你感覺累,那就對了那是因為你在走上坡路。。這句話似乎有點道理的樣子,時常提醒自己無論走到哪都不要忘記自己當初為什么出發。有時想想感覺有的東西可以記錄一下,就把它記錄下來吧,這次想寫一下關于單張圖片點擊全屏預覽的問題,網上查了一些大神寫的有的功能確實很強大但自己暫時想要的只是簡單的功能就好,還有些方法自己也沒弄出想要的效果,最后寫了一個比較簡單的點擊單張圖片的全屏預覽和雙指捏合縮小放大,可能有時要對圖片做一些處理,這里放大后只是顯示同一張圖片并未做處理,下面直接貼出代碼
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 @end17 18 @implementation ViewController19 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 *)tap32 {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 *)tapBgRecognizer70 {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 *)scrollView84 {85 return self.lastImageView;86 }
最后同樣帶上一張圖片吧,大致是這樣子
新聞熱點
疑難解答