来自 Scream.swift 的轻量级 Target-Action 替代品,使用闭包修改。
在 UIKit 中,Target-Action 一直是以控制事件处理的标准方式,直到 iOS 8 的到来,当时在 UIAlertController 中通过 UIAlertAction 的闭包处理程序。
闭包处理在很多情况下比 Target-Action 更简洁、更易于阅读。ReactiveUI 采用这种方式,通过闭包包装现有的 Target-Action API
// UIControl
func addTarget(_ target: AnyObject?, action action: Selector, forControlEvents controlEvents: UIControlEvents)
// UIGestureRecognizer
init(target target: AnyObject, action action: Selector)
// ...
。
// UIControl
func addAction(action: UIControl -> (), forControlEvents events: UIControlEvents)
// UIGestureRecognizer
init(action: UIGestureRecognizer -> ())
// ...
在 ReactiveUI 中,控制器事件处理变得更加简单
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)
查看 示例应用 以获取示例。
ReactiveUI 目前支持以下类:
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 -> ()]
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()
init(action: UIGestureRecognizer -> ())
func addAction(action: UIGestureRecognizer -> ())
func removeAction()
class func scheduledTimerWithTimeInterval(seconds: NSTimeInterval, action: NSTimer -> (), repeats: Bool) -> NSTimer
ReactiveUI 遵循 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。