iOS library adding closure (block or callback) functionality to several native classes (buttons, sliders, notifications, etc).
This library is written in Swift but it also works in Objective-C. Make sure to read the installation notes below.
- Buttons, Sliders, etc (UIControl)
Alert Views(No longer supported. UseUIAlertController
)- Notifications
- Gesture Recognizers
- Some Delegates
- Observing an Object's Deinit (Dealloc)
- Installation
Just add Curly.swift to your project, or use CocoaPods:
pod "Curly", :git => 'https://github.com/wircho/Curly.git', :branch => 'master'
This library is written in Swift but it also works in Objective-C. If you are using Objective-C, make sure you add #import "[YourProjectName]-Swift.h"
at the beginning of your Objective-C file. You may need to compile once for the Swift methods to be recognized by Xcode's Objective-C editor.
- Usage
You can preview the functionality below by running the sample project in the CurlySample folder
button.addAction(.TouchUpInside) {
(bttn:UIButton) -> Void in
print("tapped button")
}
slider.addAction(.ValueChanged) {
(sldr:UISlider) -> Void in
print("moved slider")
}
This works with any subclass of UIControl.
[button addAction:UIControlEventTouchUpInside block:^(UIControl *bttn) {
NSLog(@"tapped button");
}];
[slider addAction:UIControlEventValueChanged block:^(UIControl *sldr) {
NSLog(@"moved slider");
}];
This functionality is no longer supported by Curly. Please use UIAlertController
.
"SOME-NOTIFICATION-NAME".observeFrom(self) {
innerSelf, note in
print("Listening to NSNotification \(note) from \(innerSelf)")
}
The code above has the added advantage that the observation stops as soon as self
is released/deinited. If you would like to have more control, the method above returns a CurlyNotificationToken
which you may keep and cancel at any time using token.cancel()
.
No special Objective-C support yet.
let gestureRecognizer = UIPanGestureRecognizer {
(gr:UIPanGestureRecognizer)->Void in
print("gesture recognizer: \(gr)")
}
This works with any subclass of UIGestureRecognizer.
UIPanGestureRecognizer *gestureRecognizer
= [[UIPanGestureRecognizer alloc] initWithBlock:^(UIGestureRecognizer *gr) {
NSLog(@"gesture recognizer: %@",gr);
}];
With Curly you can define delegates for UIScrollView and UINavigationController using only closures/blocks.
scrollView.setDelegate(
didScroll: { (scrollView:UIScrollView) -> Void in
print("did scroll")
}
)
For the complete UIScrollViewDelegate
functionality use .setDelegate(willBeginDragging:,didScroll:,willEndDragging:,didEndDragging:,willBeginDecelerating:,didEndDecelerating:,didEndScrollingAnimation:,shouldScrollToTop:,didScrollToTop:,willBeginZooming:,didZoom:,didEndZooming:,viewForZooming:)
navigationController.setDelegate(
willShow: { (viewController:UIViewController) -> Void in
print("will show")
},
didShow: { (viewController:UIViewController) -> Void in
print("did show")
}
)
The second parameter didShow
is optional.
[scrollView setDelegateWithDidScroll:^(UIScrollView *scrollView) {
NSLog(@"did scroll");
}];
For the complete UIScrollViewDelegate
functionality use setDelegateWithWillBeginDragging:didScroll:willEndDragging:didEndDragging:willBeginDecelerating:didEndDecelerating:didEndScrollingAnimation:shouldScrollToTop:didScrollToTop:willBeginZooming:didZoom:didEndZooming:viewForZooming:
o
[navigationController setDelegateWithWillShow:^(UIViewController *viewController, BOOL animated) {
NSLog(@"will show");
} didShow:^(UIViewController *viewController, BOOL animated) {
NSLog(@"did show");
}];
The method below works with any subclass of NSObject. Unfortunately, as of now, you cannot refer to your object or its properties inside the closure. In fact, any weak reference to the object will be nil by the time you are in the closure.
object.deinited {
print("object has been deinited")
}
[object deinited:^{
NSLog(@"object has been deinited");
}];