我知道在 SO 中有很多关于这个问题的问题,但我没有发现对我有用的东西。
我正在开发一款能够识别屏幕上的 Action 的应用。但是 Apple 手势识别器对我来说太精确了,所以我自己做。
它几乎完成并开始工作。
我想处理多点触控手势(如捏合),我需要获取每个手指在屏幕上的方向,为此我想将每个方向都包含在一个数组中,以便之后我可以轻松地比较它们。但是我得到了索引超出范围的错误,我不知道我哪里错了。我对 swift 还很陌生(从 1 个月开始自学)所以它可能是显而易见的甚至是一个愚蠢的错误......
如果你能帮我解决这个问题,我会很高兴。 谢谢 !
这是我的完整代码:
import UIKit
class ViewController: UIViewController {
//@IBOutlet weak var statusLabel: UILabel!
@IBOutlet weak var StatusLabel: UILabel!
var fingers = [String?](repeating: nil, count:10)
var finger1 = [CGFloat]()
var finger2 = [CGFloat]()
var finger3 = [CGFloat]()
var finger4 = [CGFloat]()
var finger5 = [CGFloat]()
var direction: String = ""
var direction1 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
direction = ""
direction1 = []
finger1 = []
finger2 = []
finger3 = []
finger4 = []
finger5 = []
for touch in touches{
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if finger == nil {
fingers[index] = String(format: "%p", touch)
print("finger \(index+1): x=\(point.x) , y=\(point.y)")
break
}
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
for touch in touches {
let point = touch.location(in: self.view)
for (index,finger) in fingers.enumerated() {
if let finger = finger, finger == String(format: "%p", touch) {
switch (index){
case 0 :
finger1 += [point.x, point.y]
case 1 :
finger2 += [point.x, point.y]
case 2 :
finger3 += [point.x, point.y]
case 3 :
finger4 += [point.x, point.y]
case 4 :
finger5 += [point.x, point.y]
default :
break
}
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
for touch in touches {
for (index,finger) in fingers.enumerated() {
if let finger = finger, finger == String(format: "%p", touch) {
fingers[index] = nil
break
}
}
}
if finger1.count != 0 {
direction1[0] += GestureRecognizer(coordinates: finger1, index: 0)
}
if finger2.count != 0 {
direction1[1] += GestureRecognizer(coordinates: finger2, index: 1)
}
if finger3.count != 0 {
direction1[2] += GestureRecognizer(coordinates: finger3, index: 2)
}
if finger4.count != 0 {
direction1[3] += GestureRecognizer(coordinates: finger4, index: 3)
}
if finger5.count != 0 {
direction1[4] += GestureRecognizer(coordinates: finger5, index: 4)
}
print("1 " + direction1[0] + "2 " + direction1[1] + "3 " + direction1[2] + "4 " + direction1[3] + "5 " + direction1[4])
StatusLabel.text = direction1[1]
}
func GestureRecognizer (coordinates: [CGFloat], index: Int) -> String {
if (coordinates[0] - coordinates[coordinates.count-2]) > 100 && (coordinates[1] - coordinates[coordinates.count-1]) < (-100) {
print("Vers la gauche et bas")
direction1[0] = "downleft"
}
else if (coordinates[0] - coordinates[coordinates.count-2]) < (-100) && (coordinates[1] - coordinates[coordinates.count-1]) > 100{
print("Vers la droite et haut")
direction1[index] = "upright"
}
else if (coordinates[0] - coordinates[coordinates.count-2]) < (-100 ) && (coordinates[1] - coordinates[coordinates.count-1]) < (-100){
print("Vers la droite et bas")
direction1[index] = "downright"
}
else if (coordinates[0] - coordinates[coordinates.count-2]) > 100 && (coordinates[1] - coordinates[coordinates.count-1]) > 100 {
print("Vers la gauche et haut")
direction1[index] = "upleft"
}
else if (-100..<100).contains(coordinates[0] - coordinates[coordinates.count-2]) && (coordinates[1] - coordinates[coordinates.count-1]) > 100 {
print("Swipe up")
direction1[index] = "swipeup"
}
else if (-100..<100).contains(coordinates[0] - coordinates[coordinates.count-2]) && (coordinates[1] - coordinates[coordinates.count-1]) < -100 {
print("Swipe Down")
direction1[index] = "swipedown"
}
else if (coordinates[0] - coordinates[coordinates.count-2]) > 100 && (-100..<100).contains(coordinates[1] - coordinates[coordinates.count-1]){
print("Swipe left")
direction1[index] = "swipeleft"
}
else if (coordinates[0] - coordinates[coordinates.count-2]) < -100 && (-100..<100).contains(coordinates[1] - coordinates[coordinates.count-1]){
print("Swipe right")
direction1[index] = "swiperight"
}
else {
direction1[index] = "failed"
}
return direction1[index]
}
}
最佳答案
您应该重新检查您的 direction1
数组。错误很可能在这一行:
print("1 " + direction1[0] + "2 " + direction1[1] + "3 " + direction1[2] + "4 " + direction1[3] + "5 " + direction1[4])
您已经设置了 direction1 = []
,然后您尝试在 direction1
数组中打印 5 个不同的元素。
解决方法是初始化一个固定大小的数组 [String](repeating: "", count: 5)
并删除此行 direction1 = []
关于ios - Swift 3 中的索引超出范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44899458/