UIView touch event in controller

前端 未结 11 1774
梦毁少年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:55

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

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

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

    Swift 4.2:

    @IBOutlet weak var viewLabel1: UIView!
    @IBOutlet weak var viewLabel2: UIView!
      override func viewDidLoad() {
        super.viewDidLoad()
    
        let myView = UITapGestureRecognizer(target: self, action: #selector(someAction(_:)))
        self.viewLabel1.addGestureRecognizer(myView)
    }
    
     @objc func someAction(_ sender:UITapGestureRecognizer){
       viewLabel2.isHidden = true
     }
    
    0 讨论(0)
  • 2020-12-02 07:13

    You will have to add it through code. Try this:

        // 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
        }
    
        // or for Swift 4
        @objc func someAction(_ sender:UITapGestureRecognizer){     
           // do other task
        }
    
        // update for Swift UI
    
        Text("Tap me!")
            .tapAction {
                 print("Tapped!")
            }
    
    0 讨论(0)
提交回复
热议问题