1

我是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];      
}
4

1 回答 1

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);
}
于 2013-03-10T02:23:33.537 回答