一、基本概念与理解:
Cocoa Touch将触摸事件发送到正在处理的视图。触摸传达的信息包括:
- 触摸发生的位置 (当前位置、最近的历史位置)
- 触摸的阶段 (按下、移动、弹起)
- 轻击数量 (tapCount 单击/双击)
- 触摸发生时间 (时间戳)
每个UITouch对象都代表一个触摸事件。在iPhone中,触摸交互遵循一种原始的设计模式,即在UIView类中而非UIViewController类中编程实现。就是说触摸交互采用视图级编程方式。
如何触摸的,即所谓的基本手势,包括:
- Swipe:滑过,快速移动
- Pinch Open:二指往外拨动
- Pinch Close:二指往内拨动
- Tap:轻击
- Touch and hold:长按
这些手势的具体实现最原始的(相对最新的iOS版本)就是通过4个视图方法自定义编程来实现。
二、 触摸和视图方法——自定义手势编程
预定义的4个回调方法就是处理屏幕上触摸的开始、移动和结束。
touchesBegan: withEvent:
处理手指的移动——touchesMoved: withEvent:
touchesEnded: withEvent:
touchesCancelled: withEvent:
这4个方法都是一种UIResponder方法,通常在UIView子类中实现。所有视图继承了这些方法的基本版本。示例:自定义一个Tickle(类似划卡)手势。
// TickleGestureRecognizer.h
//
#import <UIKit/UIKit.h>
typedef enum {
DirectionUnknown = 0,
DirectionLeft,
DirectionRight
} Direction;
@interface TickleGestureRecognizer : UIGestureRecognizer
@property (assign) int tickleCount;
@property (assign) CGPoint curTickleStart;
@property (assign) Direction lastDirection;
@end
复制代码
//
// TickleGestureRecognizer.m
//
#import "TickleGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#define REQUIRED_TICKLES 2
#define MOVE_AMT_PER_TICKLE 25
@implementation TickleGestureRecognizer
@synthesize tickleCount;
@synthesize curTickleStart;
@synthesize lastDirection;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
self.curTickleStart = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint ticklePoint = [touch locationInView:self.view];
CGFloat moveAmt = ticklePoint.x - curTickleStart.x;
Direction curDirection;
if (moveAmt < 0) {
curDirection = DirectionLeft;
}else{
curDirection = DirectionRight;
}
if (ABS(moveAmt) < MOVE_AMT_PER_TICKLE) return;
// Make sure we've switched directions
if (self.lastDirection == DirectionUnknown || (self.lastDirection == DirectionLeft && curDirection == DirectionRight) || (self.lastDirection == DirectionRight && curDirection == DirectionRight)) {
self.tickleCount++;
self.curTickleStart = ticklePoint;
self.lastDirection = curDirection;
if (self.state == UIGestureRecognizerStatePossible && self.tickleCount > REQUIRED_TICKLES) {
[self setState:UIGestureRecognizerStateEnded];
}
}
}
- (void)reset {
self.tickleCount = 0;
self.curTickleStart = CGPointZero;
self.lastDirection = DirectionUnknown;
if (self.state == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateFailed];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self reset];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self reset];
}
@end
复制代码
其中方法locationInView:即获取当前Touch点位置。若是获取触摸动作之前的位置则用previousLocationInView:方法。以上定义了一个TickleGestureRecognizer新类实现了Tickle手势。具体使用则是:
// 要用手势的UIView的控制器头文件中添加
- (void)handleTickle:(TickleGestureRecognizer *)recognizer;
// 要用手势的UIView的控制器实现文件
// viewDidLoad添加
TickleGestureRecognizer *recognizer = [[TickleGestureRecognizer alloc] initWithTarget:self action:@selector(handleTickle:)];
recognizer.delegate = self; // 又见强大的委托
[view addGestureRecognizer:recognizer]; // 用addGestureRecognizer方法将手势添加到视图上
// 委托的回调方法
- (void)handleTickle:(TickleGestureRecognizer *)recognizer {
// Code.....
}
复制代码
三、利用UIGestureRecognizer类给视图添加手势
iOS3.0后引入了UIGestueRecognizer类帮助我们处理触摸动作下各种手势的添加实现。要点就是在要添加手势的视图的控制器类必须实现UIGestureRecognizerDelegate委托协议。其实从编码角度看,就是苹果公司将所有的基本手势由UIGestureRecognizer类来定义实现了(与自定义编程中的TickleGestureRecognizer异曲同工)并引入的委托机制。
四、iOS5.0中可以在StoryBoard Editor中可视化添加手势