拖动 UIButton 而不将其移至中心 [Swift 3]

Ski*_*mik 3 drag ios uipangesturerecognizer swift3

所以我发现了如何使用 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
    }
}
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Rei*_*ian 5

这至少可以通过两种不同的方式完成,一种使用GestureRecognizer 您的问题方式,另一种方式是子类化UIView并实现touchesBegan, touchesMoved, touchesEndedtouchesCancelled通常适用于任何 UIView 子类可以是UIButtonUILabelUIImageView等等......

以您的方式,使用GestureRecognizer我进行了一些更改,您仍然需要一个 var 来保持CGPoint触摸的原点在您UIButton的触摸位置,以便我们获得相对于 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.
    }


}
Run Code Online (Sandbox Code Playgroud)

方法 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
    }

}
Run Code Online (Sandbox Code Playgroud)

结果

在此处输入图片说明

在此处输入图片说明

希望这对你有帮助