#import "ViewController.h"


#define kuan [UIScreen mainScreen].bounds.size.width

#define gao [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *huaKuang;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //设置contentSize

    _huaKuang.contentSize=CGSizeMake((kuan)*4, gao);

    _huaKuang.backgroundColor=[UIColor blackColor];

    //设置分页

    _huaKuang.pagingEnabled=YES;

    //隐藏滚动条

    //滚动时是否显示水平滚动条

    _huaKuang.showsHorizontalScrollIndicator=NO;

    //滚动时是否显示垂直滚动条

    _huaKuang.showsVerticalScrollIndicator=NO;

    //设置代理,需要遵循代理协议<UIScrollViewDelegate>,写在@interface ViewController ()的后面

    _huaKuang.delegate=self;

    //添加子视图,因为是多个所以写了一个方法来实现添加

    [self tianJianZiShiTu];

}

-(void)tianJianZiShiTu

{

    //假如有六个图片,就要创建六个UIScrollView和六个UIImageView,并且找到六个图片

    for(int i=0;i<3;i++)

    {

        //创建UIScrollView

        //为了区分开不同的照片加一个20黑边,需要设置ScrollViewscroll View中的LeftView中的Width

        UIScrollView *uisv=[[UIScrollView alloc] initWithFrame:CGRectMake((kuan+20)*i, 0, kuan, gao)];

        //把创建完成的添加到总的那个UIScrollView

        [_huaKuang addSubview:uisv];

        //创建UIImageView

        UIImageView *uiiv=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kuan, gao)];

        //把创建的UIImageView添加到UIScrollView

        [uisv addSubview:uiiv];

        //设置UIImageView的图片

         NSString *imageName = [NSString stringWithFormat:@"new_feature_%d",i + 1];

        uiiv.image=[UIImage imageNamed:imageName];

        //设置tag

        uiiv.tag=1000;

        //设置UIScrollView的代理

        uisv.delegate=self;

        //设置缩放范围

        uisv.minimumZoomScale=0.5;

        uisv.maximumZoomScale=1.5;

        //定义点击事件

        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianJiShiJian:)];

        //设置有效点击数(就是双击)

        tap.numberOfTapsRequired=2;

        //添加到UIScrollView

        [uisv addGestureRecognizer:tap];

    }

}

-(void)dianJiShiJian:(UITapGestureRecognizer *)tap

{

    //获取点击事件的view

    UIScrollView *uisv1=(UIScrollView *)tap.view;

    if(uisv1.zoomScale!=1.0)

   {

       [uisv1 setZoomScale:1.0 animated:YES];

       return ;

   }

    CGPoint location=[tap locationInView:tap.view];

    CGRect rect=CGRectMake(location.x-100, location.y-100, 200, 200);

    [uisv1 zoomToRect:rect animated:YES];

}

//指定缩放的视图

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

    if (scrollView == _huaKuang) {

        return nil;

    }

    UIImageView *imageView = (UIImageView *)[scrollView viewWithTag:1000];

    return imageView;

}


//滚动结束,把所有的缩放视图的缩放比例置为1.0

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

    if (scrollView == _huaKuang) {

        for (id obj in _huaKuang.subviews) {

            if ([obj isKindOfClass:[UIScrollView class]]) {

                UIScrollView *scaleSC = (UIScrollView *)obj;

                scaleSC.zoomScale = 1.0;

            }

        }

    }

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}


@end