-1

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

4

2 回答 2

2

你正在寻找这个:

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...
}
于 2013-07-23T07:09:13.810 回答
1

在 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上的一堆示例代码可能会有所帮助。

于 2013-07-23T07:09:52.463 回答