ios - 如何在屏幕上交换文本字段的位置 - Swift

标签 ios swift

我有两个 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/

相关文章:

ios - 如何从自定义 CollectionViewController 的 didSelectItemAtIndexPath 实例化 ViewController,它是 tableViewCell 的一部分

iphone - iOS 应用程序在 Core Location 回调机制中崩溃

ios - 如何使用 Cognito 用户池注册用户 - 没有用户名和密码的 Amazon Web Services

ios - 如何将固定的 SCNNode 附加到 ARKit 的 pointOfView?

ios - UILabel.attributedText 设置属性一次,然后只更改文本(字符串)

ios - 核心数据 : Unable to load class for entity

objective-c - 如何在iPhone/iPad中访问PDF文档中的超链接?

ios - 切换到其他页面时取消选择选定的单元格

swift - 如何在 WKWebView 完成加载之前运行函数?

ios - SlideMenuController 未显示