关闭

iOS开发之手势识别汇总

标签: iOS手势汇总
152人阅读 评论(0) 收藏 举报
分类:

本文出处刚刚在线:http://www.superqq.com/blog/2015/01/14/ioskai-fa-zhi-shou-shi-shi-bie-hui-zong/

iOS开发中手势识别有六种:

轻击手势(TapGestureRecognizer),

轻扫手势 (SwipeGestureRecognizer),

长按手势(LongPressGestureRecognizer),

拖动手势(PanGestureRecognizer),

捏合手势(PinchGestureRecognizer),

旋转手势(RotationGestureRecognizer),

1,轻击手势(TapGestureRecognizer)

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //点击次数
tapGesture.numberOfTouchesRequired = 1; //点击手指数
[self.view addGestureRecognizer:tapGesture];

//轻击手势触发方法
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
    //your code
}

2,长按手势(LongPressGestureRecognizer)

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];

//长按手势触发方法
-(void)longPressGesture:(id)sender
{
    UILongPressGestureRecognizer *longPress = sender;
    if (longPress.state == UIGestureRecognizerStateBegan)
    {
       //your code
    }
}

说明:长按手势的常用状态如下

开始:UIGestureRecognizerStateBegan

改变:UIGestureRecognizerStateChanged

结束:UIGestureRecognizerStateEnded

取消:UIGestureRecognizerStateCancelled

失败:UIGestureRecognizerStateFailed

3,轻扫手势(SwipeGestureRecognizer)

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右
[self.view addGestureRecognizer:swipeGesture];

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左
[self.view addGestureRecognizer:swipeGestureLeft];

//轻扫手势触发方法
-(void)swipeGesture:(id)sender
{

    UISwipeGestureRecognizer *swipe = sender;

    if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        //向左轻扫
    }

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
    {
        //向右轻扫
    }
}

UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右
[self.view addGestureRecognizer:swipeGesture];

UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左
[self.view addGestureRecognizer:swipeGestureLeft];

//轻扫手势触发方法
-(void)swipeGesture:(id)sender
{

UISwipeGestureRecognizer *swipe = sender;

if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
    //向左轻扫
}

if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
    //向右轻扫
}

}
4,捏合手势(PinchGestureRecognizer)

UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];

    ////捏合手势触发方法
-(void) pinchGesture:(id)sender
{
    UIPinchGestureRecognizer *gesture = sender;
    //手势改变时
    if (gesture.state == UIGestureRecognizerStateChanged)
    {
         //捏合手势中scale属性记录的缩放比例
        _imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
    }

    //结束后恢复
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.5 animations:^{
            _imageView.transform = CGAffineTransformIdentity;//取消一切形变
        }];
    }
}

5,拖动手势(PanGestureRecognizer)

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];

//拖动手势触发方法
-(void) panGesture:(id)sender
{
    UIPanGestureRecognizer *panGesture = sender;
    CGPoint movePoint = [panGesture translationInView:self.view];
    //your code
}

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];

//拖动手势触发方法
-(void) panGesture:(id)sender
{
UIPanGestureRecognizer *panGesture = sender;
CGPoint movePoint = [panGesture translationInView:self.view];
//your code
}
6,旋转手势(RotationGestureRecognizer)

UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];

//旋转手势触发方法
-(void)rotationGesture:(id)sender
{
    UIRotationGestureRecognizer *gesture = sender;
    if (gesture.state==UIGestureRecognizerStateChanged)
    {
        _imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
    }
    if(gesture.state==UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:1 animations:^{
            _imageView.transform=CGAffineTransformIdentity;//取消形变
        }];
    }
}
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【iOS开发-手势】iOS中各种手势总结

iOS中支持的手势事件 UITapGestureRecognizer(敲击) UIPinchGestureRecognizer(捏合,用于缩放) UIPanGestureRecognizer(拖拽)...
  • ttf1993
  • ttf1993
  • 2015-05-19 17:27
  • 1143

【iOS开发】事件处理之手势识别(一)

手势识别手势识别将低级别事件处理代码转换成更高一级的行动。它们是对连接至一个视图,它允许,以便向操作进行响应的控制的方式做对象。手势识别解释触摸以确定它们是否对应于特定的手势,诸如滑动,挤压或转动。如...
  • Hanrovey
  • Hanrovey
  • 2016-06-07 13:18
  • 5752

iOS开发中多手势冲突解决方案

iOS开发中多手势冲突解决方案iOS开发中我们经常会使用到手势,合理的运用手势会使我们的app拥有更好的体验,但是在使用手势的同时也要注意避免手势和手势间,以及手势和其他控件间的冲突。本文就主要介绍在...
  • wlaizff
  • wlaizff
  • 2016-09-07 17:25
  • 4040

iOS开发之手势识别汇总

iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer), 轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGes...
  • Mr_Big_Kong
  • Mr_Big_Kong
  • 2015-07-03 10:01
  • 284

IOS开发UI进阶篇 — 基于手势识别的侧滑展开菜单栏

今天给大家详细介绍一下IOS手势的用法,最后给大家分享一点干货,《基于手势识别的侧滑展开菜单栏》,这个功能在开发中会经常用到,是非常重要的。 一、UIGestureRecognizer介绍 手势在i...
  • u010037928
  • u010037928
  • 2016-02-28 20:33
  • 2766

iOS开发学习之触摸事件和手势识别(转

iOS的输入事件 触摸事件 手势识别 手机摇晃 一、iOS的输入事件   触摸事件(滑动、点击) 运动事件(摇一摇、手机倾斜、行走),不需要人为参与的 ...
  • yangchen9931
  • yangchen9931
  • 2015-05-05 15:01
  • 443

iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控 2014-09-02 06:33 by KenshinCui, 45252 阅读, 42 评论, 收藏, 编辑 -- iOS...
  • ZhengYanFeng1989
  • ZhengYanFeng1989
  • 2016-03-14 14:03
  • 408

iOS开发之手势识别

感觉有必要把iOS开发中的手势识别做一个小小的总结。在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextVi...
  • ILOVELITTLEFATTER
  • ILOVELITTLEFATTER
  • 2015-02-04 22:06
  • 283

iOS开发 - 手势识别(UIGestureRecognizer)

UIGestureRecognizer为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一...
  • wangzi11322
  • wangzi11322
  • 2015-04-27 10:15
  • 2635

iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计。今天我们就针对iOS...
  • u012198553
  • u012198553
  • 2015-03-14 16:05
  • 378
    个人资料
    • 访问:102878次
    • 积分:3131
    • 等级:
    • 排名:第12784名
    • 原创:207篇
    • 转载:77篇
    • 译文:0篇
    • 评论:27条