我有一个ipad应用程序我想要如果用户触摸屏幕的任何部分,那么它应该显示警报。我已经研究了触摸开始和结束点等方法,但是如果用户触摸屏幕,如何在触摸时调用方法。
答案 0 :(得分: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...
}
答案 1 :(得分: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上可能有帮助的大量示例代码。