UIButton
有一个已发送的事件acton touchDragInside
(Touch Drag Inside)。该按钮可以连接到许多动作,例如:
@IBAction func dragButton(sender: UIButton) {
...
}
答案应该集中在确定拖拽方向,左,右,上,下,在Swift编码中。
开发人员备注中没有大量信息:https://developer.apple.com/documentation/uikit/uicontrolevents/1618240-touchdraginside
问题:
当我点击并拖动UIButton
发送的事件操作touchDragInside
时,如何确定实际的拖动方向?
答案 0 :(得分:1)
所以,我有一个答案。可能不是最漂亮的代码,但效果很好。
该怎么做:
只需在新的Xcode项目中将UIButton
放到故事板上即可。
将下面的Swift 2代码放入ViewController.swift文件中。
将UIButton
touchDown
和touchDragInside
操作连接到相应的代码。
点击Xcode中的Run►启动模拟器。
应用程序运行后,点按并拖动UIButton
即可打印出拖动方向。
<强>代码:强>
// ViewController.swift
import UIKit
class ViewController: UIViewController {
var touchDragInsideLocationX: CGFloat!
var touchDragInsideLocationX1: CGFloat!
var touchDragInsideLocationX2: CGFloat!
var touchDragInsideLocationX3: CGFloat!
var touchDragInsideLocationY: CGFloat!
var touchDragInsideLocationY1: CGFloat!
var touchDragInsideLocationY2: CGFloat!
var touchDragInsideLocationY3: CGFloat!
@IBAction func touchDownAction(sender: AnyObject, event: UIEvent) {
// Reset X location points
touchDragInsideLocationX = 0
touchDragInsideLocationX1 = 0
touchDragInsideLocationX2 = 0
touchDragInsideLocationX3 = 0
// Reset Y location points
touchDragInsideLocationY = 0
touchDragInsideLocationY1 = 0
touchDragInsideLocationY2 = 0
touchDragInsideLocationY3 = 0
}
@IBAction func touchDragInsideAction(sender: AnyObject, event: UIEvent) {
// Get current touchDragInside X and Y location points
let button = sender as? UIButton
let touch = (event.touchesForView(button!)?.first)! as UITouch
let location = touch.locationInView(button)
touchDragInsideLocationX = location.x
touchDragInsideLocationY = location.y
// Array previous touchDragInside X location points
touchDragInsideLocationX3 = touchDragInsideLocationX2
touchDragInsideLocationX2 = touchDragInsideLocationX1
touchDragInsideLocationX1 = touchDragInsideLocationX
// Array previous touchDragInside Y location points
touchDragInsideLocationY3 = touchDragInsideLocationY2
touchDragInsideLocationY2 = touchDragInsideLocationY1
touchDragInsideLocationY1 = touchDragInsideLocationY
// Determine touchDragInside X and Y velocity
let touchDragInsideVelocityX = abs(touchDragInsideLocationX - touchDragInsideLocationX3)
let touchDragInsideVelocityY = abs(touchDragInsideLocationY - touchDragInsideLocationY3)
// Determine touchDragInside direction
if touchDragInsideVelocityX > touchDragInsideVelocityY {
if touchDragInsideLocationX3 != 0 {
let touchDragInsideVelocityX = (touchDragInsideLocationX3 - touchDragInsideLocationX1)
if touchDragInsideVelocityX > 0 {touchDragInsideLeft()} else {touchDragInsideRight()}
}
} else {
if touchDragInsideLocationY3 != 0 {
let touchDragInsideVelocityY = (touchDragInsideLocationY3 - touchDragInsideLocationY1)
if touchDragInsideVelocityY > 0 {touchDragInsideUp()} else {touchDragInsideDown()}
}
}
}
func touchDragInsideLeft() {
print("Left")
}
func touchDragInsideRight() {
print("Right")
}
func touchDragInsideUp() {
print("Up")
}
func touchDragInsideDown() {
print("Down")
}
}
答案 1 :(得分:0)
@ user4806509 - 谢谢,这正是我想要的。我很高兴我走在正确的轨道上,并没有必要重新发明这个轮子!
Swift 4 Xcode 9的两个非常小的变化:
let touch = (event.touchesForView(button!)?.first)! as UITouch
...已重命名,现在可以使用:
let touch = (event.touches(for: button!)?.first)! as UITouch
此外,
let location = touch.locationInView(button)
...已重命名,现在可以使用:
let location = touch.location(in: button)
答案 2 :(得分:0)
我之前发布过,但从那以后我发现(不是这里)UIKit实际上提供了这个确切的功能。
有几个手势识别器可以检测捏,滑动,平移等。这两个解决原始问题:
我将这些拖到按钮上,也拖到了UI中的视图上(效果很好,是一个优雅的解决方案)。拖动其中一个后,您必须设置(例如)滑动的特定方向。然后,您可以从识别器控制拖动到视图控制器代码,以定义所需的IBAction功能。
以下是UP和DOWN滑动识别的基本代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBOutlet var textfield: UITextField!
@IBAction func UP(_ sender: UISwipeGestureRecognizer) {
textfield.text = "BUTTON was swiped UP"
}
@IBAction func DOWN(_ sender: UISwipeGestureRecognizer) {
textfield.text = "BUTTON was swiped DOWN"
}
@IBAction func UPview(_ sender: UISwipeGestureRecognizer) {
textfield.text = "VIEW was swiped UP"
}
@IBAction func DOWNview(_ sender: UISwipeGestureRecognizer) {
textfield.text = "VIEW was swiped DOWN"
}
}