我知道有很多關於這個問題的問題,但我沒有發現對我有用。索引超出範圍錯誤在Swift 3
我正在做一個應用程序,能夠識別屏幕上的移動。但蘋果手勢識別器對於我的使用來說太精確了,所以我正在做我自己的。
它幾乎完成並正在工作。
我想處理多點觸控手勢(如捏),我需要獲得每個手指在屏幕上的方向,爲此我想包括每個方向在一個數組中,所以我會比較他們之後很容易。但是我得到的索引超出範圍的錯誤,我不知道我錯在哪裏。我很新的迅速(自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]
}
}
有很多陣列被used.please調試和檢查它你在哪裏獲得索引超出範圍。 –
@DSDharma你是對的,我的壞。那時我在線上調用函數GestureRecognizer:direction1 [index] =「Something」這就是我得到錯誤的地方。 – Hawkydoky