iOS手势识别器

UIGestureRecognizer

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

UITapGestureRecognizer // 1.单击

UILongPressGestureRecognizer // 3.长按

UISwipeGestureRecognizer // 4.轻扫

UIPanGestureRecognizer // 5.移动

UIRotationGestureRecognizer // 6.旋转

UIPinchGestureRecognizer // 7.捏合

创建手势:
    // 1.单击
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imgView addGestureRecognizer:tap];

    // 2.双击
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
    doubleTap.numberOfTapsRequired = 2;
    [imgView addGestureRecognizer:doubleTap];

    // 双击失败才单击
    [tap requireGestureRecognizerToFail:doubleTap];

    // 3.长按
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
    //设置最短时间
    longPress.minimumPressDuration = 1;
    [imgView addGestureRecognizer:longPress];

    // 4.轻扫
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    // 设置轻扫方向
    [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
    [imgView addGestureRecognizer:swipe];

    // 5.移动
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    [imgView addGestureRecognizer:pan];

    // 轻扫失败才移动
    [pan requireGestureRecognizerToFail:swipe];

    // 6.旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
    [imgView addGestureRecognizer:rotation];

    // 7.捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
    [imgView addGestureRecognizer:pinch];
手势触发事件:

GestureAction:

-(void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        NSLog(@"长按开始");
    }else if (longPress.state == UIGestureRecognizerStateEnded){
        NSLog(@"长按结束");
    }
}

- (void)panAction:(UIPanGestureRecognizer *)pan {

    //手指所在的坐标
    CGPoint point = [pan locationInView:self.view];
    _view.center = point;
}

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{

    if (rotation.state == UIGestureRecognizerStateChanged) {

        //取到弧度
        CGFloat angle = rotation.rotation;

        //正在旋转
        rotation.view.transform = CGAffineTransformMakeRotation(angle);

    } else if (rotation.state == UIGestureRecognizerStateEnded) {

        //还原
        [UIView animateWithDuration:.5 animations:^{

            rotation.view.transform = CGAffineTransformIdentity;
        }];
    }
}

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{

    if (pinch.state == UIGestureRecognizerStateChanged) {

        // 取到缩放比率
        CGFloat scale = pinch.scale;

        // 缩放
        pinch.view.transform = CGAffineTransformMakeScale(scale, scale);

    } else if (pinch.state == UIGestureRecognizerStateEnded) {

        [UIView animateWithDuration:.5 animations:^{

            pinch.view.transform = CGAffineTransformIdentity;
        }];
    }
}
Motion 摇晃手势
//让当前对象成为第一响应者
- (BOOL)canBecomeFirstResponder
{

   return YES;
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

   NSLog(@"摇一摇开始");
}

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

   NSLog(@"摇一摇结束");
}

推荐一篇iOS手势识别的详细使用的文章:iOS手势识别



iOS手势识别器

原文地址:https://www.cnblogs.com/knightguang/p/9605832.html

时间: 2025-01-12 23:20:12

iOS手势识别器的相关文章

iOS 手势识别器概述

手势识别器 iOS 手势识别器(UIGestureRecognizer) 点击手势(UITapGestureRecognizer) 滑动手势(UISwipeGestureRecognizer) 旋转手势(UIRotationGestureRecognizer) 捏合手势( UIPinchGestureRecognizer) 长按手势( UILongPressGestureRecognizer) 平移手势( UIPanGestureRecognizer) 屏幕边缘平移手势(UIScreenEdge

我的IOS学习之路(三):手势识别器

在iOS的学习中,对于手势的处理是极为重要的,如对于图片,我们经常需要进行旋转,缩放以及移动等.这里做一下总结,详见代码. 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 UIImage *image = [UIImage imageNamed:@"018.png"]; 5 UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 6 imageView.

iOS开发UI篇—手势识别器(敲击)

iOS开发UI篇—手势识别器(敲击) 一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听view触摸事件,有很明显的几个缺点 (1)必须得自定义view (2)由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件(需要通过代理) (3)不容易区分用户的具体手势行为 iOS 3.2之后,苹果推出

iOS 开发UI中轻拍,长按,旋转手势识别器方法

一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听view触摸事件,有很明显的几个缺点 (1)必须得自定义view (2)由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件(需要通过代理) (3)不容易区分用户的具体手势行为 iOS 3.2之后,苹果推出了手势识别功能(Gesture Rec

iOS开发UI篇—手势识别器(长按+轻扫)

iOS开发UI篇—手势识别器(长按+轻扫) 一.长按事件 1 // 2 // YYViewController.m 3 // 03-长按 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @prop

IOS开发UI篇—手势识别器(拖拽+旋转+缩放)

IOS开发UI篇—手势识别器(拖拽+旋转+缩放) 一.拖拽 示例代码: 1 // 2 // YYViewController.m 3 // 06-拖拽事件 4 // 5 // Created by apple on 14-6-19. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController ()

iOS常用手势识别器

手势识别状态: typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { // 没有触摸事件发生,所有手势识别的默认状态 UIGestureRecognizerStatePossible, // 一个手势已经开始但尚未改变或者完成时 UIGestureRecognizerStateBegan, // 手势状态改变 UIGestureRecognizerStateChanged, // 手势完成 UIGestureRecognizerStateE

iOS的触摸事件的用法以及和手势识别器的区别

1.首先来介绍下触摸事件和手势识别器的利与弊 触摸事件和手势识别器二者之间有直接的关系 手势识别器是在触摸事件的基础上演变过来的 当我们用到触摸事件时 默认的uiview是没有什么效果的 只能自定义view才能实现事件的触摸 通常用到的方法如下: – touchesBegan:withEvent: – touchesMoved:withEvent: – touchesEnded:withEvent: - touchesCancelled:withEvent: 而手势识别器是在触摸事件的基础上而封

iOS UI05_手势识别器

手势:点击,长按,旋转,捏合,拖拽,清扫,清扫的方向 // // MainViewController.m // UI05_手势识别器 // // Created by dllo on 15/8/4. // Copyright (c) 2015年 zhozhicheng. All rights reserved. // #import "MainViewController.h" @interface MainViewController () @property(nonatomic,