适用于 iOS 应用程序的 Swift UIKit 键盘管理器

打字员

迅捷版 平台 兼容可可豆荚 迦太基兼容 支持Acgio 唽

Typist 是一个小型的嵌入式 Swift UIKit 键盘管理器,适用于 iOS 应用程序。它可以帮助您管理键盘的屏幕状态和行为,而无需通知中心和Objective-C。


用法

声明应该在什么事件上发生什么,并侦听键盘事件。就是这样。start()

let keyboard = Typist.shared // use `Typist()` whenever you can, see note on singleton usage below

func configureKeyboard() {

    keyboard
        .on(event: .didShow) { (options) in
            print("New Keyboard Frame is \(options.endFrame).")
        }
        .on(event: .didHide) { (options) in
            print("It took \(options.animationDuration) seconds to animate keyboard out.")
        }
        .start()

}

您必须调用才能触发回调。调用实例将阻止回调触发,但回调本身不会被关闭,因此您可以通过再次调用来恢复事件回调。start()stop()start()

要删除所有事件回调,请调用 。clear()

交互性和inputAccessoryView

在实例中使用 Typist 时,可以交互方式关闭键盘。UIScrollView

let keyboard = Typist()

func configureKeyboard() {

    keyboard
        .toolbar(scrollView: tableView) // Enables interactive dismissal
        .on(event: .willChangeFrame) { (options) in
            // You are responsible animating inputAccessoryView
        }
        .on(event: .willHide)  { (options) in
            // Triggered when keyboard is dismissed non-interactively.
        }
        .start()

}

.on(event: .willChangeFrame, do: {...}) will update as frequently as keyboard frame changes due to UIScrollView scrolling. It is good practice to implement portion as well since keyboard might be dismissed non-interactively, for example, using ..willHideresignFirstResponder()

Example from above is implemented in demo app.

On Singleton Usage

Usage of singleton, considered to be OK for convenient access to instance. However, it is strongly recommended to instantiate dedicated for each usage (in , most likely). Do not use singleton when two or more objects using are presented on screen simultaneously, as it will cause one of the controllers to fail receiving keyboard events.sharedTypist()UIViewControllerTypist.shared

Event Callback Options

Every event callback has a parameter of type. It is an inert/immutable struct which carries all data that keyboard has at the event of happening:Typist.KeyboardOptions

  • belongsToCurrentApp — that identifies whether the keyboard belongs to the current app. With multitasking on iPad, all visible apps are notified when the keyboard appears and disappears. The value is for the app that caused the keyboard to appear and for any other apps.Booltruefalse
  • startFrame — that identifies the start frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the view’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to view coordinates (using the method) before using it.CGRectconvert(CGRect, from: UIView?)
  • endFrame — that identifies the end frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the view’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to view coordinates (using the method) before using it.CGRectconvert(CGRect, from: UIView?)
  • animationCurve — constant that defines how the keyboard will be animated onto or off the screen.UIView.AnimationCurve
  • animationDuration — that identifies the duration of the animation in seconds.Double
  • animationOptions — helper property that maps the to its respective value. Usefull when performming view animations using .UIView.AnimationOptionsanimationCurveUIView.AnimationOptionsUIView.animate(...

Events

Following keyboard events are supported:

  • willShow
  • didShow
  • willHide
  • didHide
  • willChangeFrame
  • didChangeFrame – e.g. when keyboard is dynamically dismissed from scroll view interaction.

If you declare two closures on same event, only latter will be executed.


Installation

CocoaPods

You can use CocoaPods to install by adding it to your :TypistPodfile

platform :ios, '8.0'
use_frameworks!
pod 'Typist'

Import wherever you plan to listen to keyboard events. Usually in your subclasses.TypistUIViewController

import UIKit
import Typist

Carthage

Create a that lists the framework and run . Follow the instructions to add to an iOS project.Cartfilecarthage update$(SRCROOT)/Carthage/Build/iOS/Typist.framework

github "totocaster/Typist"

Accio

Initialize your project with Accio using the command.init

Add the following to your Package.swift:

.package(url: "https://github.com/totocaster/Typist.git", .upToNextMajor(from: "1.4.2")),

Next, add to your App targets dependencies like so:Typist

.target(
    name: "App",
    dependencies: [
        "Typist",
    ]
),

Then run .accio update

Manually

Download and drop in your project.Typist.swift


My thanks to Jake Marsh for featuring Typist on Little Bites of Cocoa #282: Taming the Keyboard with Typist

⌨️
. It made my day.


License

Typist is released under the MIT license. See for details.LICENSE

GitHub

https://github.com/totocaster/Typist