iphone的手勢與觸摸編程學習筆記

 

1、基本概念與理解:
Cocoa Touch將觸摸事件發送到正在處理的視圖。觸摸傳達的信息包括:html

  • 觸摸發生的位置 (當前位置、最近的歷史位置)
  • 觸摸的階段 (按下、移動、彈起) 
  • 輕擊數量 (tapCount 單擊/雙擊) 
  • 觸摸發生時間 (時間戳)  

每一個UITouch對象都表明一個觸摸事件。在iPhone中,觸摸交互遵循一種原始的設計模式,即在UIView類中而非UIViewController類中編程實現。就是說觸摸交互採用視圖級編程方式。  web

如何觸摸的,即所謂的基本手勢,包括:編程

  • Swipe:滑過,快速移動
  • Pinch Open:二指往外撥動
  • Pinch Close:二指往內撥動
  • Tap:輕擊
  • Touch and hold:長按

這些手勢的具體實現最原始的(相對最新的iOS版本)就是經過4個視圖方法自定義編程來實現。設計模式

2、 觸摸和視圖方法——自定義手勢編程
       預約義的4個回調方法就是處理屏幕上觸摸的開始、移動和結束。編碼

       當用戶開始觸摸屏幕時,在事件的開始階段被調用——touchesBegan: withEvent:url

       處理手指的移動——touchesMoved: withEvent:spa

       當手指離開屏幕時,結束觸摸過程——touchesEnded: withEvent:設計

       必須響應持續觸摸事件的系統中斷時調用——touchesCancelled: withEvent:3d

這4個方法都是一種UIResponder方法,一般在UIView子類中實現。全部視圖繼承了這些方法的基本版本。示例:自定義一個Tickle(相似劃卡)手勢。
code

複製代碼
//  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.....
}
複製代碼

3、利用UIGestureRecognizer類給視圖添加手勢
       iOS3.0後引入了UIGestueRecognizer類幫助咱們處理觸摸動做下各類手勢的添加實現。要點就是在要添加手勢的視圖的控制器類必須實現UIGestureRecognizerDelegate委託協議。其實從編碼角度看,就是蘋果公司將全部的基本手勢由UIGestureRecognizer類來定義實現了(與自定義編程中的TickleGestureRecognizer殊途同歸)並引入的委託機制。

4、iOS5.0中能夠在StoryBoard Editor中可視化添加手勢

 

來源:http://www.cnblogs.com/lovecode/archive/2011/12/08/2281232.html

相關文章
相關標籤/搜索