I have a requirement to move a label around only when the user touches on it and drags. I cant find out whether the user touched that label or not. I have used the touchesMoved method(Swift 2). Below is my code
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches as Set<UITouch>, withEvent: event)
let touch = touches.first
if (touch!.view) == (moveLabel as UIView) // moveButton is my label
{
location = touch!.locationInView(self.view)
moveLabel.center = location
}
}
When I do this, my label is not moving :( someone help me please
I had been thinking in your question, and this is my result, you need to subclass
UILabel
and in theinit
you need to setuserInteractionEnabled = true
and then override this methodoverride func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
well, my code is this:Swift 3.1 Code
Swift 2.2 Code
Hope this helps you, for me works great, this is how it works