如果你感觉累,那就对了那是因为你在走上坡路。。这句话似乎有点道理的样子,时常提醒自己无论走到哪都不要忘记自己当初为什么出发。有时想想感觉有的东西可以记录一下,就把它记录下来吧,这次想写一下关于单张图片点击全屏预览的问题,网上查了一些大神写的有的功能确实很强大但自己暂时想要的只是简单的功能就好,还有些方法自己也没弄出想要的效果,最后写了一个比较简单的点击单张图片的全屏预览和双指捏合缩小放大,可能有时要对图片做一些处理,这里放大后只是显示同一张图片并未做处理,下面直接贴出代码
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 }
最后同样带上一张图片吧,大致是这样子
新闻热点
疑难解答