如何获得longpress中的touchesEnded位置?

时间:2011-11-18 06:29:53

标签: objective-c cocoa-touch xcode4 ios4 touch

在我的应用程序中,我添加了手势和这些方法

    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 个答案:

答案 0 :(得分: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