iOS查看多张图片,并且添加双击手势,转场动画

负责跳转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 <UIKit/UIKit.h>

@interface ImageViewController : UIViewController
//传入图片数组,可以是本地图片名,也可以为网络图片URL
@property (nonatomic,strong)NSArray<NSString *>*imageArray;

@end
#import "ImageViewController.h"
#import "ETapScrollView.h"

@interface ImageViewController ()<ETapScrollViewDelegate>
@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 <UIKit/UIKit.h>
@protocol  ETapScrollViewDelegate<NSObject>
- (void)disMissController;
@end
@interface ETapScrollView : UIScrollView
@property (nonatomic, assign) id <ETapScrollViewDelegate> ETapDelegate;
- (void)setTapViewWithImage:(NSArray <NSString *>*)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 ()<UIScrollViewDelegate>

@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<NSString *>*)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 <UIKit/UIKit.h>

typedef void(^singleTapBlock)();

@interface WLScrollView : UIScrollView<UIScrollViewDelegate>

@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

详细代码可以下载:https://github.com/eeevalu/PushImage

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值