且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在UIView中添加触摸事件?

更新时间:2023-11-17 13:48:40

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

In iOS 3.2 and higher, you can use gesture recognizers. For example, this is how you would handle a tap event:

//The setup code (in viewDidLoad in your view controller)
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...
}

还有一堆内置的手势。查看有关iOS事件处理的文档和 UIGestureRecognizer 。我也有一堆 github 的示例代码,可能有帮助。

There are a bunch of built in gestures as well. Check out the docs for iOS event handling and UIGestureRecognizer. I also have a bunch of sample code up on github that might help.