UIGestureRecognizer(手势识别器)

//
//  ViewController.h
//  UIGestureRecognizer
//
//  Created by Cyric Tsao on 15-5-14.
//  Copyright (c) 2015 Cyric Tsao. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{   
    UIView *_demoView;
    CGAffineTransform _transform;
}

@end



//
//  ViewController.m
//  UIGestureRecognizer
//
//  Created by Cyric Tsao on 15-5-14.
//  Copyright (c) 2015 Cyric Tsao. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (
void )viewDidLoad {
    [
super viewDidLoad ];

   
// 创建一个 demoView 视图
   
_demoView = [[ UIView alloc ] initWithFrame : CGRectMake ( 100 , 100 , 165 , 165 )];
   
_demoView . backgroundColor = [ UIColor purpleColor ];
    [
self . view addSubview : _demoView ];
   

   
//----------UIGestureRecognizer( 手势识别器 )----------
   
// 手势识别器类 , 用于检测用户使用设备时所用的手势 . 它是一个抽象类 , 定义了所有手势的基本行为 .
   
// 以下是 UIGestureRecognizer 子类 , 用于处理具体的用户手势行为

   
   
//----------1.UITapGestureRecognizer( 轻击 , 点击手势 )----------
   
//1. 一只手指单击
   
// 默认就是一个手指点击一次
   
UITapGestureRecognizer *singleTap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (singleTapAction:)];
   
// 设置手指点击次数 ( 单击或者双击 ), 默认是 1
    singleTap.
numberOfTapsRequired = 1 ;
   
// 设置点击的手指个数 ( 一根手指或者两根手指 ), 默认是 1
    singleTap.
numberOfTouchesRequired = 1 ;
   
// _demoView 视图添加一个单击手势
    [
_demoView addGestureRecognizer :singleTap];
   
   
//2. 一只手指双击
   
UITapGestureRecognizer *doubleTap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (doubleTapAction:)];
   
// 设置手指点击次数 ( 单击或者双击 ), 默认是 1
    doubleTap.
numberOfTapsRequired = 2 ;
   
// 设置点击的手指个数 ( 一根手指或者两根手指 ), 默认是 1
    doubleTap.
numberOfTouchesRequired = 1 ;
   
// _demoView 视图添加一个双击手势
    [
_demoView addGestureRecognizer :doubleTap];
   
   
//3. 绑定单击和双击事件 ( 区别单击和双击 )
    [singleTap
requireGestureRecognizerToFail :doubleTap];
    // 这行很关键 , 意思是只有当没有检测到 doubleTap( 双击手势 ) 或者检测 doubleTap( 双击手势 ) 失败 ,singleTap( 单击手势 ) 才有效
   
   
//----------2.UIPanGestureRecognizer( 平移 , 拖拽手势 )----------
   
UIPanGestureRecognizer *pan = [[ UIPanGestureRecognizer alloc ] initWithTarget : self action : @selector (panAction:)];
   
// _demoView 视图添加一个平移手势
    [
_demoView addGestureRecognizer :pan];
   
   
//----------3.UISwipeGestureRecognizer( 轻扫 , 滑动手势 )----------
   
// 同一个手势只能指定一个方向 , 不能同时指定多个方向 , 要指定多个方向 , 必须用多个手势
   
UISwipeGestureRecognizer *swipe = [[ UISwipeGestureRecognizer alloc ] initWithTarget : self action : @selector (swipeAction:)];
   
// 轻扫的方向 ( 设置向左轻扫响应事件 )
    swipe.
direction = UISwipeGestureRecognizerDirectionLeft ;
   
// 给视图添加一个轻扫手势
    [
self . view addGestureRecognizer :swipe];
   
   
   
//----------4.UIPinchGestureRecognizer( 捏合手势 )----------
   
UIPinchGestureRecognizer *pinch = [[ UIPinchGestureRecognizer alloc ] initWithTarget : self action : @selector (pinchAction:)];
   
// 给视图添加一个捏合手势
    [
self . view addGestureRecognizer :pinch];
   
   
   
//----------5.UIRotationGestureRecognizer( 旋转手势 )----------
   
UIRotationGestureRecognizer *rotation = [[ UIRotationGestureRecognizer alloc ] initWithTarget : self action : @selector (rotationAction:)];
   
// 给视图添加一个旋转手势
    [
self . view addGestureRecognizer :rotation];
   
   
   
//----------6.UILongPressGestureRecognizer( 长按手势 )----------
   
UILongPressGestureRecognizer *longPress = [[ UILongPressGestureRecognizer alloc ] initWithTarget : self action : @selector (longPressAction:)];
    longPress.
minimumPressDuration = 2 ;
   
// 给视图添加一个长按手势
    [
self . view addGestureRecognizer :longPress];

   
}

#pragma mark -1.UITapGestureRecognizer 轻击事件
// 单击事件
- (
void )singleTapAction:( UITapGestureRecognizer *)tap
{
   
NSLog ( @" 单击 " );
}
// 双击事件
- (
void )doubleTapAction:( UITapGestureRecognizer *)tap
{
   
NSLog ( @" 双击 " );
}

#pragma mark -2.UIPanGestureRecognizer 平移手势
- ( void )panAction:( UIPanGestureRecognizer *)pan
{
   
//demoView 视图的中心 = 手指对象在视图上点击的位置
   
_demoView . center = [pan locationInView : self . view ];
   
   
// 等价的
   
//CGPoint point = [pan locationInView:self.view];
   
//_demoView.center = point;
   
   
//NSLog(@" 移动了 ");
}

#pragma mark -3.UISwipeGestureRecognizer 轻扫手势
- ( void )swipeAction:( UISwipeGestureRecognizer *)swipe
{
   
NSLog ( @" 向左轻扫了 " );
}

#pragma mark -4.UIPinchGestureRecognizer 捏合手势
- ( void )pinchAction:( UIPinchGestureRecognizer *)pinch
{
   
if (pinch. state == UIGestureRecognizerStateBegan ) {
       
// 如果捏合手势的状态是开始 ...
       
       
_transform = _demoView . transform ;
       
//... 记录开始时 _demoView 视图的 transform 属性
       
    }
else {
       
//transform: 改变 , 变换 , 改变 ... 的形状
       
_demoView . transform = CGAffineTransformScale ( _transform , pinch. scale , pinch. scale );
       
// _demoView transform 随着捏合手势的比例变化而在原来的基础上缩放
      
       
/*
         scale
属性 :
        
可以理解为两手指之间的距离 , 其实是个比例 , 相对距离 , 不是绝对距离
        
以刚开始的两个手指对应的两个 point 的之间的距离为标准 , 此时 scale = 1
        
若两手指之间距离减小 , scale 不断变小 , 当两指重合 , 则变为 0
        
若两手指之间距离变大 , scale 不断增大 , 没有上限 , 看屏幕多大
        */

    }
}

#pragma mark -5.UIRotationGestureRecognizer 旋转手势
- ( void )rotationAction:( UIRotationGestureRecognizer *)rota
{
   
if (rota. state == UIGestureRecognizerStateBegan ) {
       
// 如果旋转手势的状态是开始 ...
       
       
_transform = _demoView . transform ;
       
//... 记录开始时 _demoView 视图的 transform 属性
       
    }
else {
       
       
_demoView . transform = CGAffineTransformRotate ( _transform , rota. rotation );
       
// _demoView transform 随着旋转手势的比例变化而在原来的基础上旋转
       
       
/*
         rotation
属性 :
        
可以理解为两手指之间的旋转的角度 , 其实是个比例 , 相对角度 , 不是绝对角度
        
以刚开始的两个手指对应的两个 point 的之间的那条直线为标准 , 此时 rotation = 1
        
向顺时针旋转 , rotation 为正数且不断变大 , 当旋转 360 度时 ,rotation 大概为 6 左右 , 如果继续顺时针旋转 , 则角度会不断增加 , 两圈为 12 左右 , 此时若逆时针旋转 , 角度则不断变小
        
向逆时针旋转 , rotation 为负数且不断变小 , 当旋转 360 度时 ,rotation 大概为 -6 左右
        */

    }
}

#pragma mark -6.UILongPressGestureRecognizer 长按手势
// 长按手势会执行多次
- (
void )longPressAction:( UILongPressGestureRecognizer *)lPress
{
   
if (lPress. state == UIGestureRecognizerStateBegan ) {
       
NSLog ( @" 长按手势执行了 " );
    }
   
   
/*
     typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {
     UIGestureRecognizerStatePossible,   0//
是初始状态,表明识别过程在执行当中 ( 默认的状态 )
     UIGestureRecognizerStateBegan,      1//
开始
     UIGestureRecognizerStateChanged,    2//
改变
     UIGestureRecognizerStateEnded,      3//
结束
     UIGestureRecognizerStateCancelled,  4//
取消 , 终止
     UIGestureRecognizerStateFailed,     5//
失败
     UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded  //
识别器已识别到的手势 = 结束
     };

    */

   
NSLog ( @"%ld" ,lPress. state );
}

#pragma mark -7. 摇晃手势
// 摇晃开始
- (
void )motionBegan:( UIEventSubtype )motion withEvent:( UIEvent *)event
{
   
NSLog ( @" 摇晃手势开始了 " );
}
// 摇晃结束
- (
void )motionEnded:( UIEventSubtype )motion withEvent:( UIEvent *)event
{
   
NSLog ( @" 摇晃手势结束了 " );
}

#pragma mark -8. 重力加速计 ( 扩展知识  暂无 )

@end
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值