2011-11-18 99 views
2

在我的應用程序添加了手勢和這些方法如何獲得longpress中的touchesEnded位置?

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)]; 

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 

    UILongPressGestureRecognizer *longpress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handellongpress:)]; 

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 
      UITouch *start = [[event allTouches] anyObject]; 
      CGPoint StartPoint = [start locationInView:self.view]; 
      NSLog(@"start points x : %f y : %f", StartPoint.x, StartPoint.y); 
    } 
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 
      UITouch *move = [[event allTouches] anyObject]; 
      CGPoint MovePoint = [move locationInView:self.view]; 
      NSLog(@"MovePoint x : %f y : %f", MovePoint.x, MovePoint.y); 
    } 

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 
      UITouch *end = [[event allTouches] anyObject]; 
      CGPoint EndPoint = [end locationInView:self.view]; 
      NSLog(@"end ponts x : %f y : %f", EndPoint.x, EndPoint.y); 
    } 

    -(void)handellongpress:(UITapGestureRecognizer *)recognizer { 
     CGPoint LongTapPoint = [recognizer locationInView:self.view]; 
     NSLog(@"LongTapPoint.x %f,LongTapPoint.y %f",LongTapPoint.x,LongTapPoint.y); 
    } 

- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer { 
     CGPoint SingleTap = [recognizer locationInView:self.view]; 
     NSLog(@"SingleTap.x %f,SingleTap.y %f",SingleTap.x,SingleTap.y); 
} 

- (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer { 
     CGPoint doubleTapPoint = [recognizer locationInView:self.view]; 
    NSLog(@"doubleTapPoint.x %f,doubleTapPoint.y %f",doubleTapPoint.x,doubleTapPoint.y); 
} 

現在我想的是用戶觸摸到了某一個地方,跨越到一個新的地方拖,然後在適當位置握一會兒如何檢測最後的位置。我面臨一個問題,如果用戶開始那裏從按鈕位置輕掃touchMove和touchEnd定位方法dint被調用。 如何在longpress中調用touchEnd方法。

回答

1
if(gestureRecognizer.state == UIGestureRecognizerStateBegan) 
{ 
    NSLog(@"UIGestureRecognizerStateBegan"); 
} 
else if(gestureRecognizer.state == UIGestureRecognizerStateEnded) 
{ 
    NSLog(@"UIGestureRecognizerStateEnded"); 
} 
else if(gestureRecognizer.state == UIGestureRecognizerStateChanged) 
{ 
    NSLog(@"UIGestureRecognizerStateChanged"); 
} 
else if(gestureRecognizer.state == UIGestureRecognizerStateCancelled)  
{ 
    NSLog(@"UIGestureRecognizerStateCancelled"); 
} 
else if(gestureRecognizer.state ==UIGestureRecognizerStateFailed) 
{ 
    NSLog(@"UIGestureRecognizerStateFailed"); 
} 

使用gestureRecogizer.state