#import <UIKit/UIKit.h><br />#import <QuartzCore/QuartzCore.h></p><p>typedef enum{<br /> UIImageExNormal = 0,<br /> UIImageExFull<br />}UIImageExState;</p><p>@interface UIImageViewEx : UIImageView<UIGestureRecognizerDelegate><br />{<br /> UIView *parentview; //父視窗,即用將UIImageEx所加到的UIView</p><p> BOOL isPanEnable; //是否可以移動<br /> BOOL isPinchEnable; //是否可以放大縮小<br /> BOOL isRotateEnable; //是否可以旋轉<br /> BOOL isTap; //是否可以點擊觸摸</p><p> UIImageExState imageState; //圖片目前狀態</p><p> CGFloat imageScale; //最大縮放的倍數<br /> CGFloat imageSize; //記錄圖片的累計縮放<br /> CGFloat imageRotation; //記錄圖片的原始角度<br /> CGPoint imagePoint; //記錄圖片的原始位置</p><p> UITextView *textView; //動態彈出的文本</p><p>}</p><p>@property (nonatomic,retain) UIView *parentview;<br />@property (nonatomic) CGFloat imageSize;<br />@property (nonatomic) CGFloat imageRotation;<br />@property (nonatomic) CGPoint imagePoint;</p><p>@property BOOL isPanEnable;<br />@property BOOL isRotateEnable;<br />@property BOOL isPinchEnable;<br />@property BOOL isTap;</p><p>- (void)handlePan:(UIPanGestureRecognizer *)recognizer;<br />- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;<br />- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer;<br />- (void)handleTap:(UITapGestureRecognizer *)recognizer;</p><p>//必須設定的<br />- (void)setScaleAndRotation:(UIView*)imageView;<br />- (void)setInfoText:(NSString *)string;<br />- (void)setShadow:(BOOL)isShadow;<br />@end<br />
#import "UIImageViewEx.h"</p><p>@implementation UIImageViewEx<br />@synthesize parentview;<br />@synthesize isRotateEnable,isPanEnable,isPinchEnable,isTap;<br />@synthesize imageSize,imageRotation,imagePoint;</p><p>/*<br /> * SetScaleAndRotation 實現 ImageView的 手勢旋轉,縮放,和移動<br /> * @parent UIView 父視窗<br /> */<br />- (void)setScaleAndRotation:(UIView*) parent<br />{<br /> parentview=parent;<br /> parentview.userInteractionEnabled=YES;</p><p> isPanEnable=YES;<br /> isPinchEnable=YES;<br /> isRotateEnable=YES;<br /> isTap = YES;</p><p> imageSize=1;<br /> imageRotation=0;</p><p> imageScale= self.parentview.frame.size.width/self.frame.size.width;<br /> imagePoint=self.frame.origin;<br /> self.userInteractionEnabled=YES;</p><p> UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];<br /> panRcognize.delegate=self;<br /> [panRcognize setEnabled:YES];<br /> [panRcognize delaysTouchesEnded];<br /> [panRcognize cancelsTouchesInView];</p><p> UIPinchGestureRecognizer *pinchRcognize=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];<br /> [pinchRcognize setEnabled:YES];<br /> [pinchRcognize delaysTouchesEnded];<br /> [pinchRcognize cancelsTouchesInView];</p><p> UIRotationGestureRecognizer *rotationRecognize=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];<br /> [rotationRecognize setEnabled:YES];<br /> [rotationRecognize delaysTouchesEnded];<br /> [rotationRecognize cancelsTouchesInView];<br /> rotationRecognize.delegate=self;<br /> pinchRcognize.delegate=self;</p><p> UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];<br /> tapRecognize.numberOfTapsRequired = 1;<br /> tapRecognize.delegate = self;<br /> [tapRecognize setEnabled :YES];<br /> [tapRecognize delaysTouchesBegan];<br /> [tapRecognize cancelsTouchesInView];</p><p> [self addGestureRecognizer:rotationRecognize];<br /> [self addGestureRecognizer:panRcognize];<br /> [self addGestureRecognizer:pinchRcognize];<br /> [self addGestureRecognizer:tapRecognize];</p><p>}<br />/*<br /> * setInfoText 設定介紹文字<br /> * @string NSString 顯示的文字<br /> */<br />- (void)setInfoText:(NSString *)string<br />{<br /> if (textView!=nil) {<br /> [textView removeFromSuperview];<br /> textView = nil;<br /> }</p><p> textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)];<br /> textView.text = string;<br /> textView.hidden = YES;<br /> textView.backgroundColor = [UIColor blueColor];<br /> textView.textColor =[UIColor whiteColor];<br /> [self addSubview:textView];<br />}<br />/*<br /> * SetShadow 設定是否開啟陰影製作效果<br /> * @isShadow BOOL YES 開啟,NO 關閉<br /> */<br />- (void)setShadow:(BOOL)isShadow<br />{<br /> if (!isShadow) {<br /> [[self layer] setShadowOffset:CGSizeMake(0, 0)];<br /> [[self layer] setShadowRadius:0];<br /> [[self layer] setShadowOpacity:1];<br /> [[self layer] setShadowColor:[UIColor whiteColor].CGColor];<br /> return;<br /> }<br /> [[self layer] setShadowOffset:CGSizeMake(3, 3)];<br /> [[self layer] setShadowRadius:3];<br /> [[self layer] setShadowOpacity:0.5];<br /> [[self layer] setShadowColor:[UIColor blackColor].CGColor];<br />}</p><p>#pragma UIGestureRecognizer Handles<br />/*<br /> * 移動圖片處理的函數<br /> * @recognizer 移動手勢<br /> */<br />- (void)handlePan:(UIPanGestureRecognizer *)recognizer {</p><p> if (!isPanEnable) {<br /> return;<br /> }<br /> [self setShadow:YES];<br /> CGPoint translation = [recognizer translationInView:parentview];<br /> recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,<br /> recognizer.view.center.y + translation.y);<br /> [recognizer setTranslation:CGPointMake(0, 0) inView:parentview];</p><p> if (recognizer.state == UIGestureRecognizerStateEnded) {<br /> [UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{<br /> recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);<br /> } completion:nil];</p><p> [self setShadow:NO];</p><p> }</p><p>}<br />/*<br /> * handPinch 縮放的函數<br /> * @recognizer UIPinchGestureRecognizer 手勢辨識器<br /> */<br />- (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{<br /> if (!isPinchEnable) {<br /> return;<br /> }<br /> imageSize*=recognizer.scale;<br /> recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);<br /> if (recognizer.state==UIGestureRecognizerStateEnded) {</p><p> [UIView animateWithDuration:.35 animations:^{<br /> if (imageSize >=1 && imageState == UIImageExNormal) {<br /> recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform,imageScale/imageSize, imageScale/imageSize);<br /> imageState = UIImageExFull;<br /> }<br /> else if(imageSize<1 && imageState == UIImageExFull)<br /> {<br /> recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1/(imageScale*imageSize), 1/(imageScale*imageSize));<br /> imageState = UIImageExNormal;<br /> }else {<br /> recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1/imageSize,1/imageSize);<br /> }</p><p> [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{<br /> recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);<br /> } completion:nil];<br /> recognizer.scale = 1;<br /> imageSize = 1;<br /> }];</p><p> }<br /> recognizer.scale = 1;</p><p>}</p><p>/*<br /> * handleRotate 旋轉的函數<br /> * recognizer UIRotationGestureRecognizer 手勢辨識器<br /> */<br />- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer{<br /> if (!isRotateEnable) {<br /> return;<br /> }<br /> imageRotation+=recognizer.rotation;<br /> recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);<br /> if (recognizer.state==UIGestureRecognizerStateEnded) {<br /> [UIView animateWithDuration:.35 animations:^{<br /> recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, -imageRotation);<br /> recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);<br /> }];</p><p> imageRotation=0;<br /> }<br /> recognizer.rotation = 0;<br />}</p><p>/*<br /> * handleTap 觸摸函數<br /> * @recognizer UITapGestureRecognizer 觸摸辨識器<br /> */<br />-(void) handleTap:(UITapGestureRecognizer *)recognizer<br />{<br /> if (!isTap) {<br /> return;<br /> }<br /> if (textView.hidden) {<br /> [UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{<br /> textView.hidden = NO;<br /> textView.frame = CGRectMake(0, 0, 120, 30);<br /> } completion:nil];<br /> }else {<br /> [UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{</p><p> textView.frame = CGRectMake(0, 0, 0, 30);<br /> } completion:^(BOOL finished){<br /> if (finished){<br /> textView.hidden = YES;<br /> }<br /> }];<br /> }</p><p>}</p><p>#pragma UIGestureRecognizerDelegate<br />/*<br /> * gestureRecognizer 實現了委託,從而實現可以同時接受多個手勢<br /> * @return YES 則可以接受多個手勢,NO 則同時只能接受一個手勢<br /> */<br />- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {<br /> return YES;<br />}</p><p>@end<br />