2017-05-18 1 views
0

I débutant apprentissage Swift 3 - geste UISwipe mais ne peut pas fonctionner si vous utilisez Array.Swift 3 - Balayez vers la gauche pour changer d'étiquette en utilisant Array

Ceci est mon code.

Comment puis-je coder de sorte que l'étiquette ne change que de "hello1" à "hello2", puis glisse à gauche à "hello3". Balayez également vers l'arrière à droite de "hello3" à "hello2" et "hello1". ou revenir en arrière au premier.

Merci.

class ChangeLabelViewController: UIViewController { 

var helloArray = ["Hello1", "Hello2", "Hello3"] 
var currentArrayIndex = 0 


@IBOutlet weak var helloLabel: UILabel! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ChangeLabelViewController.handleSwipes(sender:))) 

    let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(ChangeLabelViewController.handleSwipes(sender:))) 

    leftSwipe.direction = .left 
    rightSwipe.direction = .right 

    view.addGestureRecognizer(leftSwipe) 
    view.addGestureRecognizer(rightSwipe) 

    helloLabel.text = helloArray[currentArrayIndex] 
} 

func handleSwipes(sender: UISwipeGestureRecognizer) { 

    if sender.direction == .left { 
     helloLabel.text = helloArray[currentArrayIndex + 1] 


    } 

    if sender.direction == .right { 

    } 
} 

Répondre

0

Essayez ceci:

if sender.direction == .left { 
    currentArrayIndex = (currentArrayIndex + 1) % 3 
    helloLabel.text = helloArray[currentArrayIndex] 
} 

if sender.direction == .right { 
    currentArrayIndex = (currentArrayIndex + 3 - 1) % 3 //if uInt 
    helloLabel.text = helloArray[currentArrayIndex] 
} 
+0

Merci simalone. Ce sont d'excellents conseils et tutoriels que j'ai appris. Je vais digérer le code plus loin. –

+0

J'espère que cela peut vous aider! :) @JimmyWong – simalone

0

vous pouvez aussi le faire avec le code ci-dessous,

func handleSwipes(sender: UISwipeGestureRecognizer) { 

    if sender.direction == .left { 

     if(currentArrayIndex < helloArray.count - 1) 
     { 
      currentArrayIndex += 1 
      let indexPath = IndexPath(item: currentArrayIndex, section: 0) 
      helloLabel.text = helloArray[indexPath.row] 
     } 
    } 

    if sender.direction == .right { 

     if(currentArrayIndex > 0) 
     { 
      currentArrayIndex -= 1 
      if currentArrayIndex == -1 
      { 
       currentArrayIndex = 0 
       let indexPath = IndexPath(item: currentArrayIndex, section: 0) 
       helloLabel.text = helloArray[indexPath.row] 
      } 
      else { 
       let indexPath = IndexPath(item: currentArrayIndex, section: 0) 
       helloLabel.text = helloArray[indexPath.row] 
      } 
     } 

    } 
+0

Je suis le code, lorsqu'il est exécuté jusqu'au dernier tableau, l'application a planté parce que "l'index hors de portée". Merci pour le guide. –

+0

Ohk .. maintenant vous pouvez vérifier –

+0

Merci Jigar. Le code a fonctionné parfaitement. Je vais essayer de le comprendre. –