【发布时间】:2014-07-23 23:45:38
【问题描述】:
在我的代码中,当键盘弹出时,我已正确地将 UITextfield 移动到视图中。但是,我一直在努力创建另一个仅作用于 UIImageView 的动画,因为当前的解决方案会永久偏移 UIImageView 中的徽标。
var logoImage:UIImageView!
var logoImageX:CGFloat = 85
var logoImageY:CGFloat = 35
// Logo Code
let logo:UIImage = UIImage(named:"color-logo.png")
let logoHeight:CGFloat = 150
let logoWidth:CGFloat = logoHeight
logoImage = UIImageView(image:Logo)
logoImage.frame = CGRectMake(logoImageX,logoImageY,logoWidth, logoHeight)
self.view.insertSubview(logoImage, atIndex: 1)
// Keyboard Auto Scroll
func textFieldDidBeginEditing(textField: UITextField!) {
self.animateTextField(textField, up: true)
self.animateWithDuration(true)
}
func textFieldDidEndEditing(textField: UITextField!) {
self.animateTextField(textField, up: false)
self.animateWithDuration(false)
}
func animateTextField(textField:UITextField,up: Bool){
let movementDistance:Int = -100
let movementDuration:NSTimeInterval = 0.25
var movement = CGFloat(Int((up ? movementDistance : -movementDistance)))
UIView.beginAnimations("animateTextField", context:nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration)
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
这是 UIImageView 的函数:
//Logo (UIImageView) Scroll
func animateWithDuration(up:Bool){
let logoMovementDuration = 0.25
let logoMovementDistance:Int = 20
var logoMovement = CGFloat(Int((up ? logoMovementDistance : -logoMovementDistance + (logoImageY))))
UIView.beginAnimations("animateWithDuration", context:nil)
UIView.setAnimationDuration(logoMovementDuration)
logoImage.frame = CGRectMake(logoImageX,logoMovement,logoImage.frame.size.width,logoImage.frame.size.height)
UIView.commitAnimations()
}
【问题讨论】:
-
你为什么不使用基于块的动画?自 iOS 4 以来,Apple 一直建议不要使用 beginAnimations/commitAnimations 方法。
-
我从 iOS 7 开始才开始开发,我还没有注意到 beginAnimations/commitAnimations 方法的推动。你认为基于块的动画是解决我当前难题的关键吗?
-
你是对的,通过文档我发现 Apple 确实推荐 iOS 4.0 及更低版本的 beginAnimation/commitAnimation 以及所有 iOS 版本的动画块。我找到了答案并进行了更新。
标签: ios animation uiview uiimageview swift