diff --git a/ACKReactiveExtensions/UIKit/UIKitExtensions.swift b/ACKReactiveExtensions/UIKit/UIKitExtensions.swift index 74ef538..7ea0217 100755 --- a/ACKReactiveExtensions/UIKit/UIKitExtensions.swift +++ b/ACKReactiveExtensions/UIKit/UIKitExtensions.swift @@ -28,6 +28,7 @@ private struct AssociationKey { static var on: UInt8 = 15 static var animating: UInt8 = 16 static var textElseHidden: UInt8 = 16 + static var imageElseHidden: UInt8 = 17 } extension UIView { @@ -55,12 +56,6 @@ extension CALayer { } } -extension UIImageView { - public var rac_image: MutableProperty { - return lazyMutablePropertyUiKit(self, &AssociationKey.image, { [unowned self] in self.image = $0 }, { [unowned self] in self.image }) - } -} - extension UILabel { public var rac_textColor: MutableProperty { return lazyMutablePropertyUiKit(self, &AssociationKey.textColor, { [unowned self] in self.textColor = $0 }, { [unowned self] in self.textColor }) @@ -258,3 +253,35 @@ public extension TextContainingView where Self: UIView { return rac_textElseHideView(self) } } + +public protocol ImageContainer: class { + var image: UIImage? { get set } +} + +extension UIImageView: ImageContainer { } + +public extension ImageContainer { + public var rac_image: MutableProperty { + return lazyMutablePropertyUiKit(self, &AssociationKey.image, { [unowned self] in self.image = $0 }, { [unowned self] in self.image }) + } +} + +public extension ImageContainer { + // only call this once per view + public func rac_imageElseHideView(view: UIView) -> MutableProperty { + return lazyMutablePropertyUiKit(self, &AssociationKey.textElseHidden, { [unowned self, weak view] in + self.image = $0 + if let _ = $0 { + view?.hidden = false + } else { + view?.hidden = true + } + }, { [unowned self] in self.image }) + } +} + +public extension ImageContainer where Self: UIView { + public var rac_imageElseHidden: MutableProperty { + return rac_imageElseHideView(self) + } +}