Swift 调试代码:旋转木马显示字符串数组

Swift 调试代码:旋转木马显示字符串数组,swift,uigesturerecognizer,carousel,Swift,Uigesturerecognizer,Carousel,我正在编码一个显示字符串数组的旋转木马。选定的字符串元素将显示在UIView中 var plotValue:Int = 0 var flag:Int = 1 let plotList: [String] = ["Phenotype","SNP","Synthesis","PheWAS","Circos"] 基本上,用户可以向左或向右滑动,UILabel将使用plotList中的新值进行更新 向左滑动使计数器递减-1,向右滑动使计数器递增+1 如果用户达到绘图列表的初始值[0]并继续

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

  var plotValue:Int = 0
  var flag:Int = 1
  let plotList: [String] = ["Phenotype","SNP","Synthesis","PheWAS","Circos"]
基本上,用户可以向左或向右滑动,UILabel将使用plotList中的新值进行更新

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

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

如果用户达到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)
  }

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


也许我想得太多了,有更简单的方法吗?还是苹果“首选”的方式?或者一个更为面向对象的方法???

将逻辑更改为此,并且可以工作,并将在两个方向上进行包装

  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]
  }

我很高兴你明白了,因为我不明白你在问什么。什么是旋转木马?这是Nick Lockwood iCarousel吗?如果你能给我们提供足够的背景资料,让我们知道你在做什么,这可能会对其他读者有所帮助。虽然我使用的是UILabel和UIView,但它不是iCarousel的一部分。我看了看ObjC代码,放弃了,决定自己写。我将在问题中添加一些文本。
  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]
  }