调试代码:轮播显示字符串数组

Debugging code: carousel display string arrays

我正在编写一个显示字符串数组的旋转木马。选中的字符串元素将显示在 UIView 中。

  var plotValue:Int = 0
  var flag:Int = 1
  let plotList: [String] = ["Phenotype","SNP","Synthesis","PheWAS","Circos"]

基本上,用户可以向左或向右滑动,UILabel 会更新为 plotList 中的新值。

向左滑动计数器递减 -1,向右滑动计数器递增 +1。

如果用户达到 plotList[0] 的初始值继续向左滑动,代码将环绕并从 plotList 中的最大元素开始。

如果用户达到plotList的最大值并继续向右滑动,代码将环绕并从plotList[0]开始。

一旦用户点击 UIView,就会启动另一个进程。

var swipeCarouselLeft: UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: "carouselLeft")
swipeCarouselLeft.direction = UISwipeGestureRecognizerDirection.Left
self.labelView.addGestureRecognizer(swipeCarouselLeft)

var swipeCarouselRight: UISwipeGestureRecognizer =
UISwipeGestureRecognizer(target: self, action: "carouselRight")
swipeCarouselRight.direction = UISwipeGestureRecognizerDirection.Right
self.labelView.addGestureRecognizer(swipeCarouselRight)

var tapButton:UITapGestureRecognizer =
UITapGestureRecognizer(target: self, action: "carouselTap")
self.labelView.addGestureRecognizer(tapButton)

这里是定义的函数。

  func carouselLeft(){
    AudioServicesPlaySystemSound(1052)
    flag = -1
    getLabel(flag)
  }

  func carouselRight(){
    AudioServicesPlaySystemSound(1054)
    flag = 1
    getLabel(flag)
  }

  func getLabel(flag: Int){
    plotValue = plotValue + flag
    println("plotValue \(plotValue)")
    switch plotValue {
    case (plotList.count):
      plotValue = 0
    case (-1):
      plotValue = plotList.count - 1
    default:
      plotValue = 0
    }
    UIView.animateWithDuration(2, animations: { () -> Void in
      self.labelOutlet.textColor = UIColor.blueColor()
    })
    self.labelOutlet.text = plotList[plotValue]
    println("\(plotList[plotValue])UIView")
  }

  func carouselTap(){
    AudioServicesPlaySystemSound(1057)
}

基本上,当用户滑动时,结果字符串是 plotList 数组中的第一个或最后一个元素,显示数组中其他元素的 none。

也许我想多了,有更简单的方法吗?还是 Apple "preferred" 的做法?还是更面向对象??

将逻辑更改为此并起作用,并且将在两个方向上换行。

  func getLabel(flag: Int){
    if (flag == -1 && plotValue == 0){
      plotValue = plotList.count - 1
    }
    else if (flag == 1 && plotValue == plotList.count - 1)
    {
      plotValue = 0
    }
    else
    {
      plotValue = plotValue + flag
    }
    UIView.animateWithDuration(2, animations: { () -> Void in
      self.labelOutlet.textColor = UIColor.blueColor()
    })
    self.labelOutlet.text = plotList[plotValue]
  }