IOS手势

[TOC]

## IOS****手势

学习ios手势,先要了解响应链,ios中只要继承UIResponse对象的都能成为事件的响应者,当用户操作手机屏幕时,所操作的控件就是第一响应者,操作事件一步步向下传递(如果该控件不做处理)

  • 事件响应链的传播路线:
 initial view –> super view –> …..–> view controller –> window –> Application –> AppDelegate
response响应链
  • UIResponse的触碰方法:
/**** 触摸事件方法,对触摸事件进行处理 *****/

- (void)touchesBegan:(NSSet *)touches withEvent:(nullableUIEvent*)event;//触摸开始

- (void)touchesMoved:(NSSet *)touches withEvent:(nullableUIEvent*)event;//触摸移动

- (void)touchesEnded:(NSSet *)touches withEvent:(nullableUIEvent*)event;//触摸结束

- (void)touchesCancelled:(nullableNSSet *)touches withEvent:(nullableUIEvent*)event;//触摸取消

                              

ios中主要的手势:

1.UITapGestureRecognizer //单击手势

�点击作为最常用手势,用于按下或选择一个控件或条目(类似于普通的鼠标点击)

/**
 添加点击手势
 */
- (void)addTapGestureWithTarget:(id)sender {
    
        //1,tap(点击)
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureAction:)];
        //手势点击次数
        tapGesture.numberOfTapsRequired = 1;// Default is 1
        //点击手指数量
        tapGesture.numberOfTouchesRequired = 1;// Default is 1
        //将手势识别器添加到view上
        [sender addGestureRecognizer:tapGesture];

}
/**
 点击手势的响应方法
 */
-(void)tapGestureAction:(UITapGestureRecognizer*)gesture
{
    NSLog(@"%s",__FUNCTION__);
    UILabel *tempLabel = [self.view  viewWithTag:1001];
    NSInteger tapCount = gesture.numberOfTapsRequired;//点击次数
    NSInteger touchCount = gesture.numberOfTouchesRequired;//手指个数
    tempLabel.text = [NSString stringWithFormat:@"手指个数:%ld  点击次数:%ld",(long)touchCount,(long)tapCount];
    
}

2.UIPanGestureRecognizer //拖动手势

拖动用于实现一些页面的滚动,以及对控件的移动功能。

/**
 添加拖拽手势
 */
- (void)addPanGestureWithView:(id)sender {
    //2,pan(平移)
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestureAction:)];
    panGesture.minimumNumberOfTouches = 1;//最少点击次数---
    [sender addGestureRecognizer:panGesture];
}

/**
 拖拽手势相应方法
 */
-(void)panGestureAction:(UIPanGestureRecognizer*)gesture
{
    switch (gesture.state)
    {
            //开始
        case UIGestureRecognizerStateBegan:
        {
            NSLog(@"开始");
        }
            break;
            //改变
        case UIGestureRecognizerStateChanged:
        {
            //1,改变redView的frame
            UILabel *redView = (UILabel *)gesture.view;
            //2,改变的坐标-移动的距离
            CGPoint point = [gesture translationInView:redView];
            
            NSLog(@"%@",NSStringFromCGPoint(point));
            //3,根据移动的距离改变redView的frame
            //CGRectOffset - 根据偏移量改变view的x值和y值
            redView.frame = CGRectOffset(redView.frame, point.x, point.y);
            
            //清空偏移量的累加值
            [gesture setTranslation:CGPointZero inView:redView];
        }
            break;
            //结束
        case UIGestureRecognizerStateEnded:
        {
            NSLog(@"结束");
        }
            break;
            
        default:
            break;
    }
    NSLog(@"%s",__FUNCTION__);
    
}

3.UISwipeGestureRecognizer //轻扫手势

横扫手势用于激活列表项的快捷操作菜单

/**
 添加轻扫手势
 */
- (void)addSwipeGestureWithView:(UIView *)aView {
    //3,swipe(轻扫)
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGestureAction:)];
    //8个方向
    //direction - 方向
    /*
     typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
     UISwipeGestureRecognizerDirectionRight = 1 << 0,
     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,
     UISwipeGestureRecognizerDirectionUp    = 1 << 2,
     UISwipeGestureRecognizerDirectionDown  = 1 << 3
     };
     UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionUp = 6
     UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionDown = 9
     UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionDown = 10
     UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionUp = 5
     */
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;

    [aView addGestureRecognizer:swipeGesture];
    
}
/**
 轻扫手势响应方法
 */
-(void)swipeGestureAction:(UISwipeGestureRecognizer*)gesture
{
    NSLog(@"%s",__FUNCTION__);
    int  direction = gesture.direction;
    UILabel * tempLabel = (UILabel *)gesture.view;
    if (direction == 1) {
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:右"];
    }else if (direction == 2){
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:左"];
    
    }else if (direction == 3){
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:上"];
        
    }else if (direction == 4){
        tempLabel.text = [NSString stringWithFormat:@"滑动方向:下"];
        
    }
}

4.UIPinchGestureRecognizer //捏合手势

即放大缩小

/**
 添加捏合手势
 */
- (void)addPinchGestureWithView:(UIView *)aView {

    //4,pinch捏和
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGestureAction:)];
    [self.pinchImg addGestureRecognizer:pinchGesture];
    
}
/**
 捏合手势响应方法
 */
-(void)pinchGestureAction:(UIPinchGestureRecognizer*)gesture
{
    switch (gesture.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            //手势开始
            //记录,当前view的frame,作为原始frame
            redViewRect = gesture.view.frame;
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            //1,拿到view
            UIView *redView = gesture.view;
            //2,改变view的frame
            
            //scale 缩放后的比例,跟1(原来的frame)
            CGFloat dx = CGRectGetWidth(redViewRect)*(1-gesture.scale); //宽度变化量
            CGFloat dy = CGRectGetHeight(redViewRect)*(1-gesture.scale);//高度变化量
            //dx dy 缩放的偏移量
    
            redView.frame = CGRectInset(redViewRect, dx, dy);
            
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            
        }
            break;
            
        default:
            break;
    }
    
    
}

5.UIScreenEdgePanGestureRecognizer//边缘滑入

滑动用于实现页面的快速滚动和翻页的功能。

/**
 添加边缘滑入手势
 */
- (void)addScreenEdgePanGestureWithView:(UIView *)aView {

    //5,ScreenEdgePan边缘划入
    UIScreenEdgePanGestureRecognizer *sePanGesture = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(seGestureAction:)];
    //划入的位置-(边缘的位置)
    /*
    typedef NS_OPTIONS(NSUInteger, UIRectEdge) {
        UIRectEdgeNone   = 0,
        UIRectEdgeTop    = 1 << 0,
        UIRectEdgeLeft   = 1 << 1,
        UIRectEdgeBottom = 1 << 2,
        UIRectEdgeRight  = 1 << 3,
        UIRectEdgeAll    = UIRectEdgeTop | UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight
    }*/
    sePanGesture.edges = UIRectEdgeLeft;
    [aView addGestureRecognizer:sePanGesture];

}
/**
 边缘划入响应方法
 */
-(void)seGestureAction:(UIScreenEdgePanGestureRecognizer*)gesture
{
    NSLog(@"%s",__FUNCTION__);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"边缘滑入" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

6.UIRotationGestureRecognizer //旋转手势

即将视图围绕中心点转动

/**
 添加旋转手势
 */
- (void)addRotationGestureWithView:(UIView *)aView {
    //6,rotation旋转
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGestureAction:)];
    [aView addGestureRecognizer:rotationGesture];
}
/**
 旋转手势响应方法
 */
-(void)rotationGestureAction:(UIRotationGestureRecognizer*)gesture
{
    CGFloat  rotation = gesture.rotation;//旋转的弧度
    CGFloat velocity = gesture.velocity;//旋转速度 (radians/second)
    gesture.view.transform = CGAffineTransformMakeRotation(rotation);
    UILabel *tempLabel = (UILabel *)gesture.view;
    tempLabel.text = [NSString stringWithFormat:@"旋转弧度:%f \n 旋转速度:%f",rotation,velocity];
    

}

7.UILongPressGestureRecognizer //长按手势

将出现编辑菜单

/**
 添加长按手势
 */
- (void)addLongPressGestureWithView:(UIView *)aView {

    //7,longPress长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    /* numberOfTouchesRequired这个属性保存了有多少个手指点击了屏幕,因此你要确保你每次的点击手指数目是一样的,默认值是为 0. */
    longPress.numberOfTouchesRequired = 1;//手指个数
    //longPress.minimumPressDuration = 2;//按的最少时长
    /*最大100像素的运动是手势识别所允许的  Default is 10.*/
    longPress.allowableMovement = 100; //
    /*这个参数表示,两次点击之间间隔的时间长度。Default is 0.5.*/
    longPress.minimumPressDuration = 1.0;
    [aView addGestureRecognizer:longPress];

}
/**
 长按相应方法
 */
-(void)longPressAction:(UILongPressGestureRecognizer *)gesture
{
    NSLog(@"%s",__FUNCTION__);
    //弹出menu
    switch (gesture.state) {
        case UIGestureRecognizerStateBegan:
        {
            //UIMenuController menu控制器
            //系统的粘贴复制的小弹框
            //menuController  单例
            UIMenuController *ctr = [UIMenuController sharedMenuController];
           /*自定义Menu按钮
            //menu按钮
            UIMenuItem *mItem = [[UIMenuItem alloc]initWithTitle:@"自定义" action:@selector(longPressMenuAction)];
            
            UIMenuItem *mItem1 = [[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(longPressMenuAction)];
            UIMenuItem *mItem2 = [[UIMenuItem alloc]initWithTitle:@"粘贴" action:@selector(longPressMenuAction)];
            
            //将item添加到controller中
            ctr.menuItems = [NSArray  arrayWithObjects:mItem,mItem1,mItem2,nil];//@[mItem,mItem1,mItem2];
            */
            
            //获得手指点击的位置
            CGPoint point = [gesture locationInView:gesture.view];
            //设置显示的位置
            [ctr setTargetRect:CGRectMake(point.x, point.y, 0, 0) inView:gesture.view];
            //显示menu工具条
            [ctr setMenuVisible:YES animated:YES];
            
        }
            break;
        default:
            break;
    }
}

##****参考文档:

浅谈iOS手势

ios事件处理

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 227,818评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,185评论 3 414
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 175,656评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,647评论 1 309
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,446评论 6 405
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,951评论 1 321
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,041评论 3 440
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,189评论 0 287
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,718评论 1 333
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,602评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,800评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,316评论 5 358
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,045评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,419评论 0 26
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,671评论 1 281
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,420评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,755评论 2 371

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,151评论 4 61
  • 手势识别器是附加到视图的对象,将低级别事件处理代码转换为更高级别的操作,它允许视图以控件执行的方式响应操作。 手势...
    坤坤同学阅读 4,106评论 0 9
  • 2017年11月25日 第一是去书店,偶遇书籍 第二是读报纸阅读,自己不会刻意关注的事情 第三是写短文,一定注意引...
    温伯凉_读书笔记阅读 232评论 0 0
  • 笔者来自小城市,在我进入魔都之前,遇到的姑娘都是很美好的。她们要不就是脾气好,要不就是声音甜。她们平日里可能出门从...
    CaptainKyo阅读 1,684评论 0 5
  • 雷军做手机先市调 雷军在做手机之前是做过详细市调分析的,发现苹果手机是全球做手机第一,而且就只有几款产品,保持每年...
    育儿师徐珊珊阅读 1,482评论 0 3