专栏首页DannyHoo的专栏iOS开发中简单的图片浏览器

iOS开发中简单的图片浏览器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/53010697

.m文件:

//

//  ImageBrowserView.m

//  dipai

//

//  Created by 梁森 on 16/11/2.

//  Copyright © 2016年 梁森. All rights reserved.

//

#import "ImageBrowserView.h"

#import "UIImageView+WebCache.h"

@implementation ImageBrowserView

- (instancetype)initWithImageArr:(NSArray *)imags andTag:(NSInteger)index{

if (self == [super init]) {

        [self setUpChildControlWithArr:imags andTag:index];

    }

return self;

}

- (void)setUpChildControlWithArr:(NSArray *)images andTag:(NSInteger)index{

_imageArr = images;

// 装滚动视图的滚动视图

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    scrollView.delegate = self;

    scrollView.scrollEnabled = YES;

    scrollView.pagingEnabled = YES;

    scrollView.backgroundColor = [UIColor blackColor];

    scrollView.showsHorizontalScrollIndicator = NO;

    scrollView.showsVerticalScrollIndicator = NO;

    [self addSubview:scrollView];

    scrollView.contentSize = CGSizeMake( WIDTH * images.count , 0); // 内容视图大小

    scrollView.contentOffset = CGPointMake(WIDTH * (index-1), 0);   // 偏移量

UILabel * indexLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 30)];

    indexLbl.backgroundColor = [UIColor clearColor];

    indexLbl.textColor = [UIColor whiteColor];

    indexLbl.textAlignment = NSTextAlignmentCenter;

    indexLbl.text = [NSString stringWithFormat:@"%lu/%lu", index, images.count];

    indexLbl.font = Font14;

    [self addSubview:indexLbl];

_indexLbl = indexLbl;

//    _titleLbl = titleLbl;

for ( int i = 0 ; i < images.count ; i++ ) {

UIScrollView *sc = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH , HEIGHT)];

        sc.backgroundColor = [UIColor blackColor];

        sc.maximumZoomScale = 2.0;

        sc.minimumZoomScale = 1.0;

        sc.decelerationRate = 0.2;

        sc.delegate = self;

        sc.tag = 1 + i;

        [scrollView addSubview:sc];

UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

        [img sd_setImageWithURL:[NSURL URLWithString:images[i]] placeholderImage:[UIImage imageNamed:@"123"]];

        img.userInteractionEnabled = YES;

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenPics:)];

        tap.numberOfTapsRequired = 1;

        tap.numberOfTouchesRequired = 1;

        [sc addGestureRecognizer:tap];

UITapGestureRecognizer * twoTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(makePicBigger:)];

        twoTap.numberOfTapsRequired = 2;

        img.userInteractionEnabled = YES;

        [img addGestureRecognizer:twoTap];

        img.contentMode = UIViewContentModeScaleAspectFit;

        img.tag = 1000 + i;

        img.userInteractionEnabled = YES;

        [sc addSubview:img];

        sc.contentSize = CGSizeMake( WIDTH , 0);

//   双击没有识别到的时候识别单击手势

        [tap requireGestureRecognizerToFail:twoTap];

    }

}

- (void)hiddenPics:(UITapGestureRecognizer *)tap{

    [self removeFromSuperview];

    [UIApplication sharedApplication].statusBarHidden = NO;

}

- (void)makePicBigger:(UITapGestureRecognizer *)tap{

UIScrollView * sc = (UIScrollView *)[tap.view superview];

CGFloat zoomScale = sc.zoomScale;

    zoomScale = (zoomScale == 1.0) ? 3.0 : 1.0;

CGRect zoomRect = [self zoomRectForScale:zoomScale withCenter:[tap locationInView:tap.view]];

    [sc zoomToRect:zoomRect animated:YES];

}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center

{

CGRect zoomRect;

    zoomRect.size.height =self.frame.size.height / scale;

    zoomRect.size.width  =self.frame.size.width  / scale;

    zoomRect.origin.x = center.x - (zoomRect.size.width  /2.0);

    zoomRect.origin.y = center.y - (zoomRect.size.height /2.0);

return zoomRect;

}

//告诉scrollview要缩放的是哪个子控件

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

UIImageView *imgView = [scrollView.subviews firstObject];

return imgView;

}

- (void)show{

    [UIApplication sharedApplication].statusBarHidden = YES;

UIWindow *window=[UIApplication sharedApplication].keyWindow;

    [window addSubview:self];

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

int x = scrollView.contentOffset.x / WIDTH + 1;

_indexLbl.text = [NSString stringWithFormat:@"%d/%lu", x,_imageArr.count];

}

@end

.h文件:

//

//  ImageBrowserView.h

//  dipai

//

//  Created by 梁森 on 16/11/2.

//  Copyright © 2016年 梁森. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface ImageBrowserView : UIView<UIScrollViewDelegate>

// 下标

@property (nonatomic, strong) UILabel * indexLbl;

// 图片数组

@property (nonatomic, strong) NSArray * imageArr;

// 创建图片浏览器

- (instancetype)initWithImageArr:(NSArray *)imags andTag:(NSInteger)index;

// 显示图片浏览器

- (void)show;

@end

使用代码:

ImageBrowserView * imageBrowser = [[ImageBrowserView alloc] initWithImageArr:_goodsModel.atlas andTag:tag];

    imageBrowser.frame = CGRectMake(0, 0, WIDTH, HEIGHT);

    [imageBrowser show];

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • DownLoadData代码

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

    用户1451823
  • iOS开中设置UITabBar的背景图片以及各个item的选中图片和未选中

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

    用户1451823
  • iOS开发中使用CIFilter生成二维码和条形码

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010105969/article/details/...

    用户1451823
  • Groovy语法系列教程之布尔(五)

    本系列教程介绍Groovy编程语言的语法。Groovy的语法源自Java语法,但是通过特定类型对其进行了增强,并允许进行某些简化。

    DevOps持续交付
  • WordPress实现QQ卡片链接

    QQ发出去一个网址后,在展示的时候,他会快速抓取网站的内容(标题,缩略图,描述)进行展示,抓取有特殊标记的内容,快速展示出来。我们可以通过在head部分加标签的...

    神无月
  • 前端部署演化史

    前端一说起刀耕火种,那肯定紧随着前端工程化这一话题。随着 react/vue/angular,es6+,webpack,babel,typescript 以及 ...

    前端劝退师
  • ReactiveCocoa 中 RACCommand 底层实现分析

    在ReactiveCocoa 过程中,除去RACSignal和RACSubject这些信号类以外,有些时候我们可能还需要封装一些固定的操作集合。这些操作集合都是...

    一缕殇流化隐半边冰霜
  • Clipboard还能玩出花

    用户1907613
  • 金三银四跳槽季,BAT美团滴滴java面试大纲(带答案版)之二:ThreadLocal和Valotile

    金三银四跳槽季,BAT美团滴滴java面试大纲(带答案版)之二:ThreadLocal和Valotile 继续面试大纲系列文章。   ThreadLocal和V...

    小端
  • 2019年末逆向复习系列之拼夕夕Web端anti_content参数逆向分析

    这篇文章是公众号《云爬虫技术研究笔记》的《2019年末逆向复习系列》的第八篇:《拼夕夕Web端anti_content参数逆向分析》

    云爬虫技术研究笔记

扫码关注云+社区

领取腾讯云代金券