负责跳转deviewController
//
// ViewController.m
// PushImageTest
//
// Created by Eva on 17/4/10.
// Copyright © 2017年 Eva. All rights reserved.
//
#import "ViewController.h"
#import "ImageViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
imageView.frame = CGRectMake(0, 100, self.view.frame.size.width,self.view.frame.size.width);
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickImage)];
[imageView addGestureRecognizer:tap];
[self.view addSubview:imageView];
}
- (void)didClickImage {
ImageViewController *imageVc = [[ImageViewController alloc] init];
imageVc.imageArray = @[@"1",@"2",@"3",@"4",@"5"];
CATransition *animation = [CATransition animation];
animation.duration = 0.4;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
// animation.type = @"pageCurl";//翻页效果
animation.type = kCATransitionFade;
// animation.subtype = kCATransitionFromBottom;
[self.view.window.layer addAnimation:animation forKey:nil];
[self presentViewController:imageVc animated:NO completion:^{
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
展示图片的viewController
#import
@interface ImageViewController : UIViewController
//传入图片数组,可以是本地图片名,也可以为网络图片URL
@property (nonatomic,strong)NSArray*imageArray;
@end
#import "ImageViewController.h"
#import "ETapScrollView.h"
@interface ImageViewController ()
@property (nonatomic,strong)ETapScrollView * bigImageView;
@end
@implementation ImageViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.bigImageView];
[_bigImageView setTapViewWithImage:_imageArray isUrlImage:NO];
}
#pragma mark - ETapScrollViewDelegate
- (void)disMissController {
[self dismissViewControllerAnimated:NO completion:nil];
}
#pragma mark - setter && getter
- (ETapScrollView *)bigImageView {
if (!_bigImageView) {
_bigImageView = [[ETapScrollView alloc] initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height )];
_bigImageView.center = self.view.center;
_bigImageView.backgroundColor = [UIColor blackColor];
_bigImageView.ETapDelegate = self;
}
return _bigImageView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
关于图片滚动、添加手势、图片之间添加间隙,封装的类
#import
@protocol ETapScrollViewDelegate
- (void)disMissController;
@end
@interface ETapScrollView : UIScrollView
@property (nonatomic, assign) id ETapDelegate;
- (void)setTapViewWithImage:(NSArray *)array isUrlImage:(BOOL)isUrlImage;
@end
#define kScreenW [[UIScreen mainScreen] bounds].size.width //屏幕宽度
#define kScreenH [[UIScreen mainScreen] bounds].size.height //屏幕高度
#import "ETapScrollView.h"
#import "WLScrollView.h"
#import "UIImageView+WebCache.h"
@interface ETapScrollView ()
@end
@implementation ETapScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//设置代理
self.backgroundColor = [UIColor blackColor];
self.delegate = self;
self.bounces = YES;
self.showsHorizontalScrollIndicator = NO;
//翻页效果
self.pagingEnabled = YES;
}
return self;
}
- (void)setTapViewWithImage:(NSArray*)array isUrlImage:(BOOL)isUrlImage{
CGFloat dx = 30;
CGFloat itemWidth = self.bounds.size.width + dx * 2.0;
for(NSInteger i= 0; i< array.count; i++ ) {
WLScrollView *s = [[WLScrollView alloc] initWithFrame:CGRectMake(i==0? 0:(kScreenW + 10)* i, 0, kScreenW, kScreenH)];
CGRect frame = self.bounds;
frame.origin.x = itemWidth * i;
frame.origin.y = 0.0;
frame.size.width = itemWidth;
s.frame = CGRectInset(frame, dx, 0);
[self addSubview:s];
if (isUrlImage) {
[s.imageView sd_setImageWithURL:[NSURL URLWithString:array[i]]];
}else {
s.imageView.image = [UIImage imageNamed:array[i]];
}
s.block = ^(){
[self clickImage];
};
}
self.frame = CGRectMake(-dx, 0, itemWidth, kScreenH);
self.contentSize = CGSizeMake(itemWidth *array.count, self.frame.size.height);
}
- (void)clickImage{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.alpha = 0;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
if (_ETapDelegate && [_ETapDelegate respondsToSelector:@selector(disMissController)]) {
[_ETapDelegate disMissController];
}
}
#import
typedef void(^singleTapBlock)();
@interface WLScrollView : UIScrollView
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, copy) singleTapBlock block;
@end
#import "WLScrollView.h"
@interface WLScrollView ()
@end
@implementation WLScrollView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.delegate = self;
self.minimumZoomScale = 1 * self.zoomScale;
self.maximumZoomScale = 3 * self.zoomScale;
[self initImageView];
}
return self;
}
- (void)initImageView {
self.imageView = [[UIImageView alloc] init];
// self.imageView.backgroundColor = [UIColor redColor];
self.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self addSubview:self.imageView];
//单击事件
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
singleTap.numberOfTouchesRequired = 1; //手指数
singleTap.numberOfTapsRequired = 1; //tap次数
[self addGestureRecognizer:singleTap];
//增加双击事件
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleFingerEvent:)];
doubleTap.numberOfTouchesRequired = 1;
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
//防止双击无效
[singleTap requireGestureRecognizerToFail:doubleTap];
}
- (void)handleSingleFingerEvent:(UITapGestureRecognizer *)tap {
self.block();
}
- (void)handleDoubleFingerEvent:(UITapGestureRecognizer *)tap {
static BOOL tapNum = YES;
CGFloat scale = 1;
if (tapNum) {
scale = self.maximumZoomScale;
}else {
scale = self.minimumZoomScale;
}
tapNum = !tapNum;
[self setZoomScale:scale animated:YES];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
[scrollView setZoomScale:scale animated:NO];
}
@end