Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hellozimi/KVOR
Objective C KeyValueObservation is in the block.
https://github.com/hellozimi/KVOR
Last synced: about 1 month ago
JSON representation
Objective C KeyValueObservation is in the block.
- Host: GitHub
- URL: https://github.com/hellozimi/KVOR
- Owner: hellozimi
- Archived: true
- Created: 2013-02-23T20:52:13.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2013-04-06T22:26:55.000Z (over 11 years ago)
- Last Synced: 2024-04-14T18:06:51.562Z (8 months ago)
- Language: Objective-C
- Size: 156 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome - KVOR - Objective C KeyValueObservation is in the block. (etc)
- awesome - KVOR - Objective C KeyValueObservation is in the block. (etc)
README
#KVOR
KVOR is a block based wrapper around [KVO](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html). It's really easy to use and it supports multiple key paths per block. Check the examples below to get started.
### Methods
Add observer```objc
+ (void)target:(id)target keyPath:(NSString *)keyPath task:(KVORObserverTaskBlock)block;
+ (void)target:(id)target keyPaths:(NSArray *)keyPaths task:(KVORObserverTaskBlock)block;
```Remove observer
```objc
+ (void)removeObserverWithTarget:(id)target andKeyPath:(NSString *)keyPath;
+ (void)removeObserverWithTarget:(id)target andKeyPaths:(NSArray *)keyPaths;
+ (void)removeAllObservers;
```
---### Example
Basic value listening
```objc
// Start listening
__block typeof(self) weakSelf = self;
[KVOR target:self.scrollView
keyPath:@"contentOffset"
task:^(NSString *keyPath, NSDictionary *change) {
if ([keyPath isEqualToString:@"contentOffset"]) {
NSLog(@"Offset: %@", NSStringFromCGPoint([weakSelf.scrollView contentOffset]));
}
}];```
You're also able to listen on multiple keyPaths in the same block.
```objc
// Start listening
__block typeof(self) weakSelf = self;
[KVOR target:self.scrollView
keyPaths:@[@"contentOffset", @"contentSize"]
task:^(NSString *keyPath, NSDictionary *change) {
if ([keyPath isEqualToString:@"contentOffset"]) {
NSLog(@"Offset: %@", NSStringFromCGPoint([weakSelf.scrollView contentOffset]));
}
else if ([keyPath isEqualToString:@"contentSize"]) {
NSLog(@"Size: %@", NSStringFromCGSize([weakSelf.scrollView contentSize]));
}
}];```