我有两个 UITextField 并且想在单击按钮时交换它们的位置。也许还可以为交换设置动画。
即:
textFieldOne 和 textFieldOne 使用 Interface Builder 通过 AutoLayout 定位。
点击按钮
- 获取textFieldOne位置参数,
- 获取textFieldTwo位置参数
- 将textFieldOne位置参数设置为textFieldTwo,
- 设置textFieldTwo位置参数为textFieldOne
最佳答案
要简单地交换 UITextField
的位置,您可以将它们的 origin
值分配给彼此。
// Store textFieldOne's postition
let tempPos : CGPoint = textFieldOne.frame.origin
UIView.beginAnimations(nil, context: nil)
// Set textFieldOne to textFieldTwo's position
textFieldOne.frame.origin = textFieldTwo.frame.origin
// Set textFieldTwo to textFieldOne's original position
textFieldTwo.frame.origin = tempPos
UIView.commitAnimations()
或者,您可以使用 Tuple 以更Swift 的方式做事无需任何临时变量即可交换值。
UIView.beginAnimations(nil, context: nil)
(textFieldOne.frame.origin, textFieldTwo.frame.origin) = (textFieldTwo.frame.origin, textFieldOne.frame.origin)
UIView.commitAnimations()
关于ios - 如何在屏幕上交换文本字段的位置 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30417992/