#import "SkimViewController.h" #import "UIImageView+WebCache.h" @interface SkimViewController () { NSArray *_imageNames; NSInteger _index; UIScrollView *_scrollView; NSMutableArray *_imageViews; } @end @implementation SkimViewController - (instancetype)initWithImageNames:(NSArray *)imageNames index:(NSInteger)index { self = [super init]; if (self) { _imageNames = [[NSArray alloc] initWithArray:imageNames]; _index = index; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; [self configNavigationBar]; [self createScrollView]; [self creaeteImageViews]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)createScrollView { CGSize size = self.view.frame.size; _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; _scrollView.delegate = self; if (@available(iOS 11.0, *)) { _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }else { self.automaticallyAdjustsScrollViewInsets = NO; } _scrollView.pagingEnabled = YES; _scrollView.showsHorizontalScrollIndicator = NO; _scrollView.contentSize = CGSizeMake(size.width*3, size.height); _scrollView.contentOffset = CGPointMake(size.width, 0); [self.view addSubview:_scrollView]; } - (void)creaeteImageViews { //创建用于保存图片视图的数组 _imageViews = [[NSMutableArray alloc] init]; CGSize size = _scrollView.frame.size; NSInteger count = _imageNames.count; for (NSInteger i=0; i<3; i++) { UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(i*size.width, 0, size.width, size.height)]; view.tag = (i-1+count+_index)%count; view.contentMode = UIViewContentModeScaleAspectFit; [self setImageToView:view]; if (i != 1) { //添加到滚动视图上 [_scrollView addSubview:view]; } else { //创建一个等大的滚动视图 UIScrollView *sv = [[UIScrollView alloc] initWithFrame:view.frame]; //注意 啊 大兄弟 view.frame = CGRectMake(0, 0, size.width, size.height); //缩放比例 sv.minimumZoomScale = 0.2; sv.maximumZoomScale = 2.0; sv.delegate = self; //手势,双击 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)]; [view addGestureRecognizer:tap]; view.userInteractionEnabled = YES; [sv addSubview:view]; [_scrollView addSubview:sv]; //双击手势 UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)]; doubleTap.numberOfTapsRequired = 2; //单机必须在双击手势识别失败时,才能识别 [tap requireGestureRecognizerToFail:doubleTap]; [view addGestureRecognizer:doubleTap]; } //保存到数组中 [_imageViews addObject:view]; } //刷新标题 [self refreshTitle]; } - (void)tapHandle:(UITapGestureRecognizer *)tap { if (tap.numberOfTapsRequired == 1) { BOOL hidden = !self.navigationController.navigationBarHidden; [self.navigationController setNavigationBarHidden:hidden animated:YES]; } else if (tap.numberOfTapsRequired == 2) { UIScrollView *sv = (UIScrollView *)[tap.view superview]; if (sv.zoomScale != 1.0) { [sv setZoomScale:1.0 animated:YES]; } else { [sv setZoomScale:sv.maximumZoomScale animated:YES]; [self.navigationController setNavigationBarHidden:YES animated:YES]; } } } - (void)setImageToView:(UIImageView *)view { [view sd_setImageWithURL:[NSURL URLWithString:_imageNames[view.tag][@"url"]]]; } - (void)cycleReuse { CGFloat offset = _scrollView.contentOffset.x; CGFloat width = _scrollView.frame.size.width; NSInteger flag = 0; if (offset == 0) { //向右滑 flag = 1; } else if (offset == 2*width) { //向左滑 flag = -1; } else { return; } NSInteger count = _imageNames.count; for (UIImageView *view in _imageViews) { view.tag = (view.tag-flag+count)%count; [self setImageToView:view]; } //无动画的重置偏移量 _scrollView.contentOffset = CGPointMake(width, 0); //刷新标题 [self refreshTitle]; //恢复中间滚动视图的缩放比例1.0 UIScrollView *sv = (UIScrollView *)[_imageViews[1] superview]; sv.zoomScale = 1.0; } - (void)refreshTitle { self.navigationItem.title = [NSString stringWithFormat:@"%ld/%lu",[(UIView *)_imageViews[1] tag] + 1,_imageNames.count]; } #pragma mark - 代理方法 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [self cycleReuse]; } #pragma mark - sv代理,不用跟_scrolview区分,因为后者没有设置比例 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return _imageViews[1]; } - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale { //当缩放结束,比例小于0.5时,返回到上一页 if (scale < 0.5) { //返回上一页,显示导航条 if (self.navigationController.navigationBarHidden) { self.navigationController.navigationBarHidden = NO; } [self.navigationController popViewControllerAnimated:YES]; } } @end