zoukankan
html css js c++ java
【IOS】扩展UIImageViewEx实现:手势移动,旋转,缩放(附带一个收缩的文字标签功能)
#import <UIKit/UIKit.h> #import <QuartzCore/QuartzCore.h> typedef enum{ UIImageExNormal = 0, UIImageExFull }UIImageExState; @interface UIImageViewEx : UIImageView<UIGestureRecognizerDelegate> { UIView *parentview; //父窗口,即用将UIImageEx所加到的UIView BOOL isPanEnable; //是否可以移动 BOOL isPinchEnable; //是否可以放大缩小 BOOL isRotateEnable; //是否可以旋转 BOOL isTap; //是否可以点击触摸 UIImageExState imageState; //图片当前状态 CGFloat imageScale; //最大缩放的倍数 CGFloat imageSize; //记录图片的累计缩放 CGFloat imageRotation; //记录图片的原始角度 CGPoint imagePoint; //记录图片的原始位置 UITextView *textView; //动态弹出的文本 } @property (nonatomic,retain) UIView *parentview; @property (nonatomic) CGFloat imageSize; @property (nonatomic) CGFloat imageRotation; @property (nonatomic) CGPoint imagePoint; @property BOOL isPanEnable; @property BOOL isRotateEnable; @property BOOL isPinchEnable; @property BOOL isTap; - (void)handlePan:(UIPanGestureRecognizer *)recognizer; - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer; - (void)handleRotate:(UIRotationGestureRecognizer *)recognizer; - (void)handleTap:(UITapGestureRecognizer *)recognizer; //必须设置的 - (void)setScaleAndRotation:(UIView*)imageView; - (void)setInfoText:(NSString *)string; - (void)setShadow:(BOOL)isShadow; @end
#import "UIImageViewEx.h" @implementation UIImageViewEx @synthesize parentview; @synthesize isRotateEnable,isPanEnable,isPinchEnable,isTap; @synthesize imageSize,imageRotation,imagePoint; /* * SetScaleAndRotation 实现 ImageView的 手势旋转,缩放,和移动 * @parent UIView 父窗口 */ - (void)setScaleAndRotation:(UIView*) parent { parentview=parent; parentview.userInteractionEnabled=YES; isPanEnable=YES; isPinchEnable=YES; isRotateEnable=YES; isTap = YES; imageSize=1; imageRotation=0; imageScale= self.parentview.frame.size.width/self.frame.size.width; imagePoint=self.frame.origin; self.userInteractionEnabled=YES; UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; panRcognize.delegate=self; [panRcognize setEnabled:YES]; [panRcognize delaysTouchesEnded]; [panRcognize cancelsTouchesInView]; UIPinchGestureRecognizer *pinchRcognize=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)]; [pinchRcognize setEnabled:YES]; [pinchRcognize delaysTouchesEnded]; [pinchRcognize cancelsTouchesInView]; UIRotationGestureRecognizer *rotationRecognize=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)]; [rotationRecognize setEnabled:YES]; [rotationRecognize delaysTouchesEnded]; [rotationRecognize cancelsTouchesInView]; rotationRecognize.delegate=self; pinchRcognize.delegate=self; UITapGestureRecognizer *tapRecognize = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)]; tapRecognize.numberOfTapsRequired = 1; tapRecognize.delegate = self; [tapRecognize setEnabled :YES]; [tapRecognize delaysTouchesBegan]; [tapRecognize cancelsTouchesInView]; [self addGestureRecognizer:rotationRecognize]; [self addGestureRecognizer:panRcognize]; [self addGestureRecognizer:pinchRcognize]; [self addGestureRecognizer:tapRecognize]; } /* * setInfoText 设置介绍文字 * @string NSString 显示的文字 */ - (void)setInfoText:(NSString *)string { if (textView!=nil) { [textView removeFromSuperview]; textView = nil; } textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 0, 30)]; textView.text = string; textView.hidden = YES; textView.backgroundColor = [UIColor blueColor]; textView.textColor =[UIColor whiteColor]; [self addSubview:textView]; } /* * SetShadow 设置是否开启阴影效果 * @isShadow BOOL YES 开启,NO 关闭 */ - (void)setShadow:(BOOL)isShadow { if (!isShadow) { [[self layer] setShadowOffset:CGSizeMake(0, 0)]; [[self layer] setShadowRadius:0]; [[self layer] setShadowOpacity:1]; [[self layer] setShadowColor:[UIColor whiteColor].CGColor]; return; } [[self layer] setShadowOffset:CGSizeMake(3, 3)]; [[self layer] setShadowRadius:3]; [[self layer] setShadowOpacity:0.5]; [[self layer] setShadowColor:[UIColor blackColor].CGColor]; } #pragma UIGestureRecognizer Handles /* * 移动图片处理的函数 * @recognizer 移动手势 */ - (void)handlePan:(UIPanGestureRecognizer *)recognizer { if (!isPanEnable) { return; } [self setShadow:YES]; CGPoint translation = [recognizer translationInView:parentview]; recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); [recognizer setTranslation:CGPointMake(0, 0) inView:parentview]; if (recognizer.state == UIGestureRecognizerStateEnded) { [UIView animateWithDuration:0.75 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2); } completion:nil]; [self setShadow:NO]; } } /* * handPinch 缩放的函数 * @recognizer UIPinchGestureRecognizer 手势识别器 */ - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer{ if (!isPinchEnable) { return; } imageSize*=recognizer.scale; recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale); if (recognizer.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:.35 animations:^{ if (imageSize >=1 && imageState == UIImageExNormal) { recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform,imageScale/imageSize, imageScale/imageSize); imageState = UIImageExFull; } else if(imageSize<1 && imageState == UIImageExFull) { recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1/(imageScale*imageSize), 1/(imageScale*imageSize)); imageState = UIImageExNormal; }else { recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, 1/imageSize,1/imageSize); } [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2); } completion:nil]; recognizer.scale = 1; imageSize = 1; }]; } recognizer.scale = 1; } /* * handleRotate 旋转的函数 * recognizer UIRotationGestureRecognizer 手势识别器 */ - (void)handleRotate:(UIRotationGestureRecognizer *)recognizer{ if (!isRotateEnable) { return; } imageRotation+=recognizer.rotation; recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation); if (recognizer.state==UIGestureRecognizerStateEnded) { [UIView animateWithDuration:.35 animations:^{ recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, -imageRotation); recognizer.view.center=CGPointMake(imagePoint.x+self.frame.size.width/2, imagePoint.y+self.frame.size.height/2); }]; imageRotation=0; } recognizer.rotation = 0; } /* * handleTap 触摸函数 * @recognizer UITapGestureRecognizer 触摸识别器 */ -(void) handleTap:(UITapGestureRecognizer *)recognizer { if (!isTap) { return; } if (textView.hidden) { [UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{ textView.hidden = NO; textView.frame = CGRectMake(0, 0, 120, 30); } completion:nil]; }else { [UIView animateWithDuration:0.35 delay:0.15 options:UIViewAnimationOptionTransitionCurlUp animations:^{ textView.frame = CGRectMake(0, 0, 0, 30); } completion:^(BOOL finished){ if (finished){ textView.hidden = YES; } }]; } } #pragma UIGestureRecognizerDelegate /* * gestureRecognizer 实现了委托,从而实现可以同时接受多个手势 * @return YES 则可以接受多个手势,NO 则同时只能接受一个手势 */ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } @end
查看全文
相关阅读:
this is a test from windows live writer 11529
RoR部署方案深度剖析 (转载自javaeye)
Rails每周一题(六): Security Guide(上) (转载)
让你大脑变冷静的28句英文
REST on Rails之自定义路由
REST on Rails之资源嵌套
#### 高薪 ##招聘:。。。。。 待遇非常不错。
JavaEye网站的RoR性能优化经验谈 (转载)
网页打印的分页问题
Ruby Metaclass详解
原文地址:https://www.cnblogs.com/javawebsoa/p/2458411.html
最新文章
rails NoMethodError in controller 解决
Ruby On Rails 开发环境IDE 插件安装
gem install mysql2 安装相关问题
rspec 安装、使用、入门
关于vim的使用
配置ActionMailer使用GMail发送邮件
如何进行高效的Rails单元测试
浅析Ruby on Rails部署方案(转载)
关于测试驱动开发》行为驱动开发
linux 下安装多个不同版本rails的问题
热门文章
使用Rspec进行行为驱动测试
推荐一篇很好的RoR部署方案性能评测
Matz, Koichi访谈(三):多线程
Rails应用性能优化1
Rails开发技巧集锦
Rails 应用的一些gems ,插件
11 Things to Consider Before Deploying Your Rails Application
转载 ,如何采访Ruby/Rails大牛
Ruby attr_accessor 使用,Ruby @成员变量 与 临时变量
for循环与each的区别
Copyright © 2011-2022 走看看