TextField可在Swift iOS中拖拽 [英] TextField Draggable in Swift iOS
本文介绍了TextField可在Swift iOS中拖拽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的视图中拖动textField。但我的代码问题很小。
I want to drag textField around my view. but im having small problem with my code.
这里我的代码
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch : UITouch! = touches.first as! UITouch
location = touch.locationInView(self.view)
bottomTextField.center = location
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
var touch : UITouch! = touches.first as! UITouch
location = touch.locationInView(self.view)
bottomTextField.center = location
}
问题是如果我试图触摸TextField并拖动它然后它不起作用。但如果我只是触摸其他地方然后拖动我的手指然后TextField开始拖动。我只想触摸那个textFiel就可以了。
Problem is if i tried to touch on the TextField and drag then it doesn't work. but if i just touch somewhere else and drag my finger then TextField start to drag. i want t make it only if i touch on that textFiel.
推荐答案
你可以通过在文本字段中添加一个手势来实现这一点。 :
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
}
这篇关于TextField可在Swift iOS中拖拽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文