ios - 拖动 UIButton 而不将其移动到中心 [Swift 3]

标签 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
    }
}

提前致谢。

最佳答案

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

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

方法一手势识别器

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

希望对你有帮助

关于ios - 拖动 UIButton 而不将其移动到中心 [Swift 3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45392104/

相关文章:

ios - UICollectionView prepareForSegue 通过手势点击 UIImageView

ios - 从 iOS TableViewCell Xib 中删除分隔符

ios - 使用 Swift 3 的 Alamofire 4.0 出现 "has no member"错误

firefox - 完整日历事件超链接在 Firefox 中自动触发

c++ - 如何在 Win32 应用程序中拖动纯色矩形而不会出现白色闪烁且不会干扰屏幕上的其他对象?

ios - 像 facebooks 菜单幻灯片一样拖动 uiview

ios - 结构体数组 - 可编码

ios - Itunes 连接应用程序拒绝,因为应用程序名称与 App Store 上已有的其他应用程序相似

swift - 在两个接口(interface) Controller 之间传递数据

swift - 快速打印没有换行符