前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS开发中简单的图片浏览器

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

作者头像
用户1451823
发布2018-09-13 16:04:13
1.1K0
发布2018-09-13 16:04:13
举报
文章被收录于专栏:DannyHoo的专栏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337861

.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:imagesi] 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;

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016年11月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档