58

GitHub - onmyway133/EasyClosure: ? Unified communication patterns with easy clos...

 4 years ago
source link: https://github.com/onmyway133/EasyClosure
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

EasyClosure

Version Carthage Compatible License Platform Swift

Table of contents

Story

There are many Communication patterns

68747470733a2f2f7777772e6f626a632e696f2f696d616765732f69737375652d372f6e6f74696669636174696f6e2d666c6f772d63686172742d64616534636531322e706e67

Sometimes, you just want a unified and quick way to do it. Just call on on any NSObject subclasses and handle your events the quickest way

Features

  • Shortcut to handle actions and events
  • Easy to extend
  • Correct method suggestion based on generic protocol constraint
  • Support iOS, macOS

Example

demo.gif

We can make a fun demo of good, cheap, fast with UISwitch

func allOn() -> Bool {
  return [good, cheap, fast].filter({ $0.isOn }).count == 3
}

good.on.valueChange { _ in
  if allOn() {
    fast.setOn(false, animated: true)
  }
}

cheap.on.valueChange { _ in
  if allOn() {
    good.setOn(false, animated: true)
  }
}

fast.on.valueChange { _ in
  if allOn() {
    cheap.setOn(false, animated: true)
  }
}

Target-Action and Delegate

UIButton

button.on.tap {
  print("button has been tapped")
}

UISlider

slider.on.valueChange { value in
  print("slider has changed value")
}

UITextField

textField.on.textChange { text in
  print("textField text has changed")
}

UITextView

textView.on.textChange { text in
  print("textView text has changed")
}

UISearchBar

searchBar.on.textChange { text in
  print("searchBar text has changed")
}

UIDatePicker

datePicker.on.pick { date in
  print("datePicker has changed date")
}

UIBarButtonItem

barButtonItem.on.tap {
  print("barButtonItem has been tapped")
}

UIGestureRecognizer

gestureRecognizer.on.occur {
  print("gesture just occured")
}

Extensible

Extend Container and specify Host to add more functionalities to your own types. For example

public extension Container where Host: UITableView {
  func cellTap(_ action: @escaping (UITableViewCell) -> Void)) {
    // Your code here here
  }
}

// usage
let tableView = UITableView()
tableView.on.cellTap { cell in
  
}

Installation

EasyClosure is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EasyClosure'

EasyClosure is also available through Carthage. To install just write into your Cartfile:

github "onmyway133/EasyClosure"

EasyClosure can also be installed manually. Just download and drop Sources folders in your project.

Author

Khoa Pham, [email protected]

Contributing

We would love you to contribute to EasyClosure, check the CONTRIBUTING file for more info.

License

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK