【问题标题】:How to add swipe gesture to segmented control?如何将滑动手势添加到分段控件?
【发布时间】:2015-11-15 17:38:19
【问题描述】:

我将ImageView 添加到UIView。点击图片转到Segmented Control,这也是UIView 的一部分。我正在尝试向这个分段控件添加滑动手势。

尝试了以下。

override func viewDidLoad() {
    super.viewDidLoad()

    let rightSwipe = UISwipeGestureRecognizer(target: SegmentCotroller, action: Selector("swiped:"))
    rightSwipe.direction = .Right
    self.SegmentCotroller.addGestureRecognizer(rightSwipe)
}

func swiped(sender:UIGestureRecognizer){
    print("Swiped.....!")
}

向右滑动时,代码永远不会到达滑动方法。

任何帮助表示赞赏! 谢谢

【问题讨论】:

  • 您是否在代码或情节提要中创建了您的 Segmented Control 对象,如果在情节提要中,那么您是否有 IBOutlet 为它创建对象?

标签: swift ios9 uisegmentedcontrol uiswipegesturerecognizer


【解决方案1】:

您需要将滑动手势目标设置为self

let rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("swiped:"))
rightSwipe.direction = .Right
self.segmentController.addGestureRecognizer(rightSwipe)

目标设置将执行操作的位置,因此您希望它指向执行操作的任何位置。

【讨论】:

  • 感谢您的回复。我之前确实尝试过,但没有成功。
  • 好的。然后您的控制器中还有其他问题。我试过了,效果很好。您是如何设置分段控制的?它是一个出口还是你在代码中创建它?
  • 你能解释一下你的用法,也许可以贴一张截图吗?你的问题有点难以理解。
  • 我确实有 segmentedController @IBOutlet weak var SegmentCotroller: UISegmentedControl 的出口!
  • 嗯.. 我在以编程方式创建的分段控制器上尝试了您的 swipeGestureRecognizer 代码,除了目标之外,代码没有任何问题。因此,如果没有有关其余代码或某些屏幕截图的更多信息,很难为您提供帮助。对不起
猜你喜欢
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多