代码之家  ›  专栏  ›  技术社区  ›  user2152814

IOS,Xcode:捕获UITouch事件结束

  •  1
  • user2152814  · 技术社区  · 13 年前

    我是IOS编程的初学者。。。我现在还不熟悉这些方法。 设置为: 我有一个名为的函数。在这个函数中,我想等待一次点击,然后生成一个新的ViewController。我只需要点击CGPoint,然后继续下一步。但我不知道是否有什么方法可以捕捉触摸结束。或者也许我想错了。有人能给我一些吗?

    如果我在触摸结束后创建了新的viewController,那么在模型检测之后什么都不会发生(这是必需的)。应用程序将在触摸之前结束。

    所以我现在不知道。

    真的非常感谢。

    - (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 回复  |  直到 13 年前
        1
  •  2
  •   nsgulliver    13 年前

    你可以尝试使用 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);
    }
    
    推荐文章