拖动UIButton而不转移到中心[Swift 3]

时间:2017-07-29 17:30:30

标签: ios swift3 drag uipangesturerecognizer

所以我发现了如何使用UIPanGestureRecognizer使按钮可拖动。但我知道如何做的唯一方法是通过中心存储和拖动按钮。这样做的问题是,如果您尝试从角落拖动按钮,按钮会立即从角落移动到中心。我正在寻找的是一种解决方案,可以在移动时将手指放在选定位置,而不会立即锁定到中心。

我目前正在使用的代码:

func buttonDrag(pan: UIPanGestureRecognizer) {
    print("Being Dragged")
    if pan.state == .began {
        print("panIF")
        buttonCenter = button.center // store old button center
    }else {
        print("panELSE")
        let location = pan.location(in: view) // get pan location
        button.center = location // set button to where finger is
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:3)

这可以通过两种不同的方式完成,一种使用GestureRecognizer 您的问题方式,另一种方式是对UIView进行子类化并实施touchesBegantouchesMovedtouchesEndedtouchesCancelled一般适用于任何UIView子类,可以是UIButtonUILabelUIImageView等...... < / p>

按照您的方式,使用GestureRecognizer我做了一些更改,您仍然需要一个var来保持触摸的原点CGPointUIButton中,这样我们就可以获得触摸位置相对于UIButton和拖动时继续根据原点触摸位置和运动位置调整UIButton原点

方法1 GestureRecognizer

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var button: UIButton!
    var buttonOrigin : CGPoint = CGPoint(x: 0, y: 0)
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let gesture = UIPanGestureRecognizer(target: self, action: #selector(buttonDrag(pan:)))
        self.button.addGestureRecognizer(gesture)
    }

    func buttonDrag(pan: UIPanGestureRecognizer) {
        print("Being Dragged")
        if pan.state == .began {
            print("panIF")
            buttonOrigin = pan.location(in: button)
        }else {
            print("panELSE")
            let location = pan.location(in: view) // get pan location
            button.frame.origin = CGPoint(x: location.x - buttonOrigin.x, y: location.y - buttonOrigin.y)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

方法2 UIView子类,本例中为UIButton子类

使用此UIButton子类

import UIKit

class DraggableButton: UIButton {

    var localTouchPosition : CGPoint?

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        let touch = touches.first
        self.localTouchPosition = touch?.preciseLocation(in: self)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    let touch = touches.first
    guard let location = touch?.location(in: self.superview), let localTouchPosition = self.localTouchPosition else{
        return
    }

    self.frame.origin = CGPoint(x: location.x - localTouchPosition.x, y: location.y - localTouchPosition.y)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        self.localTouchPosition = nil
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesCancelled(touches, with: event)
        self.localTouchPosition = nil
    }

}

<强>结果

enter image description here

enter image description here

希望这有助于你