UIView touch event in controller

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

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

    override func touchesBegan(_ touches: Set, 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, 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, with event: UIEvent?) {
        if let touch = touches.first {
            let currentPoint = touch.location(in: self.view)
            // do something with your currentPoint
        }
    }
    

提交回复
热议问题