【问题标题】:how to call any method when user tocuhes any part of the screen in ipad [closed]当用户触摸ipad屏幕的任何部分时如何调用任何方法[关闭]
【发布时间】:2013-07-23 07:04:05
【问题描述】:

我有一个 ipad 应用程序,如果用户触摸屏幕的任何部分,我希望它应该显示警报。我研究了触摸开始点和结束点之类的方法,但是如果用户触摸屏幕,如何在触摸上调用方法。

【问题讨论】:

标签: ios ipad methods touch


【解决方案1】:

你正在寻找这个:

UITapGestureRecognizer *singleFingerTap = 
  [[UITapGestureRecognizer alloc] initWithTarget:self 
                                          action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleFingerTap];

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
  CGPoint location = [recognizer locationInView:[recognizer.view superview]];

  //Do stuff here...
}

【讨论】:

    【解决方案2】:

    在 iOS 3.2 及更高版本中,您可以使用手势识别器。例如,这是您处理点击事件的方式:

    //The setup code (in viewDidLoad in your view controller)
    UITapGestureRecognizer *singleFingerTap = 
      [[UITapGestureRecognizer alloc] initWithTarget:self 
                                              action:@selector(handleSingleTap:)];
    [self.view addGestureRecognizer:singleFingerTap];
    [singleFingerTap release];
    
    //The event handling method
    - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
      CGPoint location = [recognizer locationInView:[recognizer.view superview]];
    
      //Do stuff here...
    }
    

    还有很多内置的手势。查看有关 iOS 事件处理和 UIGestureRecognizer 的文档。 github 上的一堆示例代码可能会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      相关资源
      最近更新 更多