@interface HQScrollView()
{
UIImageView *_imageView;
NSArray *_labels;
}
@end
- (void)initWithImage:(UIImage*)image labels:(NSArray*)labels {
if(!image)return;
self.contentSize =CGSizeMake(image.size.width , image.size.height);
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width , image.size.height )];
iv.image= image;
[_imageView removeFromSuperview];
_imageView = iv;
[self addSubview:_imageView];
self.delegate=self;
floatfx = kScreenWidth/ image.size.width;
floatfy = kScreenHeight/ image.size.height;
floatfzoom = (fx < fy ? fx : fy);
[self setMinimumZoomScale:fzoom];
[self setMaximumZoomScale:fzoom * 5];
self.zoomScale= fzoom;
_labels= labels;
for(HQLabel*label in _labels) {
[self addSubview:label];
}
[self scrollViewDidZoom:self];
}
-(void)scrollViewDidZoom:(UIScrollView*)scrollView{
//当scrollView自身的宽度或者高度大于其contentSize的时候, 增量为:自身宽度或者高度减去contentSize宽度或者高度除以2,或者为0
//条件运算符
CGFloatdelta_x= scrollView.bounds.size.width> scrollView.contentSize.width? (scrollView.bounds.size.width-scrollView.contentSize.width)/2:0;
CGFloatdelta_y= scrollView.bounds.size.height> scrollView.contentSize.height? (scrollView.bounds.size.height- scrollView.contentSize.height)/2:0;
//让imageView一直居中
//实时修改imageView的center属性 保持其居中
_imageView.center=CGPointMake(scrollView.contentSize.width/2+ delta_x, scrollView.contentSize.height/2+ delta_y);
for(HQLabel*labelin_labels) {
label.center=CGPointMake(delta_x + label.xPos * scrollView.zoomScale,
delta_y + label.yPos* scrollView.zoomScale);
}
}
-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView{
return _imageView;
}
- (void)scrollViewDidEndZooming:(UIScrollView*)scrollView withView:(UIView*)view atScale:(CGFloat)scale {
NSLog(@"scale:%f",scale);
NSLog(@"imageW:%f imageH:%f",_imageView.image.size.width,_imageView.image.size.height);
NSLog(@"contentSize = (%f,%f)",scrollView.contentSize.width,scrollView.contentSize.height);
NSLog(@"============");
}