Swift:为键盘抬起视图时视图弹回
Swift: View bounces back when raising it for keypad
我试图在键盘出现时提升视图,但它只是反弹回来。如果我给 CGRectOffset
Y 一个正值(向下移动视图),那么它会保持不变。
@IBOutlet weak var PPI: MainTextFields!
var KbHeight: CGFloat!
//----------------------------------------------
func animateTextField(up: Bool) {
let TFMovement = (up ? -KbHeight : KbHeight)
UIView.animateWithDuration(0.7, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, TFMovement)
})
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
KbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
//----------------------------------------------
// Touch Outside Keypad to leave it
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
// For TextField Movement
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
// For TextField Movement
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
PPI.delegate = self
}
我在 keyboardWillShow
函数中的 CGRectOffset
做错了吗?我是 Swift 的新手,是从教程中学到的,但似乎工作不正常。
我想在 viewController 加载时制作从左到右的背景移动动画。这是我在我的应用程序中所做的:
var backgroundImageView: UIImageView = UIImageView(image:
UIImage(named: "background.png")!)
self.view.addSubview(backgroundImageView)
self.view.sendSubviewToBack(backgroundImageView)
UIView.animateWithDuration(20.0, animations: {
backgroundImageView.frame = CGRect(x: -700, y: 0, width: backgroundImageView.image!.size.width, height:backgroundImageView.image!.size.height)
})
希望这个例子对您有所帮助:)
尝试使用 CGRect,并给它正 y 值。这样,您将在动画结束时获得这个矩形位置。当小键盘关闭时,将其移回 y 值 0。
原来是因为自动布局约束我不得不上边距,所以需要更改该约束的常量和行:
self.view.frame = CGRectOffset(self.view.frame, 0, TFMovement)
根本不需要。
我试图在键盘出现时提升视图,但它只是反弹回来。如果我给 CGRectOffset
Y 一个正值(向下移动视图),那么它会保持不变。
@IBOutlet weak var PPI: MainTextFields!
var KbHeight: CGFloat!
//----------------------------------------------
func animateTextField(up: Bool) {
let TFMovement = (up ? -KbHeight : KbHeight)
UIView.animateWithDuration(0.7, animations: {
self.view.frame = CGRectOffset(self.view.frame, 0, TFMovement)
})
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
if let keyboardSize = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
KbHeight = keyboardSize.height
self.animateTextField(true)
}
}
}
func keyboardWillHide(notification: NSNotification) {
self.animateTextField(false)
}
//----------------------------------------------
// Touch Outside Keypad to leave it
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
override func viewWillAppear(animated:Bool) {
super.viewWillAppear(animated)
// For TextField Movement
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
// For TextField Movement
NSNotificationCenter.defaultCenter().removeObserver(self)
}
override func viewDidLoad() {
super.viewDidLoad()
PPI.delegate = self
}
我在 keyboardWillShow
函数中的 CGRectOffset
做错了吗?我是 Swift 的新手,是从教程中学到的,但似乎工作不正常。
我想在 viewController 加载时制作从左到右的背景移动动画。这是我在我的应用程序中所做的:
var backgroundImageView: UIImageView = UIImageView(image:
UIImage(named: "background.png")!)
self.view.addSubview(backgroundImageView)
self.view.sendSubviewToBack(backgroundImageView)
UIView.animateWithDuration(20.0, animations: {
backgroundImageView.frame = CGRect(x: -700, y: 0, width: backgroundImageView.image!.size.width, height:backgroundImageView.image!.size.height)
})
希望这个例子对您有所帮助:)
尝试使用 CGRect,并给它正 y 值。这样,您将在动画结束时获得这个矩形位置。当小键盘关闭时,将其移回 y 值 0。
原来是因为自动布局约束我不得不上边距,所以需要更改该约束的常量和行:
self.view.frame = CGRectOffset(self.view.frame, 0, TFMovement)
根本不需要。