我是 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()
}
最佳答案
movementDistance
是 Int
,它必须是 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/