可以在Swift中拖拽UIButton / Elements? [英] Draggable UIButton/Elements in Swift?

查看:111
本文介绍了可以在Swift中拖拽UIButton / Elements?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题!我只是想知道,在Swift中(特别是Swift 2,虽然这可能不言而喻!),如何创建一个用户可以拖动的按钮。因此,例如,如果它是UIButton,则用户可以点击并按住它,当他们移动他们的手指时,UIButton随之移动,当他们释放它时,它保持在用户离开它的位置。可能会有一个捕捉系统,但现在这并不重要。



我搜索过StackOverflow并发现了一些非常有趣的东西,但是这些都是针对Objective-C的,虽然Swift在某些方面非常相似,但我无法弄清楚如何在Swift中实现这一点。



对于一个项目来说,它会非常受欢迎我正在努力!



非常感谢!

解决方案

您可以在 UIButton 上实施 UIPanGestureRecognizer



无论您在哪里创建按钮( viewDidLoad ,如果使用网点):

  let pan = UIPanGestureRecognizer(target:self,action:panButton:)
button.addGestureRecognizer(pan)

这将创建一个新的平移手势识别器并将其添加到按钮。现在,您将要实现pan的操作。首先,您需要存储按钮的中心,以便在完成平移后重置它。将其添加为视图控制器属性:

  var buttonCenter = CGPointZero 

然后执行平移动作。请注意,您可以使用手势识别器状态来确定平移开始和结束的时间:

  func panButton(pan:UIPanGestureRecognizer){

如果pan.state == .Began {
buttonCenter = button.center //存储旧按钮中心
}否则如果pan.state == .Ended || pan.state == .Failed || pan.state == .Cancelled {
button.center = buttonCenter //恢复按钮中心
} else {
let location = pan.locationInView(view)//获取平移位置
button.center = location //将按钮设置为手指为
}

}


this is my first question! I was just wondering, in Swift (specifically Swift 2, although that may go without saying!), how you create a button that the user can drag around. So for example, if it is a UIButton, the user can tap and hold it, and when they move their finger, the UIButton moves with it, and when they release it, it remains in the position that the user left it. Potentially there could be a snapping system but this is unimportant for now.

I've searched StackOverflow and found some quite interesting things, however it's all for Objective-C, and although Swift is pretty similar in some respects, I can't figure out in the slightest as to how to implement this in Swift.

It would be massively appreciated for a project that I am working on!

Thank you very much!

解决方案

You can implement UIPanGestureRecognizer on your UIButton.

Wherever you create your button (viewDidLoad if using outlets):

let pan = UIPanGestureRecognizer(target: self, action: "panButton:")
button.addGestureRecognizer(pan)

This creates a new pan gesture recognizer and adds it to the button. Now, you'll want to implement the pan's action. First, you need to store the center of the button to be able to reset it when you finish panning. Add this as a view controller property:

var buttonCenter = CGPointZero

Then you implement the pan action. Note that you can use gesture recognizer states to determine when the pan starts and ends:

func panButton(pan: UIPanGestureRecognizer) {

    if pan.state == .Began {
        buttonCenter = button.center // store old button center
    } else if pan.state == .Ended || pan.state == .Failed || pan.state == .Cancelled {
        button.center = buttonCenter // restore button center
    } else {
        let location = pan.locationInView(view) // get pan location
        button.center = location // set button to where finger is
    }

}

这篇关于可以在Swift中拖拽UIButton / Elements?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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