[iOS] Typist:简单、易用的 Keyboard 事件监听管理小类库

Typist Typist is small, drop-in Swift UIKit keyboard manager for iOS apps It helps you manage keyboard& 039;s screen

Typist

Typist is small, drop-in Swift UIKit keyboard manager for iOS apps. It helps you manage keyboard's screen presence and behavior without notification center and Objective-C.

Installation

CocoaPods

You can use CocoaPodsto install Typistby adding it to your Podfile:

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

Import Typistwherever you plan to listen to keyboard events. Usually in your UIViewControllersubclasses.

import UIKitimport Typist

Manually

Download and drop Typist.swiftin your project.

Usage

Delcare what should happen on what event and start()listening to keyboard events. That's it.

let keyboard = Typist.sharedfunc 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()}

Usage of both—singleton, or your own instance of Typist—is considered to be OK depending on what do you want to accomplish.

You mustcall start()for callbacks to be triggered. Calling stop()on instance will stop callbacks from trigering, but callbacks themselfs won't be dismissed, thus you can resume event callbacks by calling start()again.

To removed all event callbacks, call clear(). Passing nilinstead of closure also removes event callback.

Event Callback Options

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

  • belongsToCurrentApp Boolthat 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 of this key is truefor the app that caused the keyboard to appear and falsefor any other apps.
  • startFrame CGRectthat identifies the start frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow:method) or to view coordinates (using the convertRect:fromView:method) before using it.
  • endFrame CGRectthat identifies the end frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow:method) or to view coordinates (using the convertRect:fromView:method) before using it.
  • animationCurve UIViewAnimationCurveconstant that defines how the keyboard will be animated onto or off the screen.
  • animationDuration Doublethat identifies the duration of the animation in seconds.

Events

Following keyboard events are supported:

  • willShow
  • didShow
  • willHide
  • didHide
  • willChangeFrame
  • didChangeFrame

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

Licence

Typist is released under the MIT license. See LICENSEfor details.

未登录用户
全部评论0
到底啦