2012-04-25 11 views
4

私はいくつかのカスタムUIGestureRecognizersを問題なく作成しました。私は、シングルタップジェスチャーのカスタムバージョンを持ちたいと思って、UIGestureRecognizerをサブクラス化しようと考えました。 1つの問題を除いてすべてすべて問題なく表示されます。私のアクションハンドラでは、[gestureRecognizer locationInView:self]は常にxとyの両方に対してゼロを返します。私がUITapGestureRecognizerに戻ると、アクションハンドラは正常に動作します。これは、サブクラス化されたジェスチャー認識とは何かである必要があり、ここに私のコードです:カスタムUIGestureRecognizerを作成する

#import "gr_TapSingle.h" 

#define tap_Timeout 0.25 

@implementation gr_TapSingle 


- (id)init 
{ 
    self = [super init]; 
    if (self) 
    { 
    } 
    return self; 
} 

- (void)reset 
{ 
} 

-(void)gesture_Fail 
{ 
    self.state = UIGestureRecognizerStateFailed; 
} 

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [super touchesBegan:touches withEvent:event]; 

    if ([self numberOfTouches] > 1) 
    { 
     self.state = UIGestureRecognizerStateFailed; 
     return; 
    } 

    originLocation = [[[event allTouches] anyObject] locationInView:self.view]; 

    [self performSelector:@selector(gesture_Fail) withObject:nil afterDelay:tap_Timeout]; 
} 

-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [super touchesMoved:touches withEvent:event]; 

    if (self.state == UIGestureRecognizerStatePossible) 
    { 
     CGPoint l_Location = [[[event allTouches] anyObject] locationInView:self.view]; 
     CGPoint l_Location_Delta = CGPointMake(l_Location.x - originLocation.x, l_Location.y - originLocation.y); 
     CGFloat l_Distance_Delta = sqrt(l_Location_Delta.x * l_Location_Delta.x + l_Location_Delta.y * l_Location_Delta.y); 
     if (l_Distance_Delta > 15) 
      self.state = UIGestureRecognizerStateFailed; 
     return; 
    } 
} 

-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [super touchesEnded:touches withEvent:event]; 

    if (self.state == UIGestureRecognizerStatePossible) 
     [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(gesture_Fail) object:nil]; 

    if (self.state != UIGestureRecognizerStateFailed) 
     self.state = UIGestureRecognizerStateEnded; 
} 

-(void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    [super touchesCancelled:touches withEvent:event]; 
    if (self.state == UIGestureRecognizerStatePossible) 
     [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(gesture_Fail) object:nil]; 
    self.state = UIGestureRecognizerStateFailed; 
} 

@end 

答えて

2

Appleのドキュメントは言う:

返される値は、ジェスチャーのための一般的なシングルポイントの場所です によって計算UIKitフレームワークこれは、通常、ジェスチャーに含まれる タッチの重心です。 UISwipeGestureRecognizerクラスとUITapGestureRecognizerクラスのオブジェクトの場合、このメソッドによって返される の場所は、 ジェスチャーに特有の意味を持ちます。この重要性は、それらの クラスのリファレンスに記載されています。

だから、私はすべてのサブクラスが、この方法の独自の特別な実装を持っていると仮定します。 UIGestureRecognizerをサブクラス化する場合は、独自に実装する必要があります。

EDIT:

のような何か:

- (CGPoint)locationInView:(UIView *)view 
{ 
    if(view == self.view) 
    { 
     return originLocation; 
    } 
    else 
    { 
    //you decide 
    } 
} 
関連する問題