在UIButton上查找touchDragInside发送事件的方向



UIButton有一个已发送的事件actontouchDragInside(触摸内部拖动(。该按钮可以连接到许多操作,例如:

@IBAction func dragButton(sender: UIButton) {
...
}

答案应该集中在确定 Swift 编码中的拖动方向,左、右、上、下。

开发人员笔记中没有太多信息:https://developer.apple.com/documentation/uikit/uicontrolevents/1618240-touchdraginside


问题:

当我点击并拖动已发送事件操作touchDragInsideUIButton时,如何确定实际拖动方向?

所以,我有一个答案。可能不是最漂亮的代码,但它运行良好。

怎么办:

  1. 只需将UIButton放到新 Xcode 项目中的故事板上即可。

  2. 将下面的 Swift 2 代码放入 ViewController.swift 文件中。

  3. UIButtontouchDowntouchDragInside操作连接到相应的代码。

  4. 轻点"在 Xcode 中运行 ►"以启动模拟器。

  5. 应用运行后,点击并拖动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")
}
}

@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)

我之前在上面发布过,但从那时起我发现(不在这里(UIKit实际上提供了这个确切的功能。

有几个手势识别器可以检测捏合、滑动、平移等。这两个解决了最初的问题:

  • 滑动手势识别器 - 在检查器中,将其设置为检测以下之一: 向上、向下、向左或向右轻扫(为第二个、第三个和 第四方向识别(
  • 平移手势识别器(见上文(

我将它们拖到按钮上,也拖到UI中的视图上(效果很好,是一个优雅的解决方案(。拖动其中一个后,您必须设置(例如(滑动的特定方向。然后,从识别器按住 Control 键拖动到视图控制器代码,以定义所需的 IBAction 函数。

以下是向上和向下滑动识别的基本代码:

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"
}
}

最新更新