Swift 3 个可拖动的 UIButton
Swift 3 Draggable UIButton
抱歉,我仍在学习 Swift 的基础知识。
我试图在拖动按钮时移动它,这听起来很简单。我不知道如何将发件人信息传递给拖动功能,以便我可以将其与正在拖动的按钮相关联。
我创建了多个只有文本的单字按钮,并为每个按钮附加了一个平移手势识别器:
let pan = UIPanGestureRecognizer(target: self, action: #selector(panButton(_:)))
let word = UIButton(type: .system)
word.addGestureRecognizer(pan)
我创建了这个函数来在按钮移动时触发:
func panButton(sender: UIPanGestureRecognizer){
if sender.state == .began {
//wordButtonCenter = button.center // store old button center
} else if sender.state == .ended || sender.state == .failed || sender.state == .cancelled {
//button.center = wordButtonCenter // restore button center
} else {
let location = sender.location(in: view) // get pan location
//button.center = location // set button to where finger is
}
}
我收到以下错误:
Use of unresolved identifier 'panButton'
首先,您的操作需要成为 Swift 3 中的一个选择器。所以它看起来像这样:
let pan = UIPanGestureRecognizer(target: self, action: #selector(panButton(_:))
此外,您不能通过选择器传递按钮的值,因此您需要将 func 更改为:
func panButton(sender: UIPanGestureRecognizer){
...
}
如果您想知道如果不能将按钮作为参数传递应该如何找到该按钮,那么您可以考虑使用标签。
正如@benjamin 在Swift 3 中指出的那样,您需要成为一个选择器。我已将代码更新为以下内容以提取按钮标签:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureHandler(panGesture:)))
panGesture.minimumNumberOfTouches = 1
let word = UIButton(type: .system)
使用以下选择器:
@objc func panGestureHandler(panGesture recognizer: UIPanGestureRecognizer) {
let buttonTag = (recognizer.view?.tag)!
if let button = view.viewWithTag(buttonTag) as? UIButton {
if recognizer.state == .began {
wordButtonCenter = button.center // store old button center
} else if recognizer.state == .ended || recognizer.state == .failed || recognizer.state == .cancelled {
button.center = wordButtonCenter // restore button center
} else {
let location = recognizer.location(in: view) // get pan location
button.center = location // set button to where finger is
}
}
}
抱歉,我仍在学习 Swift 的基础知识。
我试图在拖动按钮时移动它,这听起来很简单。我不知道如何将发件人信息传递给拖动功能,以便我可以将其与正在拖动的按钮相关联。
我创建了多个只有文本的单字按钮,并为每个按钮附加了一个平移手势识别器:
let pan = UIPanGestureRecognizer(target: self, action: #selector(panButton(_:)))
let word = UIButton(type: .system)
word.addGestureRecognizer(pan)
我创建了这个函数来在按钮移动时触发:
func panButton(sender: UIPanGestureRecognizer){
if sender.state == .began {
//wordButtonCenter = button.center // store old button center
} else if sender.state == .ended || sender.state == .failed || sender.state == .cancelled {
//button.center = wordButtonCenter // restore button center
} else {
let location = sender.location(in: view) // get pan location
//button.center = location // set button to where finger is
}
}
我收到以下错误:
Use of unresolved identifier 'panButton'
首先,您的操作需要成为 Swift 3 中的一个选择器。所以它看起来像这样:
let pan = UIPanGestureRecognizer(target: self, action: #selector(panButton(_:))
此外,您不能通过选择器传递按钮的值,因此您需要将 func 更改为:
func panButton(sender: UIPanGestureRecognizer){
...
}
如果您想知道如果不能将按钮作为参数传递应该如何找到该按钮,那么您可以考虑使用标签。
正如@benjamin 在Swift 3 中指出的那样,您需要成为一个选择器。我已将代码更新为以下内容以提取按钮标签:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panGestureHandler(panGesture:)))
panGesture.minimumNumberOfTouches = 1
let word = UIButton(type: .system)
使用以下选择器:
@objc func panGestureHandler(panGesture recognizer: UIPanGestureRecognizer) {
let buttonTag = (recognizer.view?.tag)!
if let button = view.viewWithTag(buttonTag) as? UIButton {
if recognizer.state == .began {
wordButtonCenter = button.center // store old button center
} else if recognizer.state == .ended || recognizer.state == .failed || recognizer.state == .cancelled {
button.center = wordButtonCenter // restore button center
} else {
let location = recognizer.location(in: view) // get pan location
button.center = location // set button to where finger is
}
}
}