UIView touch event in controller
How can I add UIView touchbegin action or touchend action programmatically as Xcode is not providing from Main.storyboard?
You will have to add it through code. First, create the view and add it to the hierarchy:
var myView = UIView(frame: CGRectMake(100, 100, 100, 100))
self.view.addSubview(myView)
After that initialize gesture recognizer. Until Swift 2:
let gesture = UITapGestureRecognizer(target: self, action: "someAction:")
After Swift 2:
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:)))
Then bind it to the view:
self.myView.addGestureRecognizer(gesture)
Swift 3:
func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
Swift 4 just add @objc
before func
:
@objc func someAction(_ sender:UITapGestureRecognizer){
// do other task
}
Swift UI:
Text("Tap me!").tapAction {
print("Tapped!")
}
Swift 4 / 5:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction))
self.myView.addGestureRecognizer(gesture)
@objc func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
Swift 3:
let gesture = UITapGestureRecognizer(target: self, action: #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)
func checkAction(sender : UITapGestureRecognizer) {
// Do what you want
}
Updating @Crashalot's answer for 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
}
}
Updating @Chackle's answer for 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
}
}
Put this in your UIView
subclass (it's easiest if you make a sublcass for this functionality).
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
}
}
}