Swift,UIView控制器中的触摸事件

我怎样才能添加UIView触摸操作或touchend动作编程的Xcode不是从Main.storyboard提供?

你将不得不通过代码添加它。 尝试这个:

  // 1.create UIView programmetically var myView = UIView(frame: CGRectMake(100, 100, 100, 100)) // 2.add myView to UIView hierarchy self.view.addSubview(myView) // 3. add action to myView let gesture = UITapGestureRecognizer(target: self, action: "someAction:") // or for swift 2 + let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:))) self.myView.addGestureRecognizer(gesture) func someAction(sender:UITapGestureRecognizer){ // do other task } // or for Swift 3 func someAction(_ sender:UITapGestureRecognizer){ // do other task } 

更新@ Chackle的Swift 2.x的答案:

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(self) // do something with your currentPoint } } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(self) // do something with your currentPoint } } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(self) // do something with your currentPoint } } 

更新@ Crashalot对Swift 3.x的回答:

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.location(in: self) // do something with your currentPoint } } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.location(in: self) // do something with your currentPoint } } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.location(in: self) // do something with your currentPoint } } 

Swift 3.1的更新:

 let gesture = UITapGestureRecognizer(target: self, action: #selector (self.checkAction(sender:))) self.myView.addGestureRecognizer(gesture) func checkAction(sender : UITapGestureRecognizer) { // Do what you want } 

把它放在你的UIView子类中(如果你为这个function创build一个sublcass最简单)。

 class YourView: UIView { //Define your initialisers here override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { if let touch = touches.first as? UITouch { let currentPoint = touch.locationInView(self) // do something with your currentPoint } } override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { if let touch = touches.first as? UITouch { let currentPoint = touch.locationInView(self) // do something with your currentPoint } } override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { if let touch = touches.first as? UITouch { let currentPoint = touch.locationInView(self) // do something with your currentPoint } } } 

只是更新上面的答案:

如果你想看到点击事件的变化,即你的UIVIew shud的颜色,每当用户点击UIView,然后改变如下…

 class ClickableUIView: UIView { override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(self) // do something with your currentPoint } self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked. } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(self) // do something with your currentPoint } self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked. } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first { let currentPoint = touch.locationInView(self) // do something with your currentPoint } self.backgroundColor = UIColor.whiteColor()//Color when UIView is not clicked. }//class closes here 

另外,从Storyboard&ViewController调用这个类为:

 @IBOutlet weak var panVerificationUIView:ClickableUIView!