iOS学习 用scrollView 制作相册album 涉及双击事件

时间:2022-08-01 19:26:09

#import <UIKit/UIKit.h>

#define VIEW_WIDTH self.view.bounds.size.width

#define VIEW_HEIGHT self.view.bounds.size.height

#import "ImageScrollView.h"

@interface HomeViewController :UIViewController<UIScrollViewDelegate>{

@private

    UIScrollView *_scrollView;

}

@end



@implementation HomeViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    _scrollView = [[UIScrollViewalloc]initWithFrame:CGRectMake(0,20,VIEW_WIDTH+20,VIEW_HEIGHT)];

    _scrollView.tag =101;

    [self.viewaddSubview:_scrollView];

    _scrollView.backgroundColor = [UIColorblackColor];

    _scrollView.pagingEnabled =YES;

    _scrollView.contentSize =CGSizeMake((VIEW_WIDTH+20)*4,0);//图片之间设置间隔

    _scrollView.delegate =self;

    _scrollView.showsHorizontalScrollIndicator =NO;

    

    int _x = 0;

    for (int i=0; i<4; i++) {

        ImageScrollView *imageSV = [[ImageScrollViewalloc]initWithFrame:CGRectMake(+_x,0,VIEW_WIDTH,VIEW_HEIGHT)];

        imageSV.tag = i;

        NSString  *imageName = [NSStringstringWithFormat:@"psu-%d.jpeg",i+13];

        imageSV.imageView.image = [UIImageimageNamed:imageName];

        _x += VIEW_WIDTH+20;

        [_scrollView addSubview:imageSV];

    }

}


int pre = 0;

#pragma mark - scrollView Delegate

//翻页时调用

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    int current = scrollView.contentOffset.x/(VIEW_WIDTH+20);

    

    ImageScrollView *imageSV =(ImageScrollView *)[scrollViewviewWithTag:pre];

    NSLog(@"%.2f",imageSV.zoomScale);

     //判断翻页时图片是否放大缩小

    if (current != pre && imageSV.zoomScale != 1)

    {

        imageSV.zoomScale = 1; //恢复正常大小

    }

    pre = current;

}




@interface ImageScrollView :UIScrollView<UIScrollViewDelegate>

{

@private

    UIImageView *_imageView;

}


@property (nonatomic,readonly,retain)UIImageView *imageView; //只读方式


@end




@implementation ImageScrollView


- (id)initWithFrame:(CGRect)frame{

    self = [superinitWithFrame:frame];

    if (self) {

        self.minimumZoomScale =0.3;

        self.maximumZoomScale =3;

        

        self.showsHorizontalScrollIndicator =NO;

        self.showsVerticalScrollIndicator =NO;

        

        //创建imageView,imageScrollview尺寸一样

        _imageView = [[UIImageViewalloc]initWithFrame:self.bounds];

        [self addSubview:_imageView];

        

        //添加双击事件

        UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(zoomInOROut:)];

        //点击次数

        doubleTap.numberOfTapsRequired = 2;

        //将点击事件添加到滑动视图上

        [self addGestureRecognizer:doubleTap];

        

        self.delegate =self;

    }

    return self;

}


#pragma mark - scrollView Delegate

// 返回一个放大或者缩小的视图

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return_imageView//只能处理单张图片

}


#pragma mark - target Action

- (void)zoomInOROut:(UITapGestureRecognizer *)tapGesture{

    if (self.zoomScale >=3) {

        [selfsetZoomScale:1animated:YES];

    }else {

        //获取手指点击的位置

        CGPoint point = [tapGesture locationInView:self];

        //设置放大的区域

        [self zoomToRect:CGRectMake(point.x-40, point.y-40,80,80) animated:YES];

    }

}

@end