UIViewController + NSNotification 基于扩展协议的@objc 问题

问题描述

我使用这个 example 来避免出现键盘框架。现在我想让它基于协议,以避免使用此功能扩展所有视图控制器。现在我收到两个看起来相互冲突的错误一个是想添加@objc,另一个删除它):

enter image description here

我的代码

import UIKit

protocol KeyboardFrameIgnorable {
    func listenForKeyboardFrameChange()
    func stopListenKeyboardFrameChange()
}

extension KeyboardFrameIgnorable where Self: UIViewController {
    func listenForKeyboardFrameChange()
    {
        NotificationCenter.default
            .addobserver(self,selector: #selector(onKeyboardFrameWillChangeNotificationReceived(_:)),name: UIResponder.keyboardWillChangeFrameNotification,object: nil)
    }

    func stopListenKeyboardFrameChange()
    {
        NotificationCenter.default
            .removeObserver(self,object: nil)
    }

    @objc
    private func onKeyboardFrameWillChangeNotificationReceived(_ notification: Notification)
    {
        guard
            let userInfo = notification.userInfo,let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
        else {
            return
        }

        let keyboardFrameInView = view.convert(keyboardFrame,from: nil)
        let safeAreaFrame = view.safeAreaLayoutGuide.layoutFrame.insetBy(dx: 0,dy: -additionalSafeAreaInsets.bottom)
        let intersection = safeAreaFrame.intersection(keyboardFrameInView)

        let keyboardAnimationDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey]
        let animationDuration: TimeInterval = (keyboardAnimationDuration as? NSNumber)?.doubleValue ?? 0
        let animationCurveRawNSN = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.Animationoptions.curveEaseInOut.rawValue
        let animationCurve = UIView.Animationoptions(rawValue: animationCurveRaw)

        UIView.animate(withDuration: animationDuration,delay: 0,options: animationCurve,animations: {
            self.additionalSafeAreaInsets.bottom = intersection.height + 10
            self.view.layoutIfNeeded()
        },completion: nil)
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...