2012-08-12 79 views
2

我用UISwipeGestureRecognizer和我重寫過程既觸摸事件和手勢識別

-(void)touchesBegan...,-(void)touchesEnded...,-(void)touchesMoved... methods. 

看來的touchesBegan和touchesMoved保持跟蹤直到觸摸滑動手勢識別,touchesEnded不叫(同touchesCancelled)。但我需要滑動手勢識別器和touchesEnded來完成這項工作,我該怎麼做?

+0

你能告訴一些代碼,使我們知道我們正在講的? – sergio 2012-08-12 10:17:09

回答

10

首先,將滑動手勢識別器從庫中拖放到視圖中。

ss

你檢查過在查看取消的項目

ss

寫代碼來響應滑動手勢。

- (IBAction)swipe:(id)sender { 
    v.backgroundColor = [UIColor blueColor]; 
} 

然後,寫入觸摸委託方法。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch* touch = [touches anyObject];  
    CGPoint pt = [touch locationInView:self]; 
    layer.frame = CGRectMake(pt.x, pt.y, 100, 100); 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch* touch = [touches anyObject];  
    CGPoint pt = [touch locationInView:self]; 
    layer.frame = CGRectMake(pt.x, pt.y, 100, 100); 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    layer.frame = CGRectMake(0, 0, 100, 100); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    self.backgroundColor = [UIColor redColor]; 
} 

現在你可以將圖像不取消,你可以滑動屏幕來設置顏色爲藍色(滑動手勢識別成功)。你可以。而且,當觸摸結束時,窗口顏色變爲紅色。

ss

您可以下載這個示例項目,只是運行它:

https://github.com/weed/p120812_TouchAndGesture

+0

謝謝,這就是我需要的!在我的情況下,我還必須解決「延遲結束」問題。 – Andrea 2014-03-21 12:29:44