// // HBImageScroller.m // MyTest // // Created by weqia on 13-7-31. // Copyright (c) 2013年 weqia. All rights reserved. // #import "HBImageScroller.h" #import "HBHttpRequestCache.h" #import "DialogUtil.h" #import "HBHttpImageDownloader.h" #import "JXActionSheetVC.h" @interface HBImageScroller () @property (nonatomic, strong) JXActionSheetVC *actionVC; @end @implementation HBImageScroller @synthesize imageView=_imageView,controller; #pragma -mark 覆盖父类的方法 #pragma -mark 事件响应方法 -(void)longPressAction:(UIGestureRecognizer*)sender { if(sender.state==UIGestureRecognizerStateBegan){ if(self.controller){ self.actionVC = [[JXActionSheetVC alloc] initWithImages:@[] names:@[Localized(@"HBImageScroller_SavePhone"),Localized(@"HBImageScroller_ShareTo")]]; self.actionVC.delegate = self; [g_App.window addSubview:self.actionVC.view]; }else{ self.actionVC = [[JXActionSheetVC alloc] initWithImages:@[] names:@[Localized(@"HBImageScroller_SavePhone")]]; self.actionVC.delegate = self; [g_App.window addSubview:self.actionVC.view]; } } } /**scroll view处理缩放和平移手势,必须需要实现委托下面两个方法,另外 maximumZoomScale和minimumZoomScale两个属性要不一样*/ //1.返回要缩放的图片 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return self.imageView; } //让图片保持在屏幕中央,防止图片放大时,位置出现跑偏 - (void)scrollViewDidZoom:(UIScrollView *)scrollView{ CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0; CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0; self.imageView.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX,scrollView.contentSize.height * 0.5 + offsetY); } //2.重新确定缩放完后的缩放倍数 -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{ [scrollView setZoomScale:scale+0.01 animated:NO]; [scrollView setZoomScale:scale animated:NO]; } //-(void)imagePinchAction:(UIPinchGestureRecognizer*)recognizer //{ // if(recognizer.state==UIGestureRecognizerStateBegan) // { // _beginSize=_imageView.frame.size; // } // else if(recognizer.state==UIGestureRecognizerStateChanged) // { // int width= _beginSize.width*recognizer.scale; // int height=_imgScale*width; // if(width<_scale*1.5*_beginImageSize.width&&width>0.5*_beginImageSize.width) // { // CGSize size=CGSizeMake(width, height); // if (size.height_scale*_beginImageSize.width) // { // [UIView beginAnimations:nil context:NULL]; // [UIView setAnimationDuration:0.2]; // CGSize size=self.contentSize; // size.width=_beginImageSize.width*_scale; // size.height=_imgScale*size.width; // if (size.heightrect.size.width){ width=rect.size.width; height=width*imgScale; _scale=rect.size.height/height; }else if(imgScale>=viewScale&&size.height>rect.size.height){ height=rect.size.height; width=height/imgScale; _scale=rect.size.width/width; }else{ _scale=rect.size.width/width; } float x=0,y=0; if (width 0) { UIImage *image2 = [[HBHttpRequestCache shareCache] getBitmapFromMemory:url]; if (image2.images.count > 0) { image1 = image2; }else{ [[HBHttpRequestCache shareCache] clearOneCache:url]; [[HBHttpRequestCache shareCache] storeBitmapToMemory:image withKey:url]; image1 = image; } }else{ image1 = [[HBHttpRequestCache shareCache] getBitmapFromMemory:url]; } if(image1){ _imageView.image=image1; [self setImageFrameAndContentSize]; _imageView.userInteractionEnabled=YES; UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [self addGestureRecognizer:longPress]; }else{ UIImage *image1=[[HBHttpRequestCache shareCache] getBitmapFromDisk:url]; if(image1){ _imageView.image=image1; [self setImageFrameAndContentSize]; _imageView.userInteractionEnabled=YES; UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [self addGestureRecognizer:longPress]; }else{ _imageView.image=image; [self setImageFrameAndContentSize]; _imageView.userInteractionEnabled=NO; UIActivityIndicatorView *indicator=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; indicator.frame=CGRectMake((self.frame.size.width-20)/2, (self.frame.size.height-20)/2, 20, 20); [self addSubview:indicator]; [indicator startAnimating]; [[HBHttpImageDownloader shareDownlader] downBitmapWithURL:url process:nil complete:^(UIImage *image, NSData *data, NSError *error, BOOL finish) { dispatch_async(dispatch_get_main_queue(), ^{ [indicator stopAnimating]; [indicator removeFromSuperview]; _imageView.image=image; [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ [self setImageFrameAndContentSize]; } completion:^(BOOL finished) { _imageView.userInteractionEnabled=YES; UILongPressGestureRecognizer * longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [self addGestureRecognizer:longPress]; }]; }); } option:HBHttpImageDownloaderOptionUseCache valueReturn:nil]; } } } -(void)addTarget:(id)target tapOnceAction:(SEL)action { _target=target; _tapOnceAction=action; } -(void)reset { [self setImageFrameAndContentSize]; } @end