手势UIGestureRecognizer

UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:

1、拍击UITapGestureRecognizer (任意次数的拍击)  
    2、向里或向外捏UIPinchGestureRecognizer (用于缩放)  
    3、摇动或者拖拽UIPanGestureRecognizer (拖动)  
    4、擦碰UISwipeGestureRecognizer (以任意方向)  
    5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)  
    6、长按UILongPressGestureRecognizer (长按)

对于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一个消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。

下面列举几个具体示例代码

1,一个手指,拍击两次手势

// 创建一个手势识别器
UITapGestureRecognizer *oneFingerTwoTaps =
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];

// Set required taps and number of touches
[oneFingerTwoTaps setNumberOfTapsRequired:2];
[oneFingerTwoTaps setNumberOfTouchesRequired:1];

// Add the gesture to the view
[[self view] addGestureRecognizer:oneFingerTwoTaps];

消息方法oneFingerTwoTaps
- (void)oneFingerTwoTaps
{
  NSLog(@"Action: One finger, two taps");
}

2,两个手指,拍击两次手势

UITapGestureRecognizer *twoFingersTwoTaps =
  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];
[twoFingersTwoTaps setNumberOfTapsRequired:2];
[twoFingersTwoTaps setNumberOfTouchesRequired:2];
[[self view] addGestureRecognizer:twoFingersTwoTaps];

消息方法twoFingersTwoTaps
- (void)twoFingersTwoTaps {
  NSLog(@"Action: Two fingers, two taps");
} 

3,一个手指向上、向下擦碰手势

// 向上擦碰
UISwipeGestureRecognizer *oneFingerSwipeUp =
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];
[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];
[[self view] addGestureRecognizer:oneFingerSwipeUp];

- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer
{
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);
}

// 向下擦碰
UISwipeGestureRecognizer *oneFingerSwipeDown =
  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];
[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];
[[self view] addGestureRecognizer:oneFingerSwipeDown];

- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer
{
  CGPoint point = [recognizer locationInView:[self view]];
  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);
}

4,旋转手势

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

- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer
{
  // Convert the radian value to show the degree of rotation
  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));
}

5,向里或向外捏的手势

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

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
  NSLog(@"Pinch scale: %f", recognizer.scale);
}

手势UIGestureRecognizer,布布扣,bubuko.com

时间: 2024-10-19 02:05:08

手势UIGestureRecognizer的相关文章

iOS手势UIGestureRecognizer

UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有6个子类处理具体的手势: 1.UITapGestureRecognizer (任意手指任意次数的点击) // 点击次数 numberOfTapsRequired // 手指个数 numberOfTouchesRequired [plain] view plaincopy UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer a

iOS手势UIGestureRecognizer识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) (转)

1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类.手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别. UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecog

IOS开发之手势—UIGestureRecognizer 共存

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

iOS 手势UIGestureRecognizer

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

ios的手势UIGestureRecognizer

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

IOS开发之手势——UIGestureRecognizer 共存

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

IOS中手势UIGestureRecognizer

通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRotationGestureRecognizer IOS中手势有很多: UIRotationGestureRecognizer旋转 UITapGestureRecognizer手指点击 UIPinchGestureRecognizer缩放 UISwipeGestureRecognizer手指快速扫过 UI

IOS开发—6种常用手势UIGestureRecognizer介绍

IOS 6种常用手势介绍 // // ViewController.m //  手势 // //  Created by Lotheve on 15/6/13. //  Copyright (c) 2015年Lotheve. All rights reserved. // #import "ViewController.h" @interface ViewController () { UITapGestureRecognizer *_tap;   //点击 UIPanGestureR

UIGestureRecognizer手势

如何使用iOS手势UIGestureRecognizer UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势.UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势: 1.拍击UITapGestureRecognizer (任意次数的拍击)      2.向里或向外捏UIPinchGestureRecognizer (用于缩放)      3.摇动或者拖拽UIPanGestureRecognizer