ios - 使用交换(_ :_:) with two different types

标签 ios swift swap

我目前有我的主视图设置,当我点击一个 UILabel 时,它会将其“不透明度”更改为 0,并且会出现一个文本字段,该文本字段最初是隐藏的并且其“不透明度设置为 0”上述值反转 (hidden = false, opacity = 1)。

我打算经常交换这两个 View 之间的值,所以我认为使用 swap(_:_:) 函数是理想的选择。不幸的是,我收到此错误,因为 UILabelUITextField 不是同一类型:

无法转换“UITextField!”类型的值到预期的参数类型“UILabel”

有什么办法可以解决这个问题,还是我会被困在创建自己的函数中?

@IBAction func onTapToEnterBillAmount(_ sender: UITapGestureRecognizer) {
    UIView.animate(withDuration: 0.2, animations: {
        swap(&self.tapToEnterBillAmountLabel, &self.billAmountTextField)
    })
}

最佳答案

由于 swap(_:_:) 要求两个参数的类型相同,请尝试交换 UIButton/UILabel< 的实际匹配属性:

@IBAction func onTapToEnterBillAmount(_ sender: UITapGestureRecognizer) {
    UIView.animate(withDuration: 0.2, animations: {
        swap(&self.tapToEnterBillAmountLabel.alpha, &self.billAmountTextField.alpha)
    }, completion: { _ in
        swap(&self.tapToEnterBillAmountLabel.isHidden, &self.billAmountTextField.isHidden)
    })
}

关于ios - 使用交换(_ :_:) with two different types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45985292/

相关文章:

ios - 预填充 Realm 数据库 swift 3.0

java - 对两个 ArrayList 中的元素进行排序和交换 (Java)

java - 交换输入并将输出打印为列

swift - Swift 方法中的第一个参数标签

ios - 如何在不使用第三方 url 的情况下获取 iPhone 的公共(public) IP 地址

JavaScript:按索引交换数组值

ios - 在 UIAlertController 中更改 UITextField 颜色后出现白色 "outer border"

iphone - 高级 iOS 原型(prototype)制作工具

ios - 不更新swift4中UItableViewCell的内容

ios - 如何限制 iOS 中的前缀文本字段字符串?