Giter Club home page Giter Club logo

eventbox's Introduction

EventBox

Build Status Carthage compatible

Provides an interface for use the addObserverForName safely and easily.

Features

  • Comprehensive Unit Test Coverage
  • Carthage support
  • Thread-safe

Usage

Minimum

EventBox.onMainThread(target, name: "reload") { _ in
    // UI
}

EventBox.onBackgroundThread(target, name:"reload") { _ in
    // API Access
}

EventBox.post("reload")

EventBox.off(target) // Remove all the observers of the target

EventBox.off(target, "reload") // Remove observers of the same name of the target

Sender

EventBox.onMainThread(target, name:"getStatus") { n in
    // API Access
    let status = n.object as TwitterStatus // sender
}

let status = TwitterStatus()
EventBox.post("getStatus", sender: status)

Handling the keyboard notifications

// MARK: - View Life Cycle

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    EventBox.onMainThread(self, name: UIKeyboardWillShowNotification, handler: { n in
        self.keyboardWillChangeFrame(n, showsKeyboard: true) })

    EventBox.onMainThread(self, name: UIKeyboardWillHideNotification, handler: { n in
        self.keyboardWillChangeFrame(n, showsKeyboard: false) })
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)

    EventBox.off(self)
}

// MARK: - Keyboard Event Notifications

func keyboardWillChangeFrame(notification: NSNotification, showsKeyboard: Bool) {
    let userInfo = notification.userInfo!
    let animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as NSNumber).doubleValue
    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as NSValue).CGRectValue()

    if showsKeyboard {
        let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
        if (orientation.isLandscape) {
            containerViewButtomConstraint.constant = keyboardScreenEndFrame.size.width
        } else {
            containerViewButtomConstraint.constant = keyboardScreenEndFrame.size.height
        }
    } else {
        containerViewButtomConstraint.constant = 0
    }

    self.view.setNeedsUpdateConstraints()

    UIView.animateWithDuration(animationDuration, delay: 0, options: .BeginFromCurrentState, animations: {
        self.containerView.alpha = showsKeyboard ? 1 : 0
        self.view.layoutIfNeeded()
    }, completion: { finished in
        if !showsKeyboard {
            self.view.hidden = true
        }
    })
}

Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

Carthage

Add the following line to your Cartfile

github "s-aska/EventBox"

CocoaPods

Add the following line to your Podfile

use_frameworks!
pod 'EventBox'

License

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

eventbox's People

Contributors

s-aska avatar

Watchers

 avatar  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.