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")
}
Share:
183,096

Related videos on Youtube

dhaval shah
Author by

dhaval shah

Updated on April 05, 2022

Comments

  • dhaval shah
    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
      Miknash almost 9 years
      That one is for button, the OP wants to add event for UIView
    • Suragch
      Suragch almost 8 years
      Use a UILongPressGestureRecognizer with the minimumPressDuration set to zero. See this answer. It doesn't require subclassing or overriding anything.
  • dhaval shah
    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
    Chackle almost 9 years
    It 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 the UIView?
  • dhaval shah
    dhaval shah almost 9 years
    I 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
    dhaval shah almost 9 years
    I want touchevent for some specifi UIview only.
  • Rizwan Shaikh
    Rizwan Shaikh almost 9 years
    just add : in "touchPending:" ans in function it look like func touchPending(sender: UITapGestureRecognizer)
  • Miknash
    Miknash almost 9 years
    you are missing ':' in action.
  • C Williams
    C Williams about 5 years
    Hi, How can I distinguish which UIView got clicked when they all have the same someAction function?
  • Miknash
    Miknash about 5 years
    Easiest way would be to use tag property and then in function determine which view is sender
  • Yogesh Patel
    Yogesh Patel over 4 years
    Very 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
    Wimukthi Rajapaksha over 4 years
    Furthermore 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
    J A S K I E R about 4 years
    FIRST 2 LINES SHOULD BE CALLED FROM VIEWDIDLOAD!
  • Keval Langalia
    Keval Langalia about 3 years
    by the way, this touch will not work in simulator. so if you're working on simulator, you can't test the code.