gpt4 book ai didi

ios - 获取 UITapGestureRecognizer 目标

转载 作者:可可西里 更新时间:2023-11-01 00:41:17 25 4
gpt4 key购买 nike

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

为此,我需要传递 UIView,然后它应该更改。但是 UIViews 本身无法识别触摸,所以我“链接”(我不确定我是否理解正确)一个 UITapGestureRecognizer 到它,触发我的 ViewController 中的一个 Action 。

现在我需要将 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)
}

最佳答案

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

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

关于ios - 获取 UITapGestureRecognizer 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43405414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com