TextField Draggable in Swift iOS

前端 未结 2 738
梦如初夏
梦如初夏 2021-01-21 05:32

I want to drag textField around my view. but im having small problem with my code.

here my code

   override func touchesBegan(touches: Set

        
2条回答
  •  爱一瞬间的悲伤
    2021-01-21 05:49

    If it helps anyone, following is an updated working version of the accepted answer for swift 4.0 and 5.0

    override func viewDidLoad() {
        super.viewDidLoad()
    
        //Draggable textfields
        let gesture = UIPanGestureRecognizer(target: self, action: #selector(userDragged(gesture:)))
    
        bottomTextView.addGestureRecognizer(gesture)
                bottomTextView.isUserInteractionEnabled = true
    
    }
    
    @objc func userDragged(gesture: UIPanGestureRecognizer){
        let loc = gesture.location(in: self.view)
        self.bottomTextView.center = loc
    
    }
    

提交回复
热议问题