/////继承自 scrollView且遵守 scrollView代理方法
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
self.bounces = NO;
self.imgV = [[SDBrowserImageView alloc]init];
self.imgV.width = SCREEN_WIDTH;
self.imgV.height = SCREEN_WIDTH;
self.imgV.center = CGPointMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT *0.5 );
[self addSubview:self.imgV];
// // 设置imageView允许用户交互,支持多点触碰
// self.imgV.userInteractionEnabled = YES;
self.userInteractionEnabled = YES;
// self.imgV.multipleTouchEnabled = YES;
//
self.imgV.contentMode = UIViewContentModeScaleAspectFit;
//2.设置放大缩小的倍数
self.maximumZoomScale = 2.0;
self.minimumZoomScale = 1.0;
//3.设置协议方法
self.delegate = self;
//4.设置双击手势
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap2:)];
//设置点击的次数
tap2.numberOfTapsRequired = 2;
//设置触摸手指的个数
tap2.numberOfTouchesRequired = 1;
//将手势对象添加到需要作用的视图
[self addGestureRecognizer:tap2];
//5.创建单击手势
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
[self addGestureRecognizer:tap1];
//当双击手势被触发时,让单击手势无效
[tap1 requireGestureRecognizerToFail:tap2];
}
return self;
}
- (void)tap2:(UITapGestureRecognizer *)tap {
// NSLog(@"双击了");
// NSLog(@"%@",tap.view);
// self.zoomScale; //子视图放大的倍数
if (self.zoomScale > 1) { //放大状态
//使用这个方法实现对子视图缩放
[self setZoomScale:1 animated:YES];
} else { //缩小状态
[self setZoomScale:2 animated:YES];
}
}
//单击图片事件
- (void)tap1:(UITapGestureRecognizer *)tap {
// NSLog(@"单击了");
//发送通知
[YRNotification postNotificationName:@"tapNoti" object:self];
}
#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imgV;
}
- (void)scrollViewDidZoom:(UIScrollView *)aScrollView
{
CGFloat offsetX = (self.bounds.size.width > self.contentSize.width)?
(self.bounds.size.width - self.contentSize.width) * 0.5 : 0.0;
CGFloat offsetY = (self.bounds.size.height > self.contentSize.height)?
(self.bounds.size.height - self.contentSize.height) * 0.5 : 0.0;
_imgV.center = CGPointMake(self.contentSize.width * 0.5 + offsetX,
self.contentSize.height * 0.5 + offsetY);
}
// 清除缩放
- (void)eliminateScale
{
self.contentSize = CGSizeMake(0, 0);
self.imgV.center = CGPointMake(SCREEN_WIDTH * 0.5, SCREEN_HEIGHT *0.5);
CGAffineTransform trans = CGAffineTransformMake(1, 0, 0, 1, 1.0, 1.0);
self.imgV.transform = trans;
}