【问题标题】:Getting a UITapGestureRecognizers Target获取 UITapGestureRecognizer 目标
【发布时间】:2017-04-14 04:56:51
【问题描述】:

我正在尝试创建一个基本视图,每次点击其backgroundColor 时都会在两种颜色之间切换。为了实现这一点,我创建了一个函数,该函数将要更改的视图和要在两种颜色之间切换的参数作为参数。

为此,我需要传递它应该更改的 UIView。但是 UIViews 不能原生识别触摸,所以我“链接”(我不确定我是否理解正确)一个UITapGestureRecognizer 到它,它触发了我的ViewController 中的一个动作。

现在我需要将 UITapGestures 链接到 UIView,以便我可以更改它。

我意识到我可以简单地将相应的 UIView 输入到操作中,但我想让这个功能尽可能灵活(只是尝试从正确的礼仪开始)。

这是我不完整的功能:

@IBAction func colorViewTapped(_ sender: Any) {
    print("tap")
    //Heres the problem: I get an "Cannot convert value of type 'Any' to expected argument type 'UIView'
    toggleViewBackgroundColor(view: sender, color1: UIColor.blue, color2: UIColor.brown)
}

【问题讨论】:

  • 你可以将sender转换成UITapGestureRecognizer而不是使用它。

标签: ios swift uiview uitapgesturerecognizer


【解决方案1】:

将您的操作方法参数类型从Any 更改为UITapGestureRecognizer,然后使用UIGestureRecognizerview 属性获取点击视图的引用。

@IBAction func colorViewTapped(_ sender: UITapGestureRecognizer) {
    print("tap")
    toggleViewBackgroundColor(view: sender.view!, color1: UIColor.blue, color2: UIColor.brown)
}

【讨论】:

  • .view 正是我想要的。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 2011-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多