首页 > 移动平台 > 详细

IOS 单张图片全屏预览

时间:2015-11-13 18:30:40      阅读:349      评论:0      收藏:0      [点我收藏+]

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

 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 @end
18 
19 @implementation ViewController
20 
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 *)tap
33 {
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 *)tapBgRecognizer
71 {
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 *)scrollView
85 {
86     return self.lastImageView;
87 }

最后同样带上一张图片吧,大致是这样子

技术分享

IOS 单张图片全屏预览

原文:http://www.cnblogs.com/Lingchen-start/p/4962852.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!