快速动画在 UIKeyboardWillShowNotification 事件中不起作用

标签 swift animation

我不知道为什么动画在 UIKeyboardDidShowNotification 事件中工作正常但在 UIKeyboardWillShowNotification 事件中不工作。

代码如下所示:

注释代码中的anim效果不佳,颜色会改变但duration = 4不对

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name:UIKeyboardDidShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidHide:"), name:UIKeyboardDidHideNotification, object: nil)
    }
    deinit {NSNotificationCenter.defaultCenter().removeObserver(self)}
    func keyboardDidShow(notification: NSNotification) {
        showAnima1()
    }
    func keyboardDidHide(notification: NSNotification) {
        showAnima2()
    }
    func keyboardWillShow(notification: NSNotification) {
//        showAnima1() // animate not work here
    }
    func keyboardWillHide(notification: NSNotification) {
//        showAnima2() // animate not work here
    }
    func showAnima1() {
        UIView.animateWithDuration(4, delay: 0, options: UIViewAnimationOptions(rawValue: 7), animations: { () -> Void in
            self.view.backgroundColor = UIColor.greenColor()
            }) { (finish) -> Void in
                print("animate state = \(finish)")
        }
    }
    func showAnima2() {
        UIView.animateWithDuration(4, delay: 0, options: UIViewAnimationOptions(rawValue: 7), animations: { () -> Void in
            self.view.backgroundColor = UIColor.whiteColor()
            }) { (finish) -> Void in
                print("animate state = \(finish)")
        }
    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {self.view.endEditing(false)}
}

对不起,我在源代码中看到评论说:

// Each notification includes a nil object and a userInfo dictionary containing the 
// begining and ending keyboard frame in screen coordinates. Use the various UIView and 
// UIWindow convertRect facilities to get the frame in the desired coordinate system. 
// Animation key/value pairs are only available for the "will" family of notification.

最后一条评论可能意味着“做了”系列通知???

最佳答案

我遇到了同样的问题。 看起来必须从主线程调用 animate 函数。将动画包装在 DispatchQueue.main.async 中对我有用:

@objc func keyboardWillHide(notification: NSNotification) {
    DispatchQueue.main.async {
        UIView.animate(withDuration: 2.0) {
            self.view.frame.origin.y = 0
        }
    }
}

关于快速动画在 UIKeyboardWillShowNotification 事件中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32732337/

相关文章:

ios - SCNCamera 移动相机的枢轴

swift - 如何将字符串拆分为 Int :String Dictionary

swift - 如何从 SKScene 中删除实体

ios - Swift,改变图像内存占用

swift - Swift 中的 Firebase 函数调用返回值

jQuery: 子元素消失 $ ('.parent_class' ) IE 中的不透明效果

javascript - 需要 setInterval 来自行停止

javascript - setInterval 动画元素的替代方法 ('diamond game' )

animation - 如何使用 onmouseover 事件控制 aframe 中的 .gtlf2 网格动画?

java - 图像正在播放动画并且图像是可点击的