iOS开发之手势识别汇总

转载 2016年05月31日 10:11:36

本文出处刚刚在线: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;//取消形变
        }];
    }
}

相关文章推荐

iOS开发之手势识别汇总

iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer), 轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGes...

转载大神IOS开发系列【9】--触摸事件、手势识别、摇晃事件、耳机线控

转载自:http://www.cnblogs.com/kenshincui/p/3950646.html 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们...

iOS开发 - 手势识别

UIGestureRecognizer 为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer 利用UIGestureRecognizer,能轻松识别用户在某个view上...

iOS开发 手势识别UIGestureRecognizer

一、概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式:  - (void)touchesBegan:(NSSet *)touches wi...

iOS开发之手势识别

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

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

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

iOS开发 - 手势识别(UIGestureRecognizer)

UIGestureRecognizer为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一...

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

-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计。今天我们就针对iOS...

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

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

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

手势识别手势识别将低级别事件处理代码转换成更高一级的行动。它们是对连接至一个视图,它允许,以便向操作进行响应的控制的方式做对象。手势识别解释触摸以确定它们是否对应于特定的手势,诸如滑动,挤压或转动。如...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS开发之手势识别汇总
举报原因:
原因补充:

(最多只允许输入30个字)