我如何添加一个触摸事件到一个UIView? 我试一试:

UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, nextY)] autorelease];
[headerView addTarget:self action:@selector(myEvent:) forControlEvents:UIControlEventTouchDown];
// ERROR MESSAGE: UIView may not respond to '-addTarget:action:forControlEvents:'

我不想创建一个子类然后重写

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

当前回答

你们为什么不试试ssevenlistener呢?

您不需要创建任何手势识别器,并将您的逻辑分离到另一个方法。ssevenlistener支持在视图上设置侦听器块,以侦听单个点击手势,双击手势和N-tap手势,如果你喜欢,和长按手势。设置一个点击手势监听器是这样的:

[view ss_addTapViewEventListener:^(UITapGestureRecognizer *recognizer){…} numberOfTapsRequired: 1);

其他回答

在Swift 4.2和Xcode 10中

使用UITapGestureRecognizer for添加触摸事件

//Add tap gesture to your view
let tap = UITapGestureRecognizer(target: self, action: #selector(handleGesture))
yourView.addGestureRecognizer(tap)

// GestureRecognizer
@objc func handleGesture(gesture: UITapGestureRecognizer) -> Void {
//Write your code here
}

如果你想使用SharedClass

//This is my shared class
import UIKit

class SharedClass: NSObject {

    static let sharedInstance = SharedClass()

    //Tap gesture function
    func addTapGesture(view: UIView, target: Any, action: Selector) {
        let tap = UITapGestureRecognizer(target: target, action: action)
        view.addGestureRecognizer(tap)
    }
} 

我的ViewController中有3个视图,分别是view1 view2和view3。

override func viewDidLoad() {
    super.viewDidLoad()
    //Add gestures to your views
    SharedClass.sharedInstance.addTapGesture(view: view1, target: self, action: #selector(handleGesture))
    SharedClass.sharedInstance.addTapGesture(view: view2, target: self, action: #selector(handleGesture))
    SharedClass.sharedInstance.addTapGesture(view: view3, target: self, action: #selector(handleGesture2))

}

// GestureRecognizer
@objc func handleGesture(gesture: UITapGestureRecognizer) -> Void {
    print("printed 1&2...")
}
// GestureRecognizer
@objc func handleGesture2(gesture: UITapGestureRecognizer) -> Void {
    print("printed3...")
}

在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];

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

  //Do stuff here...
}

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

你可以通过在代码中添加手势识别器来实现这一点。

步骤1:ViewController.m:

// Declare the Gesture.
UITapGestureRecognizer *gesRecognizer = [[UITapGestureRecognizer alloc] 
                                          initWithTarget:self 
                                          action:@selector(handleTap:)];
gesRecognizer.delegate = self;

// Add Gesture to your view.
[yourView addGestureRecognizer:gesRecognizer]; 

步骤2:ViewController.m:

// Declare the Gesture Recogniser handler method.
- (void)handleTap:(UITapGestureRecognizer *)gestureRecognizer{
   NSLog(@"Tapped");
}

注意:这里的yourView在我的情况是@property(强,非原子)IBOutlet UIView *localView;

编辑:*localView是Main中的白色框。下面的故事板

你们为什么不试试ssevenlistener呢?

您不需要创建任何手势识别器,并将您的逻辑分离到另一个方法。ssevenlistener支持在视图上设置侦听器块,以侦听单个点击手势,双击手势和N-tap手势,如果你喜欢,和长按手势。设置一个点击手势监听器是这样的:

[view ss_addTapViewEventListener:^(UITapGestureRecognizer *recognizer){…} numberOfTapsRequired: 1);

创建一个手势识别器(子类),它将实现触摸事件,如touchesBegan。之后可以将其添加到视图中。

这样您将使用组合而不是子类化(这是请求)。