UIView touch event in controller
183,096
Solution 1
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!")
}
Solution 2
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
}
Solution 3
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
}
}
Solution 4
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
}
}
Solution 5
For swift 4
@IBOutlet weak var someView: UIView!
let gesture = UITapGestureRecognizer(target: self, action: #selector (self.someAction (_:)))
self.someView.addGestureRecognizer(gesture)
@objc func someAction(_ sender:UITapGestureRecognizer){
print("view was clicked")
}
Related videos on Youtube
Author by
dhaval shah
Updated on April 05, 2022Comments
-
dhaval shah about 2 years
How can I add UIView touchbegin action or touchend action programmatically as Xcode is not providing from Main.storyboard?
-
Miknash almost 9 yearsThat one is for button, the OP wants to add event for UIView
-
Suragch almost 8 yearsUse a
UILongPressGestureRecognizer
with theminimumPressDuration
set to zero. See this answer. It doesn't require subclassing or overriding anything.
-
-
dhaval shah almost 9 years@Chacle, I have more than 10 Uiview in my page, and i want to add action in some of Sub UIView. So for that what should i change?
-
Chackle almost 9 yearsIt depends what you want to use it for. Do you want to tell which
UIView
you press or do you want to handle some presses in each of theUIView
? -
dhaval shah almost 9 yearsI have did that it is giving me an error when i click on the uiView. My code: let gesture = UITapGestureRecognizer(target: self.uiPendingView, action: "touchPending") self.uiPendingView.addGestureRecognizer(gesture) and method: func touchPending(sender: AnyObject) { println("METHOD>>>>>>>>>>>>>>>>>") }
-
dhaval shah almost 9 yearsI want touchevent for some specifi UIview only.
-
Rizwan Shaikh almost 9 yearsjust add : in "touchPending:" ans in function it look like func touchPending(sender: UITapGestureRecognizer)
-
Miknash almost 9 yearsyou are missing ':' in action.
-
C Williams about 5 yearsHi, How can I distinguish which UIView got clicked when they all have the same someAction function?
-
Miknash about 5 yearsEasiest way would be to use tag property and then in function determine which view is sender
-
Yogesh Patel over 4 yearsVery well great example thanks to showing me!! that we can also do this using touchEvent .. i know only two way button and gesture.. Thanks +1
-
Wimukthi Rajapaksha over 4 yearsFurthermore you can design a custom view xib and then bind it into the touch event. But it depends on your requirement. [Here] (stackoverflow.com/questions/27371194/…)
-
J A S K I E R about 4 yearsFIRST 2 LINES SHOULD BE CALLED FROM VIEWDIDLOAD!
-
Keval Langalia about 3 yearsby the way, this touch will not work in simulator. so if you're working on simulator, you can't test the code.