-, UIScrollView, UIImageView , UIScrollView UIImageView, UIImageView, .
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
self.maximumZoomScale = 4.0f;
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
[self loadImageView:frame];
}
return self;
}
- (void) loadImageView:(CGRect) frame {
if (!_imageView) {
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,(frame.size.height-frame.size.width)/2, frame.size.width, frame.size.width)];
_imageView.contentMode = UIViewContentModeScaleAspectFit;
_imageView.center = self.center;
[self addSubview:_imageView];
}
}
- (void)showImage:(UIImage*)image{
self.imageView.image = image;
[self handleImageType];
}
-, viewForZoomingInScrollView scrollViewDidZoom , :
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
CGSize boundSize = self.bounds.size;
CGFloat boundWidth = boundSize.width;
CGFloat boundHeight = boundSize.height;
CGSize imageSize = self.imageView.image.size;
CGFloat imageWidth = imageSize.width;
CGFloat imageHeight = imageSize.height;
CGFloat zoomScale = scrollView.zoomScale;
DLog(@"zoomScale === %f",zoomScale);
[self normalScaleViewDidZoom:boundWidth boundHeight:boundHeight imageWidth:imageWidth imageHeight:imageHeight scrollView:scrollView];
}
- (void)handleImageType{
CGSize imageSize = self.imageView.image.size;
CGFloat imageWidth = imageSize.width;
CGFloat imageHeight = imageSize.height;
if (imageWidth > imageHeight) {
type = 1;
} else if (imageWidth < imageHeight){
type = 2;
} else if (imageWidth == imageHeight) {
type = 3;
}
}
- (void)normalScaleViewDidZoom:(CGFloat)boundWidth boundHeight:(CGFloat)boundHeight imageWidth:(CGFloat)imageWidth imageHeight:(CGFloat)imageHeight scrollView:(UIScrollView*)scrollView{
CGFloat zoomScale = scrollView.zoomScale;
if (type == 1) {
self.contentSize = CGSizeMake(boundWidth/imageHeight*imageWidth*zoomScale,boundWidth*zoomScale + (boundHeight-boundWidth));
} else if (type == 2){
self.contentSize = CGSizeMake(boundWidth*zoomScale,boundWidth/imageWidth*imageHeight*zoomScale + (boundHeight-boundWidth) );
} else if (type == 3) {
self.contentSize = CGSizeMake(boundWidth*zoomScale,boundHeight-boundWidth+boundWidth*zoomScale);
}
[self zoomCenter:scrollView];
}
- (void)zoomCenter:(UIScrollView*)scrollView{
CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}
, , , :)