【IOS】扩展UIImageViewEx实现:手…

  1. #import <UIKit/UIKit.h>  
  2. #import <QuartzCore/QuartzCore.h>  
  3.   
  4. typedef enum 
  5.     UIImageExNormal 0,  
  6.     UIImageExFull  
  7. }UIImageExState;  
  8.   
  9. @interface UIImageViewEx UIImageView<UIGestureRecognizerDelegate>  
  10.  
  11.     UIView *parentview;         //父窗口,即用将UIImageEx所加到的UIView  
  12.       
  13.     BOOL isPanEnable;           //是否可以移动  
  14.     BOOL isPinchEnable;         //是否可以放大缩小  
  15.     BOOL isRotateEnable;        //是否可以旋转  
  16.     BOOL isTap;                 //是否可以点击触摸  
  17.       
  18.     UIImageExState imageState;  //图片当前状态  
  19.   
  20.     CGFloat imageScale;        //最大缩放的倍数   
  21.     CGFloat imageSize;         //记录图片的累计缩放  
  22.     CGFloat imageRotation;     //记录图片的原始角度  
  23.     CGPoint imagePoint;        //记录图片的原始位置  
  24.       
  25.     UITextView *textView;      //动态弹出的文本  
  26.       
  27.  
  28.   
  29. @property (nonatomic,retain) UIView *parentview;  
  30. @property (nonatomic) CGFloat imageSize;  
  31. @property (nonatomic) CGFloat imageRotation;  
  32. @property (nonatomic) CGPoint imagePoint;  
  33.   
  34. @property  BOOL isPanEnable;  
  35. @property  BOOL isRotateEnable;  
  36. @property  BOOL isPinchEnable;  
  37. @property  BOOL isTap;  
  38.   
  39. (void)handlePan:(UIPanGestureRecognizer *)recognizer;  
  40. (void)handlePinch:(UIPinchGestureRecognizer *)recognizer;  
  41. (void)handleRotate:(UIRotationGestureRecognizer *)recognizer;  
  42. (void)handleTap:(UITapGestureRecognizer *)recognizer;  
  43.   
  44. //必须设置的  
  45. (void)setScaleAndRotation:(UIView*)imageView;  
  46. (void)setInfoText:(NSString *)string;  
  47. (void)setShadow:(BOOL)isShadow;  
  48. @end  




[cpp]  view plain copy
  1. #import "UIImageViewEx.h"  
  2.   
  3. @implementation UIImageViewEx  
  4. @synthesize parentview;  
  5. @synthesize isRotateEnable,isPanEnable,isPinchEnable,isTap;  
  6. @synthesize imageSize,imageRotation,imagePoint;  
  7.   
  8.   
  9. (void)setScaleAndRotation:(UIView*) parent  
  10.  
  11.     parentview=parent;  
  12.     parentview.userInteractionEnabled=YES;  
  13.       
  14.     isPanEnable=YES;  
  15.     isPinchEnable=YES;  
  16.     isRotateEnable=YES;  
  17.     isTap YES;  
  18.       
  19.     imageSize=1;  
  20.     imageRotation=0;  
  21.       
  22.     imageScale= self.parentview.frame.size.width/self.frame.size.width;  
  23.     imagePoint=self.frame.origin;  
  24.     self.userInteractionEnabled=YES;  
  25.       
  26.     UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];  
  27.     panRcognize.delegate=self;  
  28.     [panRcognize setEnabled:YES];  
  29.     [panRcognize delaysTouchesEnded];  
  30.     [panRcognize cancelsTouchesInView];  
  31.       
  32.     UIPinchGestureRecognizer *pinchRcognize=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];  
  33.     [pinchRcognize setEnabled:YES];  
  34.     [pinchRcognize delaysTouchesEnded];  
  35.     [pinchRcognize cancelsTouchesInView];  
  36.       
  37.     UIRotationGestureRecognizer *rotationRecognize=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];  
  38.     [rotationRecognize setEnabled:YES];  
  39.     [rotationRecognize delaysTouchesEnded];  
  40.     [rotationRecognize cancelsTouchesInView];  
  41.     rotationRecognize.delegate=self;  
  42.     pinchRcognize.delegate=self;  
  43.       
  44.     UITapGestureRecognizer *tapRecognize [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];  
  45.     tapRecognize.numberOfTapsRequired 1;  
  46.     tapRecognize.delegate self;  
  47.     [tapRecognize setEnabled :YES];  
  48.     [tapRecognize delaysTouchesBegan];  
  49.     [tapRecognize cancelsTouchesInView];  
  50.       
  51.     [self addGestureRecognizer:rotationRecognize];  
  52.     [self addGestureRecognizer:panRcognize];  
  53.     [self addGestureRecognizer:pinchRcognize];  
  54.     [self addGestureRecognizer:tapRecognize];  
  55.      
  56.  
  57.   
  58. (void)setInfoText:(NSString *)string  
  59.  
  60.     if (textView!=nil)  
  61.         [textView removeFromSuperview];  
  62.         textView nil;  
  63.      
  64.       
  65.     textView [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)];  
  66.     textView.text string;  
  67.     textView.hidden YES;  
  68.     textView.backgroundColor [UIColor blueColor];  
  69.     textView.textColor =[UIColor whiteColor];  
  70.     [self addSubview:textView];  
  71.  
  72.   
  73. (void)setShadow:(BOOL)isShadow  
  74.  
  75.     if (!isShadow)  
  76.         [[self layer] setShadowOffset:CGSizeMake(0, 0)];  
  77.         [[self layer] setShadowRadius:0];  
  78.         [[self layer] setShadowOpacity:1];  
  79.         [[self layer] setShadowColor:[UIColor whiteColor].CGColor];  
  80.         return 
  81.      
  82.     [[self layer] setShadowOffset:CGSizeMake(3, 3)];  
  83.     [[self layer] setShadowRadius:3];  
  84.     [[self layer] setShadowOpacity:0.5];  
  85.     [[self layer] setShadowColor:[UIColor blackColor].CGColor];  
  86.  
  87.   
  88. #pragma UIGestureRecognizer Handles  
  89.   
  90. (void)handlePan:(UIPanGestureRecognizer *)recognizer  
  91.       
  92.     if (!isPanEnable)  
  93.         return 
  94.      
  95.     [self setShadow:YES];  
  96.     CGPoint translation [recognizer translationInView:parentview];  
  97.     recognizer.view.center CGPointMake(recognizer.view.center.x translation.x,   
  98.                                          recognizer.view.center.y translation.y);  
  99.     [recognizer setTranslation:CGPointMake(0, 0) inView:parentview];  
  100.       
  101.     if (recognizer.state == UIGestureRecognizerStateEnded)  
  102.         [UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{  
  103.             recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);  
  104.         completion:nil];  
  105.          
  106.         [self setShadow:NO];  
  107.           
  108.      
  109.       
  110.  
  111.   
  112. (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{  
  113.     if (!isPinchEnable)  
  114.         return 
  115.      
  116.     imageSize*=recognizer.scale;  
  117.     recognizer.view.transform CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);  
  118.     if (recognizer.state==UIGestureRecognizerStateEnded)  
  119.       
  120.         [UIView animateWithDuration:.35 animations:^{  
  121.         if (imageSize >=1 && imageState == UIImageExNormal)  
  122.             recognizer.view.transform CGAffineTransformScale(recognizer.view.transform,imageScale/imageSize, imageScale/imageSize);  
  123.             imageState UIImageExFull;  
  124.          
  125.         else if(imageSize<1 && imageState == UIImageExFull)  
  126.          
  127.             recognizer.view.transform CGAffineTransformScale(recognizer.view.transform, 1/(imageScale*imageSize), 1/(imageScale*imageSize));  
  128.             imageState UIImageExNormal;  
  129.         }else  
  130.             recognizer.view.transform CGAffineTransformScale(recognizer.view.transform, 1/imageSize,1/imageSize);  
  131.          
  132.               
  133.         [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{  
  134.                 recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);  
  135.         completion:nil];  
  136.          recognizer.scale 1;  
  137.          imageSize 1;  
  138.     }];  
  139.      
  140.      
  141.     recognizer.scale 1;  
  142.       
  143.  
  144.   
  145.   
  146. (void)handleRotate:(UIRotationGestureRecognizer *)recognizer{  
  147.     if (!isRotateEnable)  
  148.         return 
  149.      
  150.     imageRotation+=recognizer.rotation;  
  151.     recognizer.view.transform CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);  
  152.     if (recognizer.state==UIGestureRecognizerStateEnded)  
  153.         [UIView animateWithDuration:.35 animations:^{  
  154.             recognizer.view.transform CGAffineTransformRotate(recognizer.view.transform, -imageRotation);  
  155.             recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2);  
  156.         }];  
  157.           
  158.         imageRotation=0;  
  159.      
  160.     recognizer.rotation 0;  
  161.  
  162.   
  163.   
  164. -(voidhandleTap:(UITapGestureRecognizer *)recognizer  
  165.  
  166.     if (!isTap)  
  167.         return 
  168.      
  169.     if (textView.hidden)  
  170.         [UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{  
  171.             textView.hidden NO;  
  172.             textView.frame CGRectMake(0, 0, 120, 30);  
  173.         completion:nil];  
  174.     }else  
  175.         [UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{  
  176.               
  177.             textView.frame CGRectMake(0, 0, 0, 30);  
  178.         completion:^(BOOL finished){  
  179.             if (finished){  
  180.               textView.hidden YES;  
  181.              
  182.         }];  
  183.      
  184.      
  185.  
  186.   
  187.   
  188. #pragma UIGestureRecognizerDelegate  
  189.   
  190. (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer  
  191.     return YES;  
  192.  
  193.   
  194.   
  195. @end  


转自: http://blog.csdn.net/toss156/article/details/7371225
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值