ios - Swift 无法将表达式的类型 '()' 转换为 CGRect 类型

标签 ios swift uitextfield type-conversion uiviewanimation

我是 Swift 新手,我正在尝试为 textField 设置动画,因为我的键盘隐藏了它。 在 Objective-C 中,这段代码运行得很好。 这是编辑 did begin 事件函数:

@IBAction func passwordEditingBegin(sender : UITextField) {
    var movementDistance:Int = 50
    var movementDuration = 0.3

    UIView.beginAnimations("anim", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration)



    self.view.frame = CGRectOffset(self.view.frame, 0, movementDistance)
                    //^ Can't convert the expressions type '()' to type CGFloat
    UIView.commitAnimations()
}

最佳答案

movementDistanceInt ,它必须是 CGFLoat对于 CGRectOffset .

var movementDistance:CGFloat = 50

Objective-C 会自动提升变量,而 Swift 不会。

CGRectOffset 的定义

func CGRectOffset(rect: CGRect, dx: CGFloat, dy: CGFloat) -> CGRect

关于ios - Swift 无法将表达式的类型 '()' 转换为 CGRect 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25339327/

相关文章:

iphone - 如何在 iPhone 中使用异步 Web 服务调用自动完成 TextField?

ios - 将辅助功能画外音光标焦点移动到特定元素

ios - UIStackView 和 subview 大小类

ios - 如何在应用程序切换器/关闭器栏上方设置输入附件 View 的约束?

ios - 如何禁用删除并移动 UICollectionView 中的第一个单元格?

iphone - 如何使用 UIPickerView 在一个 View 中填充不同的文本字段?

ios - 如何不允许用户在 uitextfield 中输入超过 190 的值?

ios - 如何更改当前显示的 UINavigationController 的标题

ios - Objective-c BOOL 总是传递

ios - 我们应该如何显示以下 ViewController?