Giter Site home page Giter Site logo

typist's Introduction

Typist

Swift Version Platform CocoaPods Compatible Carthage compatible Accio supported Twitter

Typist is a 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.


Usage

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

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()

}

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

To remove all event callbacks, call clear().

Interactivity and inputAccessoryView

You can dismiss keyboard interactively when using Typist with UIScrollView instances.

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 .willHide portion as well since keyboard might be dismissed non-interactively, for example, using resignFirstResponder().

Example from above is implemented in demo app.

On Singleton Usage

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

Event Callback Options

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

  • belongsToCurrentAppBool 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 true for the app that caused the keyboard to appear and false for any other apps.
  • startFrameCGRect 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 convert(CGRect, from: UIView?) method) before using it.
  • endFrameCGRect 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 convert(CGRect, from: UIView?) method) before using it.
  • animationCurveUIView.AnimationCurve constant that defines how the keyboard will be animated onto or off the screen.
  • animationDurationDouble that identifies the duration of the animation in seconds.
  • animationOptionsUIView.AnimationOptions helper property that maps the animationCurve to its respective UIView.AnimationOptions value. Usefull when performming view animations using UIView.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 Typist by adding it to your Podfile:

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

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

import UIKit
import Typist

Carthage

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

github "totocaster/Typist"

Accio

Initialize your project with Accio using the init command.

Add the following to your Package.swift:

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

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

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

Then run accio update.

Manually

Download and drop Typist.swift in your project.


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 LICENSE for details.

typist's People

Contributors

colinhumber avatar duliodenis avatar jeanbernard avatar jeehut avatar lasha-ring avatar mman avatar mohpor avatar nikans avatar sirioz avatar tosinaf avatar totocaster avatar tylerian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typist's Issues

ios 12 changes

Seems like lot of constants have changed,
'UIApplicationDidBecomeActive' has been renamed to 'UIApplication.didBecomeActiveNotification'
'kCAFillModeBoth' has been renamed to 'CAMediaTimingFillMode.both'
'UIApplicationDidBecomeActive' has been renamed to 'UIApplication.didBecomeActiveNotification'

and a few more

Make it possible to use typist in multiple View controllers at the same time

I'm aware of the current limitations of using a singleton instance and start/stop methods to setup and remove listeners.

I'm opening this issue to start conversation about how to best address this limitation so that multiple typist instances can be setup at the same time for example from a full screen view controller on the iPad and another modally present page view controller that is occupying just part of the screen (page modal view controller)

Quickly thinking out loud I think it should be possible instead of using a singleton just instantiate and start typist in viewDidLoad and stop it in deinit...

Any thoughts on this?

Thanks
Martin

Installation Section

Hey, your library is really interesting.

The only problem I found was the README.md, which lacks an Installation Section
I created this iOS Open source Readme Template so you can take a look on how to easily create an Installation Section
If you want, I can help you to organize the lib.

What are your thoughts? 😄

How to use options.animationCurve?

I'm trying to setup a UIView animation to match the keyboard animation, including the animation curve. .animationCurve is available from Typist.KeyboardOptions, but I'm getting a type error. Any way around this?

UIView.animate(withDuration: options.animationDuration, delay: 0, options: options.animationCurve, animations: {
    self.view.layoutIfNeeded()
}, completion: nil)
Cannot convert value of type 'UIViewAnimationCurve' to expected argument type 'UIViewAnimationOptions'

The singleton pattern is encouraged, but doesn't support multiple callbacks

Since callbacks only stores one closure of each event type, multiple controllers that use Typist.shared will "fight" over who receives notifications.

class ControllerOne: UIViewController {
  override func viewDidLoad() {
    Typist.shared.on(event: .willShow) { _ in print("will show from one") }
  }
}

class ControllerTwo: UIViewController {
  override func viewDidLoad() {
    Typist.shared.on(event: .willShow) { _ in print("will show from two") }
  }
}

If both of these controllers are loaded, only the second controller will get the notifications.

Easy fix: remove the singleton, and have each controller retain its own Typist instance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.