拖动 UIButton 而不将其移至中心 [Swift 3] [英] Drag UIButton without it shifting to center [Swift 3]

查看:20
本文介绍了拖动 UIButton 而不将其移至中心 [Swift 3]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I found out how to make a button draggable using the UIPanGestureRecognizer. But the only way I know how to do it is by storing and dragging the button by the center. The problem with this is if you try and drag the button from a corner, the button instantly shifts from the corner to the center. What I'm looking for is a solution that would keep my finger on a selected place while moving without instantly locking onto the center.

我目前使用的代码:

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 子类可以是 UIButtonUILabelUIImageView 等...

This can be done at least in two different ways, one using GestureRecognizer your question way and other way is subclassing the UIView and implementing the touchesBegan, touchesMoved , touchesEnded, touchesCancelled in general will work for any UIView subclass can be UIButton or UILabel or UIImageView etc...

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

In your way, using GestureRecognizer I make a few changes you still require a var to keep the origin CGPoint of the touch in your UIButton so we get the touch position relative to the UIButton and when the drag continue adjust the UIButton origin according to the origin touch position and the positions of the movement

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
    }

}

结果

希望对你有帮助

这篇关于拖动 UIButton 而不将其移至中心 [Swift 3]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆