IOS,Xcode:捕获UITouch事件结束

时间:2013-03-10 02:11:38

标签: ios xcode view uitouch

我是IOS编程的初学者......我现在对这些方法并不熟悉。 设置是:    我有一个叫做的函数。在这个函数中,我想等待一次点击,然后生成一个新的ViewController。我只需要点击CGPoint,然后继续下一步。但我不知道是否有一些方法可以捕获touchesEnded。或者我想错误的方式。有人能提供给我一些吗?

如果我在触摸结束后立即创建新的viewController,那么在modelDetect(它是必需的)之后什么都不会发生。应用程序将在触摸之前结束。

所以我现在不知道。

非常感谢。

- (void)modelDetect
{
 //wait for touches....then
 [self addNewViewController]; 
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
{
   NSLog(@"touch happens");
   UITouch *touch = [touches anyObject];
   Location = [touch locationInView:self.view];      
}

1 个答案:

答案 0 :(得分:2)

您可以尝试使用UITapGestureRecognizer它可以解决您的问题

UITapGestureRecognizer *rec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTapped:)];
[self addGestureRecognizer:rec];


- (void)userTapped:(UITapGestureRecognizer *)recognizer
{
    if(recognizer.state == UIGestureRecognizerStateRecognized)
    {
        CGPoint point = [recognizer locationInView:recognizer.view]; 
        // point.x and point.y  touch coordinates
         NSLog("%lf %lf", point.x, point.y);

        // here you could call your method or add the new view controller which you want
         [self addNewViewController]; 
    }
}

要获得点touchesEnded,您应该使用CGPoint

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *aTouch = [touches anyObject];
    CGPoint point = [aTouch locationInView:self];
    // point.x and point.y touch coordinates
    NSLog("%lf %lf", point.x, point.y);
}