6种手势的讲解
来源:互联网 发布:南风知我意txt下载西州 编辑:程序博客网 时间:2024/05/20 20:01
在这里主要讲解一下自己理解的关于6种手势的相关内容
六种手势包括:
轻击手势(TapGestureRecognizer),
轻扫手势 (SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
(1)、轻击手势(包括单击和双击)
//单击
{ UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; [imageV addGestureRecognizer:tap]; } //方法 -(void)tapAction:(UITapGestureRecognizer*)tap{ NSLog(@"单击"); }
//双击
{UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTapAction:)]; doubleTap.numberOfTapsRequired = 2; [imageV addGestureRecognizer:doubleTap]; //双击失败了才算单击(双击成功就是双击) [tap requireGestureRecognizerToFail:doubleTap]; }-(void)doubleTapAction:(UITapGestureRecognizer*)tap{ NSLog(@"双击");}
(2)轻扫手势
{UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)]; //属性:设置轻扫的方向(默认 右)//swipe.direction = UISwipeGestureRecognizerDirectionRight; [imageV addGestureRecognizer:swipe];}-(void)swipeAction:(UISwipeGestureRecognizer*)swipe{ NSLog(@"轻扫");}
(3)长按手势
{UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)]; [imageV addGestureRecognizer:longPress]; //其中属性allowableMovement:是允许抖动的范围}-(void)longPressAction:(UILongPressGestureRecognizer*)longPress{ if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"长按开始"); }else if (longPress.state ==UIGestureRecognizerStateEnded){ NSLog(@"长按结束"); } }
(4)拖动手势
{UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; [imageV addGestureRecognizer:pan]; //轻扫和移动的冲突,一旦有冲突,默认移动 //轻扫失败算移动 (轻扫成功就是轻扫) [pan requireGestureRecognizerToFail:swipe];}-(void)panAction:(UIPanGestureRecognizer*)pan{// NSLog(@"移动..."); //设置当前位置的坐标(相对 起始点 ) CGPoint p1 =[pan translationInView:imageV]; NSLog(@"p1 = %@",NSStringFromCGPoint(p1)); //移动点的速度// CGPoint v1 = [pan velocityInView:imageV];// NSLog(@"v1 = %@",NSStringFromCGPoint(v1)); //设置当前位置// CGPoint p2 = CGPointMake(100, 100);// [pan setTranslation:p2 inView:imageV];}
(5)捏合手势
{UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)]; [imageV addGestureRecognizer:pinch];}-(void)pinchAction:(UIPinchGestureRecognizer*)pinch{ /*属性 scale:缩放的大小比例 velocity:缩放的速度 */ if (pinch.state ==UIGestureRecognizerStateChanged) { NSLog(@"一直捏合"); CGFloat pScale =pinch.scale; pinch.view.transform = CGAffineTransformMakeScale(pScale, pScale); }else if (pinch.state == UIGestureRecognizerStateEnded){ NSLog(@"捏合结束"); [UIView animateWithDuration:0.2 animations:^{ pinch.view.transform =CGAffineTransformIdentity; }]; }}
(6)旋转手势
{UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)]; [imageV addGestureRecognizer:rotation];}-(void)rotationAction:(UIRotationGestureRecognizer*)rotation{ /* rotation:旋转的弧度 角度:0~360 弧度:0~2π velocity:旋转的速度 */ if (rotation.state == UIGestureRecognizerStateChanged) { NSLog(@"一直旋转"); CGFloat angle = rotation.rotation; rotation.view.transform = CGAffineTransformMakeRotation(angle); }else if (rotation.state == UIGestureRecognizerStateEnded){ NSLog(@"旋转结束"); [UIView animateWithDuration:0.2 animations:^{ rotation.view.transform = CGAffineTransformIdentity; }]; }}
在模拟器上进行手势操作时可能会出现错误,建议尽量在真机上进行操作。
0 0
- 6种手势的讲解
- Android的手势讲解
- iOS自定义弹出视图,以及简单的手势讲解
- 6种手势详解
- iOS - 6种手势
- 手势UIGestureRecognizer 几种手势的使用实现
- ios开发,6种手势的创建方法
- IOS开发中手势滑动的6种技巧
- ios中的6种手势
- 6种手势交互Demo
- IOS 触摸事件、手势识别讲解
- iOS疯狂讲解之手势识别器
- [深入剖析React Native]手势响应讲解
- [深入剖析React Native]手势响应讲解
- iOS中常用的六种手势
- UI中提供的七种手势
- IOS-六种手势的简单使用
- 定义响应的手势类:长按手势,缩放手势
- Set重写hashCode()和equals()的实例
- python random从集合中随机选择元素
- 随堂笔记7.30:枚举,构建者模式,内部类的详细介绍-------2
- C#控制台 判断字符串的索引值为N的字符是字母,是数字
- hduThe All-purpose Zero+LIS
- 6种手势的讲解
- 第二篇:SWT和JavaFX选哪个?
- 第一天的Android学习
- sqlite3_column
- JDBC查看数据表结构
- 实用工具:VS上python开发插件(python tools for visual studio)
- Java入门级知识小结
- HDU 5444-二叉树的遍历+建树
- poj3155 Hard Life 【最大密度图 01分数规划】