Cocoa touch将触摸事件发送到正在处理的视图。触摸传达的信息包括:
触摸发生的位置 (当前位置、最近的历史位置) 触摸的阶段 (按下、移动、d起) 轻击数量 (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.....
} 三、利用UIGestureRecognizer类给视图添加手势
iOS3.0后引入了UIGestueRecognizer类帮助我们处理触摸动作下各种手势的添加实现。要点就是在要添加手势的视图的控制器类必须实现UIGestureRecognizerDelegate委托协议。其实从编码角度看,就是苹果公司将所有的基本手势由UIGestureRecognizer类来定义实现了(与自定义编程中的TickleGestureRecognizer异曲同工)并引入的委托机制。
四、iOS5.0中可以在StoryBoard Editor中可视化添加手势 总结
以上是内存溢出为你收集整理的iphone的手势与触摸编程学习笔记全部内容,希望文章能够帮你解决iphone的手势与触摸编程学习笔记所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)