pomu0325 / ReactiveUI

A lightweight replacement for target action with closures, modified from Scream.swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reactive UI

A lightweight replacement for target action with closures, modified from Scream.swift.

Why?

In UIKit, Target-Action has been the default way to handle control events until the arrival of iOS 8 when UIAlertController introduces closure handler in UIAlertAction.

Closure handlers, in many cases, are more concise and readable than Target-Action. ReactiveUI follows this approach, wrapping existing Target-Action APIs

// UIControl
func addTarget(_ target: AnyObject?, action action: Selector, forControlEvents controlEvents: UIControlEvents)

// UIGestureRecognizer
init(target target: AnyObject, action action: Selector)

// ...

in closures

// UIControl
func addAction(action: UIControl -> (), forControlEvents events: UIControlEvents)

// UIGestureRecognizer
init(action: UIGestureRecognizer -> ())

// ...

With ReactiveUI, control events handling is much simpler:

var button = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
button.setTitle("Title", forState: .Normal)
button.addAction({_ in println("TouchDown")}, forControlEvents: .TouchDown)
button.addAction({_ in println("TouchUpInside")}, forControlEvents: .TouchUpInside)
button.addAction({_ in println("TouchDragOutside")}, forControlEvents: .TouchDragOutside)

CocoaPods

You can install ReactiveUI through CocoaPods adding the following to your Podfile:

pod 'ReactiveUI'

CocoaPods' support for swift is still pre-released, and requires your iOS deployment target to be 8.0 or later:

[sudo] gem install cocoapods --pre

Usage

Checkout the demo app for an example.

ReactiveUI currently supports the following classes:

###UIControl

init(action: UIControl -> (), forControlEvents events: UIControlEvents)
init(forControlEvents events: UIControlEvents, action: UIControl -> ())
func addAction(action: UIControl -> (), forControlEvents events: UIControlEvents)
// can be called with a trailing closure
func forControlEvents(events: UIControlEvents, addAction action: UIControl -> ())
func removeAction(forControlEvents events: UIControlEvents)
func actionForControlEvent(events: UIControlEvents) -> (UIControl -> ())?
var actions: [UIControl -> ()]

###UIBarButtonItem

init(barButtonSystemItem systemItem: UIBarButtonSystemItem, action: UIBarButtonItem -> ())
init(title: String?, style: UIBarButtonItemStyle, action: UIBarButtonItem -> ())
init(image: UIImage?, style: UIBarButtonItemStyle, action: UIBarButtonItem -> ())
init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItemStyle, action: UIBarButtonItem -> ())
func addAction(action: UIBarButtonItem -> ())
func removeAction()

###UIGestureRecognizer

init(action: UIGestureRecognizer -> ())
func addAction(action: UIGestureRecognizer -> ())
func removeAction()

###NSTimer

class func scheduledTimerWithTimeInterval(seconds: NSTimeInterval, action: NSTimer -> (), repeats: Bool) -> NSTimer

License

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

About

A lightweight replacement for target action with closures, modified from Scream.swift.

License:MIT License


Languages

Language:Swift 95.3%Language:Ruby 2.8%Language:Objective-C 1.9%