Giter Club home page Giter Club logo

keyboardspy's Introduction

KeyboardSpy

Version Language: Swift Platform License

Written in Swift 3

KeyboardSpy is a super lightweight and easy to use wrapper that makes observing keyboard notifications in iOS a breeze.

Requirements

  • iOS 8.0+
  • xCode 8.1+

Installation

CocoaPods

To integrate KeyboardSpy into your xCode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'KeyboardSpy'

Then, run the following command:

$ pod install

Usage

KeyboardSpy uses a protocol based approach to observe keyboard notifications:

public protocol KeyboardSpyAgent {
    var keyboardEventsToSpyOn: [KeyboardSpyEvent] { get set }
    func keyboardSpyEventProcessed(event:KeyboardSpyEvent, keyboardInfo: KeyboardSpyInfo)
}

To add a spy, simply:

KeyboardSpy.spy(on: self)

To remove a spy, simply:

KeyboardSpy.unspy(on: self)

There are six different events you can spy on:

public enum KeyboardSpyEvent {
    case willShow
    case didShow
    case willHide
    case didHide
    case willChangeFrame
    case didChangeFrame
}

You will get the following object for each event you spy on:

public class KeyboardSpyInfo: NSObject {
    public private(set) var beginFrame: CGRect!
    public private(set) var endFrame: CGRect!
    public private(set) var animationCurve: UIViewAnimationCurve!
    public private(set) var animationDuration: Double!
    public private(set) var isLocal: Bool!
    public var keyboardHeight: CGFloat
}

Example:

import KeyboardSpy

class KeyboardSpyViewController: UIViewController,  KeyboardSpyAgent {

    internal var keyboardEventsToSpyOn: [KeyboardSpyEvent] = [.willShow, .willHide]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        KeyboardSpy.spy(on: keyboardSpyView) // This can be placed anywhere
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        KeyboardSpy.unspy(on: keyboardSpyView) // This can be placed anywhere
    }
    
    func keyboardSpyEventProcessed(event: KeyboardSpyEvent, keyboardInfo: KeyboardSpyInfo) {
        if event == .willShow {
        	// Do something like moving a view above the keyboard
        } else if event == .willHide {
        	// Do something like moving a view back to its original position
        }   
    }
    
}

Author

Dalton Hinterscher, [email protected]

License

KeyboardSpy is available under the MIT license. See the LICENSE file for more info.

keyboardspy's People

Contributors

daltron avatar

Watchers

 avatar

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.