Closed:: 创建一个带有滑动和点击手势的 UIButton 水平旋转木马

Closed:: Creating a UIButton horizontal carousel with swipe and tap gestures

在 VC 中,我正在尝试弄清楚如何让 UIButton 识别水平滑动然后点击。

有一个文本数组。

var plotList = ["stuff", "to do", "this", "or that"]

默认显示为 plotList[0]。

无限循环中的轮播不会停在数组的末尾。

当用户仅在按钮上滑动时,我不希望 VC 的其他组件对这个特定的滑动做出反应,我希望按钮显示 plotList 字符串。

当用户点击按钮时,我想将结果推送到将启动相应 UIView 的开关中。

我想避免这样做 UILabel/UIButton 组合。参见 Handling Touch Event in UILabel and hooking it up to an IBAction

到目前为止我在这里

import UIKit

class carouselVC: UIViewController {


  @IBOutlet var carouselView: UIView!
  @IBOutlet var labelOutlet: UILabel!
  @IBOutlet var buttonOutlet: UIButton!

  var plotList = ["stuff", "this", "that"]

  let swipeRec = UISwipeGestureRecognizer()


  override func viewDidLoad() {
    super.viewDidLoad()
    var swipeButtonLeft: UISwipeGestureRecognizer =
    UISwipeGestureRecognizer(target: self, action: "buttonLeft")
    swipeButtonLeft.direction = UISwipeGestureRecognizerDirection.Left
    self.buttonOutlet.addGestureRecognizer(swipeButtonLeft)

    var swipeButtonRight: UISwipeGestureRecognizer =
    UISwipeGestureRecognizer(target: self, action: "buttonRight")
    swipeButtonRight.direction = UISwipeGestureRecognizerDirection.Right
    self.buttonOutlet.addGestureRecognizer(swipeButtonRight)

  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  func buttonLeft(){
    println("buttonLeft")
  }
  func buttonRight(){
    println("buttonRight")
  }

  @IBAction func buttonAction(sender: UIButton) {
    sender.setTitle(plotList[1], forState: .Normal)
  }
}

有什么帮助吗?

使用带有 UILabel 的 UIView,而不是按钮。在 UILabel 上添加 UIView 并使其颜色清晰。你可以让 UIlabel 看起来像一个按钮。在其上添加滑动 gesture/tap 手势。并在手势的动作中做任何操作。