圖片瀏覽工具
STImageVIew.h
//
// STImageVIew.h
// STPhotoBroeser
//
// Created by StriEver on 16/3/15.
// Copyright ? 2016年 StriEver. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol STImageViewDelegate;
@interface STImageVIew : UIImageView
@property (nonatomic, weak)id<STImageViewDelegate>delegate;
- (void)resetView;
@end
@protocol STImageViewDelegate <NSObject>
- (void)stImageVIewSingleClick:(STImageVIew *)imageView;
@end
STImageVIew.m
//
// STImageVIew.m
// STPhotoBroeser
//
// Created by StriEver on 16/3/15.
// Copyright ? 2016年 StriEver. All rights reserved.
//
#import "STImageVIew.h"
#import "UIView+Extension.h"
@interface STImageVIew()<UIGestureRecognizerDelegate>{
CGFloat _lastScale;//記錄最后一次的圖片放大倍數(shù)
}
/**手機屏幕高度不夠用的時候 用于顯示完整圖片*/
@property (nonatomic, strong) UIScrollView * scrollView;
/**完整圖片*/
@property (nonatomic, strong) UIImageView * scrollImgV;
/**用于放大 縮小 圖片的scrollview*/
@property (nonatomic, strong) UIScrollView * scaleScrollView;
/**用于顯示 放大縮小的 圖片*/
@property (nonatomic, strong) UIImageView * scaleImgV;
@property (nonatomic, assign) BOOL doubleAction;
@end
@implementation STImageVIew
- (instancetype)initWithFrame:(CGRect)frame{
if (self == [super initWithFrame:frame]) {
self.userInteractionEnabled = YES;
UIPinchGestureRecognizer * ges = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scaleImageViewAction:)];
ges.delegate = self;
_lastScale = 1.f;
[self addGestureRecognizer:ges];
UITapGestureRecognizer * singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleClick:)];
[self addGestureRecognizer:singleTap];
UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleClick:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
}
return self;
}
//getter
- (UIScrollView *)scrollView{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
[self addSubview:_scrollView];
}
return _scrollView;
}
- (UIImageView *)scrollImgV{
if (!_scrollImgV) {
_scrollImgV = [[UIImageView alloc]init];
_scrollImgV.image = self.image;
[self.scrollView addSubview:_scrollImgV];
}
return _scrollImgV;
}
- (UIScrollView *)scaleScrollView{
if (!_scaleScrollView) {
_scaleScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
_scaleScrollView.bounces = NO;
_scaleScrollView.backgroundColor = [UIColor blackColor];
_scaleScrollView.contentSize = self.bounds.size;
[self addSubview:_scaleScrollView];
}
return _scaleScrollView;
}
- (UIImageView *)scaleImgV{
if (!_scaleImgV) {
_scaleImgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.width, self.height)];
_scaleImgV.image = self.image;
[self.scaleScrollView addSubview:_scaleImgV];
}
return _scaleImgV;
}
- (void)layoutSubviews{
[super layoutSubviews];
CGSize imageSize = self.image.size;
//圖片高度大于屏幕高度
if (self.width * (imageSize.height / imageSize.width) > self.height) {
[self scrollView];
self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.width * (imageSize.height / imageSize.width));
self.scrollImgV.center = self.scrollView.center;
self.scrollImgV.bounds = CGRectMake(0, 0, imageSize.width, self.width * (imageSize.height / imageSize.width));
}else{
if (_scrollView)[_scrollView removeFromSuperview];
}
}
#pragma mark ---action
-(void)scaleImageViewAction:(UIPinchGestureRecognizer*)sender {
CGFloat scale = sender.scale;//得到的是當前手勢放大倍數(shù)
NSLog(@"--------%f",scale);
CGFloat shouldScale = _lastScale + (scale - 1);//我們需要知道的是當前手勢相收縮率對于剛才手勢的相對收縮 scale - 1颜启,然后加上最后一次收縮率忠聚,為當前要展示的收縮率
[self setScaleImageWithScale:shouldScale];
sender.scale = 1.0;//圖片大小改變后設置手勢scale為1
}
- (void)setScaleImageWithScale:(CGFloat)scale{
//最大2倍最小.5
if (scale >=2) {
scale = 2;
}else if(scale <=.5){
scale = .5;
}
_lastScale = scale;
self.scaleImgV.transform = CGAffineTransformMakeScale(scale, scale);
if (scale > 1) {
CGFloat imageWidth = self.scaleImgV.width;
CGFloat imageHeight = MAX(self.scaleImgV.height, self.frame.size.height);
[self bringSubviewToFront:self.scaleScrollView];
self.scaleImgV.center = CGPointMake(imageWidth * 0.5, imageHeight * 0.5);
self.scaleScrollView.contentSize = CGSizeMake(imageWidth, imageHeight);
CGPoint offset = self.scaleScrollView.contentOffset;
offset.x = (imageWidth - self.width)/2.0;
offset.y = (imageHeight - self.height)/2.0;
self.scaleScrollView.contentOffset = offset;
}else{
self.scaleImgV.center = self.scaleScrollView.center;
self.scaleScrollView.contentSize = CGSizeZero;
}
}
- (void)singleClick:(UITapGestureRecognizer *)tap{
if (_delegate &&[_delegate respondsToSelector:@selector(stImageVIewSingleClick:)]) {
[_delegate stImageVIewSingleClick:self];
}
}
- (void)doubleClick:(UITapGestureRecognizer *)tap{
if (_lastScale > 1) {
_lastScale = 1;
}else{
_lastScale = 2;
}
[UIView animateWithDuration:.5 animations:^{
[self setScaleImageWithScale:_lastScale];
}completion:^(BOOL finished) {
if (_lastScale == 1) {
[self resetView];
}
}];
}
//當達到原圖大小 清除 放大的圖片 和scrollview
- (void)resetView{
if (!self.scaleScrollView) {
return;
}
self.scaleScrollView.hidden = YES;
[self.scaleScrollView removeFromSuperview];
self.scaleScrollView = nil;
self.scaleImgV = nil;
}
@end
STPhotoBroswer.h
//
// STPhotoBroswer.h
// STPhotoBroeser
//
// Created by StriEver on 16/3/16.
// Copyright ? 2016年 StriEver. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface STPhotoBroswer : UIView
/**
* @brief 初始化方法 圖片以數(shù)組的形式傳入, 需要顯示的當前圖片的索引
*
* @param imageArray需要顯示的圖片以數(shù)組的形式傳入.
* @param index 需要顯示的當前圖片的索引
*/
- (instancetype)initWithImageArray:(NSArray *)imageArray currentIndex:(NSInteger)index;
- (void)show;
@end
STPhotoBroswer.m
//
// ;
// STPhotoBroeser
//
// Created by StriEver on 16/3/16.
// Copyright ? 2016年 StriEver. All rights reserved.
//
#import "STPhotoBroswer.h"
#import "STImageVIew.h"
#define MAIN_BOUNDS [UIScreen mainScreen].bounds
#define Screen_Width [UIScreen mainScreen].bounds.size.width
#define Screen_Height [UIScreen mainScreen].bounds.size.height
//圖片距離左右 間距
#define SpaceWidth 10
@interface STPhotoBroswer ()<STImageViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) NSArray * imageArray;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UILabel * numberLabel;
@end
@implementation STPhotoBroswer
- (instancetype)initWithImageArray:(NSArray *)imageArray currentIndex:(NSInteger)index{
if (self == [super init]) {
self.imageArray = imageArray;
self.index = index;
[self setUpView];
}
return self;
}
//--getter
- (UIScrollView *)scrollView{
if (!_scrollView) {
_scrollView = [[UIScrollView alloc]initWithFrame:[UIScreen mainScreen].bounds];
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.delegate = self;
//這里
_scrollView.contentSize = CGSizeMake((Screen_Width + 2*SpaceWidth) * self.imageArray.count, Screen_Height);
_scrollView.contentOffset = CGPointMake(Screen_Width * self.index, 0);
_scrollView.scrollEnabled = YES;
_scrollView.pagingEnabled = YES;
[self addSubview:_scrollView];
[self numberLabel];
}
return _scrollView;
}
- (UILabel *)numberLabel{
if (!_numberLabel) {
_numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, Screen_Width, 40)];
_numberLabel.textAlignment = NSTextAlignmentCenter;
_numberLabel.textColor = [UIColor greenColor];
_numberLabel.text = [NSString stringWithFormat:@"%d/%d",self.index +1,self.imageArray.count];
[self addSubview:_numberLabel];
}
return _numberLabel;
}
- (void)setUpView{
int index = 0;
for (UIImage * image in self.imageArray) {
STImageVIew * imageView = [[STImageVIew alloc]init];
imageView.delegate = self;
imageView.image = image;
imageView.tag = index;
[self.scrollView addSubview:imageView];
index ++;
}
}
#pragma mark ---UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSInteger index = scrollView.contentOffset.x/Screen_Width;
self.index = index;
[self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([NSStringFromClass(obj.class) isEqualToString:@"STImageVIew"]) {
STImageVIew * imageView = (STImageVIew *) obj;
[imageView resetView];
}
}];
self.numberLabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1,self.imageArray.count];
}
- (void)layoutSubviews{
[super layoutSubviews];
//主要為了設置每個圖片的間距弧关,并且使 圖片鋪滿整個屏幕翰舌,實際上就是scrollview每一頁的寬度是 屏幕寬度+2*Space 居中嚣潜。圖片左邊從每一頁的 Space開始,達到間距且居中效果椅贱。
_scrollView.bounds = CGRectMake(0, 0, Screen_Width + 2 * SpaceWidth,Screen_Height);
_scrollView.center = self.center;
[self.scrollView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.frame = CGRectMake(SpaceWidth + (Screen_Width+20) * idx, 0,Screen_Width,Screen_Height);
}];
}
- (void)show{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
self.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
[window addSubview:self];
self.transform = CGAffineTransformMakeScale(0, 0);
[UIView animateWithDuration:.5 animations:^{
self.transform = CGAffineTransformIdentity;
}];
}
- (void)dismiss{
self.transform = CGAffineTransformIdentity;
[UIView animateWithDuration:.5 animations:^{
self.transform = CGAffineTransformMakeScale(0.0000000001, 0.00000001);
}completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
#pragma mark ---STImageViewDelegate
- (void)stImageVIewSingleClick:(STImageVIew *)imageView{
[self dismiss];
}
@end
使用方法
STPhotoBroswer * broser = [[STPhotoBroswer alloc]initWithImageArray:@[[UIImage imageNamed:@"1.jpg"],[UIImage imageNamed:@"2.jpg"],[UIImage imageNamed:@"3.jpg"],[UIImage imageNamed:@"4.jpg"],[UIImage imageNamed:@"5.jpg"],[UIImage imageNamed:@"6.jpg"],[UIImage imageNamed:@"7.jpg"]] currentIndex:0];
[broser show];
``` 最近項目中需要用到這個功能懂算,就抽時間自己動手實現(xiàn)了一下吧。希望和大家共同進步夜涕。
[我的github](https://github.com/strivever/STPhotoBroswer)可以下載一下哦
![4.7.gif](http://upload-images.jianshu.io/upload_images/1663049-71d26cb658505472.gif?imageMogr2/auto-orient/strip)