一、基本概念与理解:
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];
}
复制代码
其中方法locationInView:即获取当前Touch点位置。若是获取触摸动作之前的位置则用prevIoUsLocationInView:方法。以上定义了一个TickleGestureRecognizer新类实现了Tickle手势。具体使用则是:
- (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.....
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。