[英]How to swap textfields position on screen - Swift
I have two UITextFields and want to swap their position when a button is clicked. 我有两个UITextFields,并希望在单击按钮时交换它们的位置。 Perhaps also animate the swap.
也许还会为交换制作动画。
ie: 即:
textFieldOne & textFieldOne are positioned with AutoLayout using Interface Builder. textFieldOne和textFieldOne使用Interface Builder定位AutoLayout。
On Button Click 按钮单击
To simply swap positions of your UITextField
's you can assign their origin
values to each other. 要简单地交换
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()
Or, for a more Swift way of doing things you can use a Tuple to swap the values without the need for any temporary variables. 或者,对于更快速的Swift方式,您可以使用Tuple交换值而无需任何临时变量。
UIView.beginAnimations(nil, context: nil)
(textFieldOne.frame.origin, textFieldTwo.frame.origin) = (textFieldTwo.frame.origin, textFieldOne.frame.origin)
UIView.commitAnimations()
In the function that receive the button click you will need to save the position of each button and transfer the other: 在接收按钮单击的功能中,您需要保存每个按钮的位置并转移另一个按钮:
Swap position: 交换位置:
let positionButton1 = button1.frame.origin
let positionButton2 = button2.frame.origin
button1.frame.origin = positionButton2
button2.frame.origin = positionButton1
Swap positions and size: 交换头寸和大小:
let positionButton1 = button1.frame
let positionButton2 = button2.frame
button1.frame = positionButton2
button2.frame = positionButton1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.