EasyClosure

? Unified communication patterns with easy closure in Swift

Github星跟踪图

EasyClosure

Version
Carthage Compatible
License
Platform
Swift

Table of contents

Story

There are many Communication patterns

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

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, onmyway133@gmail.com

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.

主要指标

概览
名称与所有者onmyway133/EasyClosure
主编程语言Swift
编程语言Objective-C (语言数: 3)
平台
许可证Other
所有者活动
创建于2019-05-18 19:43:49
推送于2020-06-11 06:24:26
最后一次提交2020-06-11 08:24:25
发布数6
最新版本名称1.1.3 (发布于 )
第一版名称1.0.0 (发布于 )
用户参与
星数201
关注者数6
派生数12
提交数32
已启用问题?
问题数1
打开的问题数0
拉请求数1
打开的拉请求数0
关闭的拉请求数0
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?