Skip to content

A light way to convert objc target-action style to closure

License

Notifications You must be signed in to change notification settings

mithyer/Selector-Closure

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Moved to https://github.com/mithyer/Ralyo/tree/master/src/Extension/Selector-Closure

Selector-Closure

Swift32 Swift4 Platform

Selector-Closure is a light way to convert objc target-action selector to closure style

Version 1.x.x

Type of Object calling "sce" will be auto recognized in closure

UIControl

use ".sce" to make closure callback

let btn = UIButton()
let field = UITextField()

// for specific event 
_ = btn.sce.add(.touchUpInside) { sender in
  // ... sender is UIButton
}

// for default event(UIButton, UISwitch, UISlider, UITextField has default events)
let invoker = field.sce.add { sender in
  // ... sender is UITextField
}

// now you can remove action with invoker returned
field.sce.remove(invoker)

// remove all for events
field.sce.removeAll(.touchUpInside) { sender in
  // ...
}

// test didAdd events
let res: Bool = field.sce.didAdd(.touchUpInside)

UIBarButtonItem

use ".SCE" to initialize

let item = UIBarButtonItem.SCE.initialize(title: "test", style: .plain, { item in
  // ...
})

// ...

UIGestureRecognizer

use ".SCE" to initialize

// init a recognizer, 
let tgr = UITapGestureRecognizer.SCE.initialize { tgr in
  // tgr is UITapGestureRecognizer
}

let sgr = UISwipeGestureRecognizer.SCE.initialize { sgr in
  // sgr is UISwipeGestureRecognizer
}

UIView

use ".sce" to make closure callback

// fast make tap action
let view = UIView()
let tapGestureRecognizer = view.sce.whenTapped { tgr in
  // tgr === tapGestureRecognizer
}

Convert your own

// you have an instance named obj
let invoker = Invoker(obj) { obj in
  // ...
}
let act = invoker.action // convert done, now pass invoker to target, act to action