对两种不同的类型使用 swap(_:_:)
Using swap(_:_:) with two different types
我目前有我的主视图设置,当我点击 UILabel
时,它会将其“不透明度”更改为 0,并且会出现一个文本字段,该文本字段最初是隐藏的,其“不透明度”设置为 0上述值反转 (hidden = false, opacity = 1
).
我打算经常交换这两个视图之间的值,所以我认为使用 swap(_:_:)
函数会比较理想。不幸的是,我收到此错误,因为 UILabel
和 UITextField
不是同一类型:
Cannot convert value of type 'UITextField!' to expected argument type '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)
})
}
我目前有我的主视图设置,当我点击 UILabel
时,它会将其“不透明度”更改为 0,并且会出现一个文本字段,该文本字段最初是隐藏的,其“不透明度”设置为 0上述值反转 (hidden = false, opacity = 1
).
我打算经常交换这两个视图之间的值,所以我认为使用 swap(_:_:)
函数会比较理想。不幸的是,我收到此错误,因为 UILabel
和 UITextField
不是同一类型:
Cannot convert value of type 'UITextField!' to expected argument type '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)
})
}