EventBox 1.0.0

EventBox 1.0.0

测试已测试
Lang语言 SwiftSwift
许可 MIT
发布最新发布2015年9月
SPM支持SPM

Shinichiro Aska 维护。



EventBox 1.0.0

EventBox Build Status

提供一个用于安全、方便地使用addObserverForName的接口。

特点

  • [x] 完整的单元测试覆盖
  • [x] 支持Carthage
  • [x] 线程安全

要求

  • iOS 8+
  • Xcode 6.3

安装

创建一个Cartfile,列出您项目需要使用的框架。

echo 'github "s-aska/EventBox"' >> Cartfile

运行carthage update

carthage update

在“General”设置标签页中,在“Embedded Binaries”部分,将您想要使用的每个框架从磁盘上的Carthage.build文件夹拖放到您的应用目标。

用法

最小要求

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

发送者

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

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

处理键盘通知

// 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
        }
    })
}

许可证

EventBox在MIT许可证下发布。有关详细信息,请参阅LICENSE。