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
查看全文
相关阅读:
性能计数器
SpringBoot_数据访问-整合JPA
SpringBoot_数据访问-整合MyBatis(二)-注解版MyBatis
SpringBoot_数据访问-整合Druid&配置数据源监控
SpringBoot_数据访问-JDBC&自动配置原理
八字克妻口诀
没想到,我能这么细心地看算法
辰戌丑未,四库
The jar of divisors
分布式系统阅读笔记(十六)-----事务和并发控制
原文地址:https://www.cnblogs.com/javawebsoa/p/2458411.html
最新文章
URL最大长度限制
关于手机微网站ICP备案
JSP中request getParameter和getAttribute不同(转载)
函数式编程摘录
开始对函数式编程 产生了尊崇感,因为Spring4.x ,Grooxy,Lisp,网易出来伞哥和他的博客
测试
navicat and connection is being used
MySQL必知必会
mysql 1130 Navicat for MySQL 连接MySQL 8.0 出现1130错误
js获取当前域名、Url、相对路径和参数以及指定参数
热门文章
评论中的敏感词过滤
Laravel 中使用 Laravel-Excel 美化
Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)
使用 Laravel-Excel 和流的方法导出 Excel
Laravel-admin 消息提醒、播放音频、点击跳转
Source优化
Target优化
System优化
相关概念
DBA_OBJECTS
Copyright © 2011-2022 走看看