UIView touch event in controller

前端 未结 11 1773
梦毁少年i
梦毁少年i 2020-12-02 06:26

How can I add UIView touchbegin action or touchend action programmatically as Xcode is not providing from Main.storyboard?

相关标签:
11条回答
  • 2020-12-02 06:46

    you can used this way: create extension

    extension UIView {
    
        func addTapGesture(action : @escaping ()->Void ){
            let tap = MyTapGestureRecognizer(target: self , action: #selector(self.handleTap(_:)))
            tap.action = action
            tap.numberOfTapsRequired = 1
    
            self.addGestureRecognizer(tap)
            self.isUserInteractionEnabled = true
    
        }
        @objc func handleTap(_ sender: MyTapGestureRecognizer) {
            sender.action!()
        }
    }
    
    class MyTapGestureRecognizer: UITapGestureRecognizer {
        var action : (()->Void)? = nil
    }
    

    and use this way:

    @IBOutlet weak var testView: UIView!
    testView.addTapGesture{
       // ...
    }
    
    0 讨论(0)
  • 2020-12-02 06:47

    Just an update to above answers :

    If you want to have see changes in the click event, i.e. Color of your UIVIew shud change whenever user clicks the UIView then make changes as below...

    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
    

    Also, call this Class from Storyboard & ViewController as:

    @IBOutlet weak var panVerificationUIView:ClickableUIView!
    
    0 讨论(0)
  • 2020-12-02 06:49

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:50

    Updating @stevo.mit's answer for Swift 4.x:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self.view)
            // 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.view)
            // 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.view)
            // do something with your currentPoint
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:52

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-02 06:53

    Create outlets from views that were created in StoryBoard.

    @IBOutlet weak var redView: UIView!
    @IBOutlet weak var orangeView: UIView!
    @IBOutlet weak var greenView: UIView!   
    

    Override the touchesBegan method. There are 2 options, everyone can determine which one is better for him.

    1. Detect touch in special view.

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
           if let touch = touches.first {
              if touch.view == self.redView {
                  tapOnredViewTapped()
              } else if touch.view == self.orangeView {
                  orangeViewTapped()
              } else if touch.view == self.greenView {
                  greenViewTapped()
              } else {
                  return
              }
          }
      
      }
      
    2. Detect touch point on special view.

      override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
          if let touch = touches.first {
              let location = touch.location(in: view)
              if redView.frame.contains(location) {
                  redViewTapped()
              } else if orangeView.frame.contains(location) {
                  orangeViewTapped()
              } else if greenView.frame.contains(location) {
                  greenViewTapped()
              }
          }
      
      }
      

    Lastly, you need to declare the functions that will be called, depending on which view the user clicked.

    func redViewTapped() {
        print("redViewTapped")
    }
    
    func orangeViewTapped() {
        print("orangeViewTapped")
    }
    
    func greenViewTapped() {
        print("greenViewTapped")
    }
    
    0 讨论(0)
提交回复
热议问题