【问题标题】:enable disable swipe gesture swift启用禁用快速滑动手势
【发布时间】:2015-12-04 19:34:51
【问题描述】:

我的功能是下面的那个 我试图在数组达到其限制时禁用文本。 我收到数组超出范围的错误。当array1.count 等于swipeCount 时,我可以使用什么类型的条件来禁用数组。 这是我的代码:

 let array1 = ["a","b","c","d"]

 func getRandom1() {
    for var i = 0; i < array1.count ; i++
    {
        array1.shuffle1()
    }

}

func getText1() {

    self.display.text = "\(array1[i++])"
    swipeCount++
}

func getTextBack() {


    self.display.text = "\(array1[i])"

}


func handleSwipes(sender:UISwipeGestureRecognizer) {

if (sender.direction == .Right)
{

    if swipeCount != array1.count
    {
        getText1()
    }


    else

    {
        getTextBack()
    }

  }


  }

【问题讨论】:

  • array1 包含所有文本,例如 let array1 = ["a","b","c","d"]
  • @SabhaySardana:我在这里没有看到任何可能导致array out of range 的代码。请告诉我们getText1()notText() 是如何定义的。
  • func getText1() { self.display.text = "\(array1[i++])" swipeCount++ } func noTextBack() { self.display.text = "\(array1[i])" }
  • i 定义在哪里?
  • array1[i++] 超出了您的范围。在递增之前检查以确保有下一个元素。

标签: swift swipe-gesture ios9.1


【解决方案1】:
func handleSwipes(sender:UISwipeGestureRecognizer) {

if (sender.direction == .Right) {
   let aCount = array1.count - 1

   if swipeCount < aCount
{
    getText1()
}


else

{
    getTextBack()
}

}

【讨论】:

    【解决方案2】:

    改变这一行:

    if swipeCount != array1.count
    

    if swipeCount < array1.count - 1
    

    我认为你不能在 getText1 和 getTextBack 中使用 i。而不是使用 i,您应该像这样使用 swipeCount:

    func getText1() {
        self.display.text = "\(array1[swipeCount++])"
    }
    
    func getTextBack() {
        self.display.text = "\(array1[swipeCount])"
    }
    

    【讨论】:

    • 我之前尝试过,我刚刚更新了我的代码。你可以在我的编辑中看到:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多