简体   繁体   English

从UISwipeGestureRecognizer的功能更新阵列索引

[英]Updating Array Index from the function of UISwipeGestureRecognizer

Label view shall display the next item (just as text string) from an array when it is swiped. 标签视图在刷卡时应显示数组中的下一个项目(就像文本字符串一样)。 So, I made index of array with initial value of 0 and referenced it within the function. 所以,我创建了初始值为0的数组索引,并在函数内引用了它。 However, when the label is swiped the index does not update. 但是,当刷标签时,索引不会更新。 Would be great to understand how to update the index when swiped label. 很高兴了解如何在刷标签时更新索引。 I also tried by putting index var within function, but it did not work either. 我也尝试将index var放在函数中,但它也没有用。 Here is the code. 这是代码。 Thanks. 谢谢。

   import UIKit

     class ViewController: UIViewController 
{
         @IBOutlet var result: UILabel!
         var index: Int = 0
         var items: [String]  = ["apple", "hammer", "cup"]
         let swipeRec = UISwipeGestureRecognizer()

         override func viewDidLoad() {
             super.viewDidLoad()

             swipeRec.addTarget(self, action: "swipedView")
             result.addGestureRecognizer(swipeRec)
             result.userInteractionEnabled = true
         }

          func swipedView () {
             result.text = "\(items[index])"
             index++
         } 
}

in viewDidLoad 在viewDidLoad中

    var leftSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
    var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))

    leftSwipe.direction = .Left
    rightSwipe.direction = .Right

    result.addGestureRecognizer(leftSwipe)
    result.addGestureRecognizer(rightSwipe) viewDidLoad
    result.userInteractionEnabled = true


func handleSwipes(sender:UISwipeGestureRecognizer) {
    if (sender.direction == .Left) {
        println("Swipe Left")
        if index > 0 { index -= 1}
    }

    if (sender.direction == .Right) {
        println("Swipe Right")
        if index < (items.count - 1) { index += 1}
    }

    println(items[index])
    result.text = items[index]

}

This is working for me. 这对我有用。 Tested on a real iPhone 4s. 在真正的iPhone 4s上测试过。

@IBOutlet var result: UILabel!
var index: Int = 0
var items: [String]  = ["apple", "hammer", "cup"]

override func viewDidLoad() {
    super.viewDidLoad()

    var leftSwipe = UISwipeGestureRecognizer(target: self, action: "swipedView:")
    var rightSwipe = UISwipeGestureRecognizer(target: self, action: "swipedView:")

    leftSwipe.direction = .Left
    rightSwipe.direction = .Right

    result.addGestureRecognizer(leftSwipe)
    result.addGestureRecognizer(rightSwipe)

    result.userInteractionEnabled = true
    result.text = items[index]
}

func swipedView(sender:UISwipeGestureRecognizer!) {
    if (sender.direction == .Left) {
        println("Swipe left")
        if index > 0 {
            index -= 1
        }
    }

    if (sender.direction == .Right) {
        println("Swipe right")
        if index < (items.count - 1) {
            index += 1
        }
    }

    result.text = items[index]
}

As we are adding UISwipeGestureRecognizer to the result UILabel, make sure that the label has the necessary size to allow us to make the gesture and if you are swiping the label and not the background view. 当我们将UISwipeGestureRecognizer添加到结果UILabel时,请确保标签具有允许我们制作手势的必要大小,以及是否要滑动标签而不是背景视图。 The solution provided by HMHero works for me too. HMHero提供的解决方案也适用于我。

Hope that helps. 希望有所帮助。

The original posted code did work for me. 原始发布的代码确实对我有用。 Just be sure to specify the direction you would like the user to use. 请务必指定您希望用户使用的方向。 The direction defaulted to a right swipe. 方向默认为向右滑动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM