我要求只有当用户触摸并拖动标签时才移动标签。我无法确定用户是否触摸了标签。我使用了touchesMoved方法(Swift 2)。下面是我的代码
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
}
}
当我这样做的时候,我的标签没有移动:(有人帮我吗
1条答案
按热度按时间6fe3ivhb1#
我一直在思考你的问题,这是我的结果,你需要子类化
UILabel
,在init
中你需要设置userInteractionEnabled = true
,然后覆盖override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
这个方法,我的代码是这样的:Swift 3.1代码
Swift 2.2代码
希望这对你有帮助,对我来说效果很好,这就是它的工作原理