#import "ViewController.h"
#import "PhotoScrollView.h"
@interface ViewController ()
{
CGFloat width;
CGFloat height;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
width = self.view.bounds.size.width;
height = self.view.bounds.size.height;
//是否自定向下偏移64px
self.automaticallyAdjustsScrollViewInsets = NO;
//外层滑动视图
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
scrollView.delegate = self;
//内层
for (int i = 0; i < 5; i ++) {
PhotoScrollView *psView = [[PhotoScrollView alloc]initWithFrame:CGRectMake(width * i, 0, width, height)];
psView.tag = 100 + i;
psView.img = [UIImage imageNamed:[NSString stringWithFormat:@"%d.JPG",i]];
[scrollView addSubview:psView];
}
//内容尺寸
scrollView.contentSize = CGSizeMake(width * 5, height);
//分页显示
scrollView.pagingEnabled = YES;
//隐藏滑动条
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
[self.view addSubview:scrollView];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSInteger nowIndex = scrollView.contentOffset.x / width;
// NSLog(@"%ld",nowIndex);
if (nowIndex != _lastIndex) {
PhotoScrollView *imgview =(PhotoScrollView *) [scrollView viewWithTag:_lastIndex + 100];
imgview.zoomScale = 1;
}
_lastIndex = nowIndex;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "PhotoScrollView.h"
#import "UIView+viewController.h"
@implementation PhotoScrollView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_imgView = [[UIImageView alloc]initWithFrame:self.bounds];
// _imgView.image = self.img;
self.delegate = self;
[self addSubview:_imgView];
//缩放倍数
self.maximumZoomScale = 3;
self.minimumZoomScale = 1;
//添加单击事件
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget: self action:@selector(singelTapClick:)];
[self addGestureRecognizer:singleTap];
//添加2击事件
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget: self action:@selector(doubleTapClick:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
}
return self;
}
//添加2击事件
- (void)doubleTapClick:(UITapGestureRecognizer *)tap
{
if (self.zoomScale > 1) {
// self.zoomScale = 1;
[self setZoomScale:1 animated:1];
}else
{
[self setZoomScale:3 animated:1];
}
}
//添加单击事件
- (void)singelTapClick:(UITapGestureRecognizer *)tap
{
BOOL show = !self.viewController.navigationController.navigationBarHidden;
//导航栏隐藏与显示
[self.viewController.navigationController setNavigationBarHidden:show animated:1];
// [[UIApplication sharedApplication]setStatusBarOrientation:show animated:1];
[[UIApplication sharedApplication] setStatusBarHidden:show withAnimation:UIStatusBarAnimationFade];
}
- (void)setImg:(UIImage *)img
{
if (_img != img) {
_img = img;
_imgView.image = self.img;
}
}
//缩放时
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _imgView;
}
@end
#import "UIView+viewController.h"
@implementation UIView (viewController)
- (UIViewController *)viewController
{
UIResponder *next = self.nextResponder;
do {
if ([next isKindOfClass:[UIViewController class]]) {
return (UIViewController *)next;
}
next = next.nextResponder;
} while (next);
return nil;
}
@end