9.6 触摸和手势:手势识别器的使用

艾才良
2023-12-01

==========无限互联IOS视频学习笔记=====UI高级=====

#参考资源:

IOS开发之手势——UIGestureRecognizer 共存


2、手势识别器
·UIGestureRecognizer

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

在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:

·UITapGestureRecognizer(轻击) 

·UIPinchGestureRecognizer(捏合) 

·UIPanGestureRecognizer(平移----慢速移动) 

·UISwipeGestureRecognizer(轻扫---快速移动) 

·UIRotationGestureRecognizer(旋转) 

·UILongPressGestureRecognizer(长按)

从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。

通过用 touchesBegan / touchesMoved / touchesEnded 实现手势,要监听视图view的手势,必须为这个view创建一个子类,然后才可以监听手势;

手势有很多种状态,定义在UIGestureRecognizer中的UIGestureRecognizerState;

//
//  RootViewController.m
//  my_TouchGestureDemo
//
//  Created by fenghuo on 14-7-19.
//  Copyright (c) 2014年 fenghuo. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    //----------------------点击手势---单击----------------------
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer:tap];
    
    
    //----------------------点击手势---双击----------------------
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
    doubleTap.numberOfTapsRequired = 2;     //指定点击次数
    [self.view addGestureRecognizer:doubleTap];
    
    // 如果双击确定侦测失败,才会触发单击
    // 区别单击、双击手势
    [tap requireGestureRecognizerToFail:doubleTap];
    
    
    //----------------------轻扫手势--快速移动-------------------
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // 默认向右
    swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeGesture];
    
    
    //----------------------滑动手势---慢速移动-------------------
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    
    [self.view addGestureRecognizer:panGesture];
    
    
    //----------------------长按手势-------------------------
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(LongPressAction:)];
    longPress.minimumPressDuration = 2; // 最小长按时间
    
    [self.view addGestureRecognizer:longPress];
    
    
    //----------------------旋转手势-------------------------
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [self.view addGestureRecognizer:rotation];
    
    
    //----------------------捏合手势-------------------------
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [self.view addGestureRecognizer:pinch];
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//----------手势绑定的触发方法-------------------
- (void)tap:(UITapGestureRecognizer *)tapGesture
{
    NSLog(@"单击");
}


- (void)doubleTap:(UITapGestureRecognizer *)tapGetrue
{
    NSLog(@"双击");
}


- (void)swipeAction:(UISwipeGestureRecognizer *)swipeGesture
{
    NSLog(@"轻扫");
}

- (void)panAction:(UIPanGestureRecognizer *)panAction
{
//    NSLog(@"平移");
    CGPoint p = [panAction locationInView:self.view];
    
    NSLog(@"%@", NSStringFromCGPoint(p));
}

- (void)LongPressAction:(UILongPressGestureRecognizer *)longPress
{
    // 当手势结束,就不在打印;
    if (longPress.state == UIGestureRecognizerStateEnded) {
        return;
    }
    
    // 会打印两次;超过两秒手指按下会打印一次,手指放开也会打印一次;
    // 手势有很多种状态,定义在UIGestureRecognizer中的UIGestureRecognizerState,
    // 可以通过判断手势状态,避免两次输出;
    
    NSLog(@"长按");
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
    // 角度,旋转弧度转换为角度
    float degree = rotation.rotation * (180 / M_PI);
    
    NSLog(@"%f", degree);
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
    if (pinch.scale > 1) {
        NSLog(@"放大");
    } else {
        NSLog(@"缩小");
    }
    
    NSLog(@"%f", pinch.scale);
}


@end


 类似资料: