Code Monkey home page Code Monkey logo

centipede's Introduction

Centipede License MIT CocoaPods CocoaPods Carthage compatible

一个 Swift 库,使用闭包实现 UIKit 等组件的 delegate 和 dataSource 方法

使用

Xcode 8, Swift 3.0, iOS 8+

所有方法名称以ce_开头

// UITableView
let kCellReuseIdentifier = "MYCELL"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: kCellReuseIdentifier)
tableView.ce_numberOfSections_in { (tableView) -> Int in
    return 3
}.ce_tableView_numberOfRowsInSection { (tableView, section) -> Int in
    return 5
}.ce_tableView_cellForRowAt { (tableView, indexPath) -> UITableViewCell in
    return tableView.dequeueReusableCell(withIdentifier: kCellReuseIdentifier, for: indexPath)
}.ce_tableView_willDisplay { (tableView, cell, indexPath) in
    cell.textLabel?.text = "\(indexPath.section) - \(indexPath.row)"
}.ce_scrollViewDidScroll { (scrollView) in
    print(scrollView.contentOffset)
}

// UIControl
button.ce_addControlEvents(.touchDown) { (control, touches) in
    print("TouchDown")
}.ce_addControlEvents(.touchUpInside) { (control, touches) in
    print("TouchUpInside")
}
button.ce_removeControlEvents(.touchDown)

textField.ce_addControlEvents([.editingChanged, .editingDidBegin]) { (control, touches) in
    print("TextChanged")
}

// UIBarButtonItem
let barButtonItem = UIBarButtonItem()
barButtonItem.action { (barButtonItem) in
    print("UIBarButtonItem action")
}

// UIGestureRecognizer
let gestureRecognizer = UIPanGestureRecognizer { (gestureRecognizer) in
    print(gestureRecognizer.state.rawValue)
}
self.view.addGestureRecognizer(gestureRecognizer)

源码使用

Centipede目录复制到您的项目中及可。

CocoaPods

platform :ios, '8.0'
use_frameworks!

pod 'Centipede'

Carthage

github "klaus01/Centipede"

注意

使用闭包需要注意循环引用问题,Swift 使用 weak 或 unowned 解决循环引用问题

原由

在实现 delegate 的各个方法时:

  • 方法遍布整个 ViewController,散乱。
  • 具体的实现与成员变量被分开了,阅读时需要分开查看。
  • 如果 ViewController 中实现多个 UITableViewDataSource 时,方法中需要判断组件来做出反应。如下:(这很丑)
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableView == leftTableView ? leftDatas.count : rightDatas.count
}

这些情况让代码不易阅读和维护。

希望:

  • 代码连续。组件的构造、样式设置和各 delegate 实现方法可写在一个位置。
  • 独立。有多个 UITableView 时,tableViewA 和tableViewB 的 delegate 方法实现是独立的,互不干扰。

实现列表

UIKit delegate and dataSource method

  • AVFoundation/AVAudioPlayer
  • AVFoundation/AVAudioRecorder
  • AVFoundation/AVSpeechSynthesizer
  • CoreBluetooth/CBCentralManager
  • CoreBluetooth/CBPeripheral
  • CoreBluetooth/CBPeripheralManager
  • CoreLocation/CLLocationManager
  • ExternalAccessory/EAAccessory
  • ExternalAccessory/EAWiFiUnconfiguredAccessoryBrowser
  • Foundation/NSKeyedArchiver
  • Foundation/NSKeyedUnarchiver
  • Foundation/NSMetadataQuery
  • Foundation/NSUserActivity
  • Foundation/XMLParser
  • GameKit/GKMatch
  • GLKit/GLKView
  • GLKit/GLKViewController
  • iAd/ADBannerView
  • iAd/ADInterstitialAd
  • MapKit/MKMapView
  • MediaPlayer/MPMediaPickerController
  • MediaPlayer/MPPlayableContentManager
  • MultipeerConnectivity/MCAdvertiserAssistant
  • MultipeerConnectivity/MCBrowserViewController
  • MultipeerConnectivity/MCNearbyServiceAdvertiser
  • MultipeerConnectivity/MCNearbyServiceBrowser
  • MultipeerConnectivity/MCSession
  • PassKit/PKAddPassesViewController
  • PassKit/PKPaymentAuthorizationViewController
  • PushKit/PKPushRegistry
  • QuartzCore/CAAnimation
  • QuartzCore/CALayer
  • QuickLook/QLPreviewController
  • SceneKit/SCNProgram
  • SpriteKit/SKScene
  • StoreKit/SKProductsRequest
  • StoreKit/SKRequest
  • StoreKit/SKStoreProductViewController
  • UIKit/NSLayoutManager
  • UIKit/NSTextStorage
  • UIKit/UIActionSheet
  • UIKit/UIAlertView
  • UIKit/UICollectionView
  • UIKit/UIDocumentInteractionController
  • UIKit/UIDocumentMenuViewController
  • UIKit/UIDocumentPickerViewController
  • UIKit/UIDynamicAnimator
  • UIKit/UIGestureRecognizer
  • UIKit/UIImagePickerController
  • UIKit/UINavigationBar
  • UIKit/UINavigationController
  • UIKit/UIPageViewController
  • UIKit/UIPickerView
  • UIKit/UIPopoverController
  • UIKit/UIPopoverPresentationController
  • UIKit/UIPresentationController
  • UIKit/UIPrinterPickerController
  • UIKit/UIPrintInteractionController
  • UIKit/UIScrollView
  • UIKit/UISearchBar
  • UIKit/UISearchController
  • UIKit/UISplitViewController
  • UIKit/UITabBar
  • UIKit/UITabBarController
  • UIKit/UITableView
  • UIKit/UITextField
  • UIKit/UITextView
  • UIKit/UIToolbar
  • UIKit/UIVideoEditorController
  • UIKit/UIViewController
  • UIKit/UIWebView

Other add target action method

  • UIControl
    • UIButton
    • UIDatePicker
    • UIPageControl
    • UIRefreshControl
    • UISegmentedControl
    • UISlider
    • UIStepper
    • UISwitch
    • UITextField
  • UIBarButtonItem
  • UIGestureRecognizer

License

Centipede is released under the MIT license. See LICENSE for details.

centipede's People

Contributors

klaus01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

centipede's Issues

为什么extension里可以override?

我本来想把tableview那个文件拖进我的demo里试下的,但是报错,看了下是因为在extension里写了override,我记得苹果是不让在extension里override的,你那框架里为什么不报错?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.