6种手势

news/2024/11/7 6:47:23/

1.手势UIGestureRecognizer

UIGestureRecognizer是一个抽象类我们不能直接使用,他有6个子类(tap,pan,swip,long,scale,raition)。他的父类NSObject。

2.常用属性和函数

//函数
创建-(instancetype)initWithTarget:target action(SEL)action单独添加target方法 可以添加多个手势-(void)addTarget:target action:(SEL)action删除手势-(void)removeTarget:(id)target action:(SEL)action- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //手势依赖(手势互斥)方法  设置手势优先级 A requireRecognizerToFail B 手势条件都满足时B触发,A不会//获取在传入view的点击位置的信息方法- (CGPoint)locationInView:(nullable UIView*)view;//(touchIndex 是第几个触摸点)用来获取多触摸点在view上位置信息的方法- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(nullable UIView*)view;常用属性
UIGestureRecognizerState state; 手势状态 只读//手势状态枚举值typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {UIGestureRecognizerStatePossible,   // 默认的状态,这个时候的手势并没有具体的情形状态UIGestureRecognizerStateBegan,      // 手势开始被识别的状态UIGestureRecognizerStateChanged,    // 手势识别发生改变的状态UIGestureRecognizerStateEnded,      // 手势识别结束,将会执行触发的方法UIGestureRecognizerStateCancelled,  // 手势识别取消UIGestureRecognizerStateFailed,     // 识别失败,方法将不会被调用UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEndedid <UIGestureRecognizerDelegate> delegate;代理BOOL enabled 手势是否有效 默认YESUIview *view 获取手势所在的view 只读BOOL cancelsTouchesInView 取消view上面的touch事件响应 默认YES 就是拦截了,手势触发了就不会发送touchesCancelled:withEvent:消息BOOL delaysTouchsBegan    延迟touch事件开始 默认NO
BOOL delaysTouchsEnded    延迟touch事件结束 默认YES
NSArray<NSNumber *> *allowedTouchTypes 允许touch类型的数组
NSArray<NSNumber *> *allowedPressTypes 允许按压press的类型数组
BOOL requiresExclusiveTouchType  是否只允许一种touchType 类型,是否同时只接受一种触摸类型 默认YES
NSUInteger numberOfTouches;  获取触摸点数代理方法开始进行手势识别时调用,返回NO,手势识别失败-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;手势触发屏幕后回调方法,返回NO手势识别失败-(BOOL)getureRecognizer:(uiGestureRecognizer*)gestureRecognizer;

2.子类类型

UITapGestureRecognizer 点击

  UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapclack)];//点击次数 默认单击tapGesture.numberOfTapsRequired = 2;//同时点击的手指数//  tapGesture.numberOfTouchesRequired = 2;[self.imageView addGestureRecognizer:tapGesture];-(void)tapclack{NSLog(@"点击了");}

UIPinchGestureRecognizer 捏合
 

 //属性 CGFloat scale 缩放比例// CGFloat velocty  设置捏合速度 只读UIPinchGestureRecognizer *pingGest = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinClack:)];[self.imageView addGestureRecognizer:pingGest];-(void)pinClack:(UIPinchGestureRecognizer *)pin{NSLog(@"%f,%f",pin.scale,pin.velocity);//固定写法pin.view.transform = CGAffineTransformScale(pin.view.transform, pin.scale, pin.scale);//重置缩放系数(否则系数会累加)pin.scale = 1.0;
}

 

 

UIRotationGestureRecognizer 旋转

-(void)rotation{
//属性 CGFloat rotation 旋转角度
//旋转速度 CGFloat velocity 只读UIRotationGestureRecognizer *rotationGest = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationClack:)];[self.imageView addGestureRecognizer:rotationGest];}-(void)rotationClack:(UIRotationGestureRecognizer *)rotationGest{rotationGest.view.transform = CGAffineTransformRotate(rotationGest.view.transform,rotationGest.rotation);NSLog(@"旋转角度%f",rotationGest.rotation);}

UISwipeGestureRecognizer 轻扫

-(void)swipe{//常用属性 NSInteger numberOfTouchesRequired 触发轻扫手势所需触摸点数量//UISwipeGestureRecognizerDirection direction 轻扫手势方向//左UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];//轻扫方向swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;[self.imageView addGestureRecognizer:swipeLeft];//右UISwipeGestureRecognizer *swipRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];swipRight.direction = UISwipeGestureRecognizerDirectionRight;[self.imageView addGestureRecognizer:swipRight];//上UISwipeGestureRecognizer *swipUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];swipUp.direction = UISwipeGestureRecognizerDirectionUp;[self.imageView addGestureRecognizer:swipUp];//下UISwipeGestureRecognizer *swipDrown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipClack:)];swipDrown.direction = UISwipeGestureRecognizerDirectionDown;[self.imageView addGestureRecognizer:swipDrown];
}-(void)swipClack:(UISwipeGestureRecognizer *)swipe{switch (swipe.direction) {case UISwipeGestureRecognizerDirectionLeft:NSLog(@"左");break;case UISwipeGestureRecognizerDirectionRight:NSLog(@"右");break;case UISwipeGestureRecognizerDirectionUp:NSLog(@"上");break;case UISwipeGestureRecognizerDirectionDown:NSLog(@"下");break;default:break;}}

 

UIPanGestureRecognizer 滑动

-(void)pan{/*常用属性NSUInteger minimumNumberOfTouchches 设置触发移动拖拽最少触发点数量 默认1NSUInteger maximumNumberOfTouches   设置触发拖拽最多触发点-(void)setTranslation:(CGPoint)translation inView(UIView *)view 设置当前位置-(CGPoint)translationInView:(UIView)view 获取当前位置-(void)setVelocityInView:(UIView *)view 设置平移速度-(CGPoint)velocityInView:(UIView *)view 获取平移速度*/UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panClack:)];[self.imageView addGestureRecognizer:panGest];}-(void)panClack:(UIPanGestureRecognizer *)pan{//获取imageview在self.view上的便宜量CGPoint point = [pan translationInView:self.view];NSLog(@"%f--%f",point.x,point.y);//移动到的中心点 本来中心点+便宜量CGPoint newCenter = CGPointMake(pan.view.center.x + point.x, pan.view.center.y+point.y);//设置拖动范围newCenter.x = MAX(pan.view.frame.size.width,newCenter.x);//最小范围//newCenter.x = MIN();//没错调用后,重之手势便宜量[pan setTranslation:CGPointZero inView:self.imageView];if (pan.state == UIGestureRecognizerStateBegan) {NSLog(@"开始响应");}else if (pan.state == UIGestureRecognizerStateChanged){NSLog(@"手势状态发生改变");}else{NSLog(@"手势结束");}}

 

UILongPressGestureRecognizer 长按

-(void)LongPress{/*属性NSUInteger numberOfTapsRequired。    设置触发前的点击次数NSUInteger numberOfTouchesReauired  设置触发的触摸点数CGTimeInterval minimumPressDuration 设置最短长按时间CGFloat allowableMovement 设置在按触时允许移动的最大距离,默认10像素*/UILongPressGestureRecognizer *longPressGest = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressClack:)];//触摸点 默认1longPressGest.numberOfTapsRequired = 1;//设置最短长按时间 秒 默认 0.5longPressGest.minimumPressDuration = 1;//设置手势识别期间所允许的手势可移动范围//longPressGest.allowableMovement = 10;[self.imageView addGestureRecognizer:longPressGest];}
-(void)longPressClack:(UILongPressGestureRecognizer *)longpress{if (longpress.state == UIGestureRecognizerStateBegan) {NSLog(@"长按手势开始响应");}else if (longpress.state == UIGestureRecognizerStateChanged){NSLog(@"长按手势状态发生改变");}else{NSLog(@"长按手势结束");}}

3.解决手势冲突

 - (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer; //手势依赖(手势互斥)方法  设置手势优先级 A requireRecognizerToFail B 手势条件都满足时B触发,A不会

 


http://www.ppmy.cn/news/180479.html

相关文章

什么是三次握手、什么是四次握手

在上一篇我们了解的TCP的报文格式和TCP连接管理机制&#xff1a;TCP初认识 今天来理解什么是三次握手&#xff0c;什么是四次挥手 图1 TCP连接管理 什么是三次握手 在网络数据传输中&#xff0c;传输层协议TCP(传输控制协议)是建立连接的可靠传输&#xff0c;TCP建立连接的过程…

什么是三次握手?

文章目录 三次握手三次握手的目的&#xff1a;名词解释&#xff1a;具体流程&#xff1a; 三次握手 三次握手的目的&#xff1a; 当两台主机要通信时&#xff0c;确认两台主机都具备发送和接收的能力。 名词解释&#xff1a; SYN 1 表示请求建立连接 SYN 0 …

孩子快抓住妈妈的手

孩子快抓住妈妈的手 说明&#xff1a;来自网络 孩子 快抓紧妈妈的手 去天堂的路 太黑了 妈妈怕你 碰了头 快 抓紧妈妈的手 让妈妈陪你走 妈妈 怕 天堂的路 太黑 我看不见你的手 自从 倒塌的墙 把阳光夺走 我再也看不见 你柔情的眸 孩子 你走吧 前面的路 …

都握手了,怎么能少得了挥手嘞

四次挥手只有一个目的&#xff1a;让双方都知道对方想要断开连接并且把该传输的数据都传输完 假设是由有客户端发起的断开请求&#xff1a; 第一次挥手&#xff1a;客户端给服务器端发送一个FIN标志位和seq&#xff0c;就像是在告诉服务器&#xff0c;"我想跟你断开连接&a…

练手~~~

文本操作 逆转字符串——输入一个字符串&#xff0c;将其逆转并输出。 拉丁猪文字游戏——这是一个英语语言游戏。基本规则是将一个英语单词的第一个辅音音素的字母移动到词尾并且加上后缀-ay&#xff08;譬如“banana”会变成“anana-bay”&#xff09;。可以在维基百科上了解…

三次握手~

在TCB传输中为了保证文件传输的可靠性&#xff08;可靠性不等于安全性&#xff0c;可靠性是指A发送的信息&#xff0c;A知道B是否收到&#xff0c;安全性是指传输的数据会不会被发生截取和篡改&#xff09;做了一系列操作&#xff0c;其中包括了确认应答&#xff08;核心&#…

三握四挥

一、VUE和Dj ango工作流程&#xff1a; 二、Django中间件的作用&#xff1a; 三、Django中间件请求顺序&#xff1a; 四、总结的(终极版)&#xff1a; 五、三次握手&#xff0c;四次挥手&#xff1a; 六、HTTP的三次握手&#xff0c;四次挥手总结&#xff1a; 舔狗的过程 七、H…