2012-09-27 67 views
1

我想編寫我自己的水龍頭手勢識別器,檢測水龍頭的數量和觸摸的次數(我不想使用iOS輕拍手勢識別器,因爲我想擴展它後來以各種其他方式);iOS的自制水龍頭手勢識別器

我試過如下:使用第一motionBegin數字觸摸爲龍頭的numberOfTouches,遞增numberOfTaps,如果沒有新的水龍頭已經在一段時間被視爲啓動輕擊檢測計時器檢測敲擊手勢

問題是,人們很快意識到,在做雙擊輕擊手勢時,iOS可以通過雙擊或兩個快速單觸事件正確檢測到一個motionBegin。我想一個正確的實現應該嘗試檢測那些發生緊密的快速單觸事件,但我想知道是否有更好的方法來實現手勢識別器。

有人知道iOS輕拍手勢是如何實現的嗎?

+2

同上@ hpiOSCoder評論,如果你想從社區的嚴重幫助。關於這個問題 - 只是UIGestureRecognizer的子類 - 它是爲此設計的。你爲什麼要從頭開始?子類或UITapGestureRecognizer。 –

回答

0
1. Add UIGestureRecognizerDelegate in your .h file. like 
@interface finalScreenViewController : UIViewController <UIGestureRecognizerDelegate> 
{ 
// do your stuff 
} 


2. Create a view in your viewDidLoad method (or any other method) you wanna to add the gesture in your .m file 
ex 

UIView * myView=[[UIView alloc]init]; 
myView.frame=CGRectMake(0,0.self.view.frame.size.width,self.view.frame.size.height); 
[self.view addSubView: myView]; 



UITapGestureRecognizer *letterTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapMethod:)]; 
     letterTapRecognizer.numberOfTapsRequired = 1; 
     [myView addGestureRecognizer:letterTapRecognizer]; 



3. you can get view by 

- (void) tapMethod:(UITapGestureRecognizer*)sender { 
    UIView *view = sender.view; 
    NSLog(@"%d", view.tag);//By tag, you can find out where you had tapped. 
}