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

来源:互联网 发布:cnc串联程序软件 编辑:程序博客网 时间:2024/05/21 18:22

.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 == [superinit]) {

        

        [selfsetUpChildControlWithArr:imagsandTag:index];

    }

    returnself;

}


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

    

    _imageArr = images;

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

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

    scrollView.delegate =self;

    scrollView.scrollEnabled =YES;

    scrollView.pagingEnabled =YES;

    scrollView.backgroundColor = [UIColorblackColor];

    scrollView.showsHorizontalScrollIndicator =NO;

    scrollView.showsVerticalScrollIndicator =NO;

    [selfaddSubview:scrollView];

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

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

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

    indexLbl.backgroundColor = [UIColorclearColor];

    indexLbl.textColor = [UIColorwhiteColor];

    indexLbl.textAlignment =NSTextAlignmentCenter;

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

    indexLbl.font =Font14;

    [selfaddSubview:indexLbl];

    _indexLbl = indexLbl;

//    _titleLbl = titleLbl;

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

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

        sc.backgroundColor = [UIColorblackColor];

        sc.maximumZoomScale =2.0;

        sc.minimumZoomScale =1.0;

        sc.decelerationRate =0.2;

        sc.delegate =self;

        sc.tag =1 + i;

        [scrollView addSubview:sc];

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

        [img sd_setImageWithURL:[NSURLURLWithString:images[i]]placeholderImage:[UIImageimageNamed:@"123"]];

        

        img.userInteractionEnabled =YES;

        UITapGestureRecognizer * tap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(hiddenPics:)];

        tap.numberOfTapsRequired =1;

        tap.numberOfTouchesRequired =1;

        [sc addGestureRecognizer:tap];

        

        UITapGestureRecognizer * twoTap = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@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{

    [selfremoveFromSuperview];

    [UIApplicationsharedApplication].statusBarHidden =NO;

}

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

    UIScrollView * sc = (UIScrollView *)[tap.viewsuperview];

    CGFloat zoomScale = sc.zoomScale;

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

    CGRect zoomRect = [selfzoomRectForScale:zoomScalewithCenter:[tap locationInView:tap.view]];

    [sc zoomToRect:zoomRectanimated: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.subviewsfirstObject];

    return imgView;

}

- (void)show{

    [UIApplicationsharedApplication].statusBarHidden =YES;

    UIWindow *window=[UIApplicationsharedApplication].keyWindow;

    [window addSubview:self];

}

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

    

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

    _indexLbl.text = [NSStringstringWithFormat:@"%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 = [[ImageBrowserViewalloc] initWithImageArr:_goodsModel.atlasandTag:tag];

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

    [imageBrowser show];





1 0