swift2 可拖动标签- ios

eqzww0vc  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(308)

我要求只有当用户触摸并拖动标签时才移动标签。我无法确定用户是否触摸了标签。我使用了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
  }
}

当我这样做的时候,我的标签没有移动:(有人帮我吗

6fe3ivhb

6fe3ivhb1#

我一直在思考你的问题,这是我的结果,你需要子类化UILabel,在init中你需要设置userInteractionEnabled = true,然后覆盖override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)这个方法,我的代码是这样的:

Swift 3.1代码

import UIKit

class draggableLabel: UILabel {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.red.cgColor
        self.isUserInteractionEnabled = true
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first;
        let location = touch?.location(in: self.superview);
        if(location != nil)
        {
        self.frame.origin = CGPoint(x: location!.x-self.frame.size.width/2, y: location!.y-self.frame.size.height/2);
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

    }

}

Swift 2.2代码

import UIKit

class draggableLabel: UILabel {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderWidth = 1
        self.layer.borderColor = UIColor.redColor().CGColor
        self.userInteractionEnabled = true
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first;
        let location = touch?.locationInView(self.superview);
        if(location != nil)
        {
        self.frame.origin = CGPointMake(location!.x-self.frame.size.width/2, location!.y-self.frame.size.height/2);
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    }

}

希望这对你有帮助,对我来说效果很好,这就是它的工作原理

相关问题