TextField Draggable in Swift iOS

前端 未结 2 762
梦如初夏
梦如初夏 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:43

    You can achieve this by adding a gesture to the text field:

    override func viewDidLoad() {
        super.viewDidLoad()
    
    
        var gesture = UIPanGestureRecognizer(target: self, action: Selector("userDragged:"))
        bottomTextField.addGestureRecognizer(gesture)
        bottomTextField.userInteractionEnabled = true
    }
    
    func userDragged(gesture: UIPanGestureRecognizer){
        var loc = gesture.locationInView(self.view)
        self.bottomTextField.center = loc
    
    }
    

提交回复
热议问题