Skip to content

Commit

Permalink
Convert source compatible to Swift3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
takasek committed Sep 22, 2016
1 parent 6a74a9c commit f413603
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 46 deletions.
12 changes: 6 additions & 6 deletions ActionClosurable-Demo/AppDelegate.swift
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
19 changes: 9 additions & 10 deletions ActionClosurable-Demo/ViewController.swift
Expand Up @@ -12,9 +12,9 @@ import ActionClosurable
extension UIImage {
static func whiteImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(CGSize(width: 20, height: 20), false, 0)
let contextImg = UIGraphicsGetCurrentContext()
CGContextSetRGBFillColor(contextImg!,1,1,1,1)
CGContextFillRect(contextImg!, CGRect(x: 0, y: 0, width: 20, height: 20))
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(red: 1,green: 1,blue: 1,alpha: 1)
context?.fill(CGRect(x: 0, y: 0, width: 20, height: 20))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Expand All @@ -25,21 +25,20 @@ extension UIImage {
class ViewController: UIViewController {
var button: UIButton? = UIButton(frame: CGRect(x: 0,y: 70,width: 100,height: 100))
var gr: UITapGestureRecognizer? = UITapGestureRecognizer()
var timer: NSTimer?

override func viewDidLoad() {
super.viewDidLoad()

navigationItem.rightBarButtonItems = [
UIBarButtonItem(title: "title", style: .Plain, closure: { _ in
UIBarButtonItem(title: "title", style: .plain, closure: { _ in
print("barButtonItem title")
}),
UIBarButtonItem(image: UIImage.whiteImage(), style: .Plain, closure: { _ in
UIBarButtonItem(image: UIImage.whiteImage(), style: .plain, closure: { _ in
print("barButtonItem image")
}),
]
button?.setTitle("button", forState: .Normal)
button?.backgroundColor = UIColor.redColor()
button?.setTitle("button", for: UIControlState())
button?.backgroundColor = UIColor.red
self.view.addSubview(button!)

button?.onTap { [weak self] in
Expand All @@ -48,7 +47,7 @@ class ViewController: UIViewController {
self!.button = nil
}

button?.on(.TouchDown) {
button?.on(.touchDown) {
print($0)
}

Expand All @@ -61,7 +60,7 @@ class ViewController: UIViewController {

let label = UILabel(frame: CGRect(x: 0, y: 300, width: 200, height: 20))
label.text = "hogehoge"
label.userInteractionEnabled = true
label.isUserInteractionEnabled = true
self.view.addSubview(label)
label.addGestureRecognizer(UIPanGestureRecognizer { gr in
print("UIPanGestureRecognizer fire")
Expand Down
14 changes: 8 additions & 6 deletions ActionClosurable.xcodeproj/project.pbxproj
Expand Up @@ -232,9 +232,11 @@
};
B6554A291CC24F3D0060DC4B = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
};
B6554A4A1CC25F320060DC4B = {
CreatedOnToolsVersion = 7.3;
LastSwiftMigration = 0800;
};
};
};
Expand Down Expand Up @@ -448,7 +450,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurableTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -459,7 +461,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurableTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand All @@ -478,7 +480,7 @@
PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurable;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -499,7 +501,7 @@
PRODUCT_BUNDLE_IDENTIFIER = takasek.ActionClosurable;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -514,7 +516,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "takasek.ActionClosurable-Demo";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -527,7 +529,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "takasek.ActionClosurable-Demo";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
22 changes: 11 additions & 11 deletions ActionClosurable/ActionClosurable.swift
Expand Up @@ -9,19 +9,19 @@
import Foundation

public class Actor<T> {
@objc func act(sender: AnyObject) { closure(sender as! T) }
private let closure: T -> Void
init(_ closure: T -> Void) {
@objc func act(_ sender: AnyObject) { closure(sender as! T) }
fileprivate let closure: (T) -> Void
init(acts closure: @escaping (T) -> Void) {
self.closure = closure
}
}

private class GreenRoom {
private var actors: [Any] = []
fileprivate var actors: [Any] = []
}
private var GreenRoomKey: UInt32 = 893

func register<T>(actor: Actor<T>, to object: AnyObject) {
private func _register<T>(_ actor: Actor<T>, to object: AnyObject) {
let room = objc_getAssociatedObject(object, &GreenRoomKey) as? GreenRoom ?? {
let room = GreenRoom()
objc_setAssociatedObject(object, &GreenRoomKey, room, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
Expand All @@ -32,15 +32,15 @@ func register<T>(actor: Actor<T>, to object: AnyObject) {

public protocol ActionClosurable {}
public extension ActionClosurable where Self: AnyObject {
public func registerClosure(closure: Self -> Void, @noescape configure: (Actor<Self>, Selector) -> Void) {
let actor = Actor(closure)
public func register(closure: @escaping (Self) -> Void, configure: (Actor<Self>, Selector) -> Void) {
let actor = Actor(acts: closure)
configure(actor, #selector(Actor<AnyObject>.act(_:)))
register(actor, to: self)
_register(actor, to: self)
}
public static func registerClosure(closure: Self -> Void, @noescape configure: (Actor<Self>, Selector) -> Self) -> Self {
let actor = Actor(closure)
public static func register(closure: @escaping (Self) -> Void, configure: (Actor<Self>, Selector) -> Self) -> Self {
let actor = Actor(acts: closure)
let instance = configure(actor, #selector(Actor<AnyObject>.act(_:)))
register(actor, to: instance)
_register(actor, to: instance)
return instance
}
}
Expand Down
26 changes: 13 additions & 13 deletions ActionClosurable/Extensions.swift
Expand Up @@ -9,49 +9,49 @@
import UIKit

extension ActionClosurable where Self: UIControl {
public func on(controlEvents: UIControlEvents, closure: Self -> Void) {
registerClosure(closure) {
self.addTarget($0, action: $1, forControlEvents: controlEvents)
public func on(_ controlEvents: UIControlEvents, closure: @escaping (Self) -> Void) {
register(closure: closure) {
self.addTarget($0, action: $1, for: controlEvents)
}
}
}

extension ActionClosurable where Self: UIButton {
public func onTap(closure: Self -> Void) {
registerClosure(closure) {
self.addTarget($0, action: $1, forControlEvents: .TouchUpInside)
public func onTap(_ closure: @escaping (Self) -> Void) {
register(closure: closure) {
self.addTarget($0, action: $1, for: .touchUpInside)
}
}
}


extension ActionClosurable where Self: UIGestureRecognizer {
public func onGesture(closure: Self -> Void) {
registerClosure(closure) {
public func onGesture(_ closure: @escaping (Self) -> Void) {
register(closure: closure) {
self.addTarget($0, action: $1)
}
}
public init(closure: Self -> Void) {
public init(closure: @escaping (Self) -> Void) {
self.init()
onGesture(closure)
}
}

extension ActionClosurable where Self: UIBarButtonItem {
public init(title: String, style: UIBarButtonItemStyle, closure: Self -> Void) {
public init(title: String, style: UIBarButtonItemStyle, closure: @escaping (Self) -> Void) {
self.init()
self.title = title
self.style = style
self.onTap(closure)
}
public init(image: UIImage?, style: UIBarButtonItemStyle, closure: Self -> Void) {
public init(image: UIImage?, style: UIBarButtonItemStyle, closure: @escaping (Self) -> Void) {
self.init()
self.image = image
self.style = style
self.onTap(closure)
}
public func onTap(closure: Self -> Void) {
registerClosure(closure) {
public func onTap(_ closure: @escaping (Self) -> Void) {
register(closure: closure) {
self.target = $0
self.action = $1
}
Expand Down

0 comments on commit f413603

Please sign in to comment.