Remove KVO in Category. Require manual update now.

This commit is contained in:
Kyle Fang
2013-06-02 17:32:55 +08:00
parent ce1842d673
commit b023b499f9
4 changed files with 18 additions and 79 deletions

View File

@@ -23,4 +23,6 @@ typedef NSUInteger ZGScrollViewStyle;
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio cutOffAtMax:(BOOL)cutOffAtMax DEPRECATED_ATTRIBUTE; - (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio cutOffAtMax:(BOOL)cutOffAtMax DEPRECATED_ATTRIBUTE;
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio headerViewStyle:(ZGScrollViewStyle )style; - (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio headerViewStyle:(ZGScrollViewStyle )style;
- (void)updateParallelViewWithOffset:(CGPoint)contentOffset;
@end @end

View File

@@ -9,17 +9,10 @@
#import "UITableView+ZGParallelView.h" #import "UITableView+ZGParallelView.h"
#import <objc/runtime.h> #import <objc/runtime.h>
@interface ZGScrollView : UIScrollView
@property (nonatomic, weak) UITableView *tableView;
@end
static char UITableViewZGParallelViewDisplayRadio; static char UITableViewZGParallelViewDisplayRadio;
static char UITableViewZGParallelViewViewHeight; static char UITableViewZGParallelViewViewHeight;
static char UITableViewZGParallelViewStyle; static char UITableViewZGParallelViewStyle;
static char UITableViewZGParallelViewEmbededScrollView; static char UITableViewZGParallelViewEmbededScrollView;
static char UITableViewZGParallelViewIsObserving;
static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet; static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
@@ -28,13 +21,12 @@ static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
@property (nonatomic, assign) CGFloat viewHeight; @property (nonatomic, assign) CGFloat viewHeight;
@property (nonatomic, assign) ZGScrollViewStyle parallelViewStyle; @property (nonatomic, assign) ZGScrollViewStyle parallelViewStyle;
@property (nonatomic, assign) BOOL cutOffAtMaxSetContentOffSet; @property (nonatomic, assign) BOOL cutOffAtMaxSetContentOffSet;
@property (nonatomic, strong) ZGScrollView *embededScrollView; @property (nonatomic, strong) UIScrollView *embededScrollView;
@property (nonatomic, assign) BOOL isObserving;
@end @end
@implementation UITableView (ZGParallelViewPri) @implementation UITableView (ZGParallelViewPri)
@dynamic displayRadio, viewHeight, parallelViewStyle, embededScrollView, isObserving, cutOffAtMaxSetContentOffSet; @dynamic displayRadio, viewHeight, parallelViewStyle, embededScrollView, cutOffAtMaxSetContentOffSet;
- (void)setDisplayRadio:(CGFloat)displayRadio { - (void)setDisplayRadio:(CGFloat)displayRadio {
[self willChangeValueForKey:@"displayRadio"]; [self willChangeValueForKey:@"displayRadio"];
@@ -92,7 +84,7 @@ static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
} }
} }
- (void)setEmbededScrollView:(ZGScrollView *)embededScrollView { - (void)setEmbededScrollView:(UIScrollView *)embededScrollView {
[self willChangeValueForKey:@"embededScrollView"]; [self willChangeValueForKey:@"embededScrollView"];
objc_setAssociatedObject(self, &UITableViewZGParallelViewEmbededScrollView, objc_setAssociatedObject(self, &UITableViewZGParallelViewEmbededScrollView,
embededScrollView, embededScrollView,
@@ -100,35 +92,9 @@ static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
[self didChangeValueForKey:@"embededScrollView"]; [self didChangeValueForKey:@"embededScrollView"];
} }
- (ZGScrollView *)embededScrollView { - (UIScrollView *)embededScrollView {
return objc_getAssociatedObject(self, &UITableViewZGParallelViewEmbededScrollView); return objc_getAssociatedObject(self, &UITableViewZGParallelViewEmbededScrollView);
} }
- (void)setIsObserving:(BOOL)isObserving {
if (self.isObserving == YES && isObserving == NO) {
@try {
[self removeObserver:self forKeyPath:@"contentOffset"];
}
@catch (NSException *exception) {
//It's not observing
}
}
[self willChangeValueForKey:@"isObserving"];
objc_setAssociatedObject(self, &UITableViewZGParallelViewIsObserving,
[NSNumber numberWithBool:isObserving],
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"isObserving"];
}
- (BOOL)isObserving {
NSNumber *number = objc_getAssociatedObject(self, &UITableViewZGParallelViewIsObserving);
if (number == nil) {
return NO;
} else {
return [number boolValue];
}
}
@end @end
@@ -158,9 +124,8 @@ static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
} }
self.viewHeight = aViewToAdd.frame.size.height; self.viewHeight = aViewToAdd.frame.size.height;
self.parallelViewStyle = parallelViewStyle; self.parallelViewStyle = parallelViewStyle;
self.embededScrollView = [[ZGScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.viewHeight)]; self.embededScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.viewHeight)];
self.embededScrollView.scrollsToTop = NO; self.embededScrollView.scrollsToTop = NO;
self.embededScrollView.tableView = self;
[self.embededScrollView addSubview:aViewToAdd]; [self.embededScrollView addSubview:aViewToAdd];
aViewToAdd.frame = CGRectOffset(aViewToAdd.frame, 0, self.viewHeight*(1.f - self.displayRadio)/2.f); aViewToAdd.frame = CGRectOffset(aViewToAdd.frame, 0, self.viewHeight*(1.f - self.displayRadio)/2.f);
UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.viewHeight*self.displayRadio)]; UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.viewHeight*self.displayRadio)];
@@ -168,23 +133,9 @@ static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
self.embededScrollView.frame = CGRectOffset(self.embededScrollView.frame, 0, self.viewHeight*(self.displayRadio-1.f)); self.embededScrollView.frame = CGRectOffset(self.embededScrollView.frame, 0, self.viewHeight*(self.displayRadio-1.f));
self.tableHeaderView = headView; self.tableHeaderView = headView;
if (self.isObserving == NO) {
[self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
self.isObserving = YES;
}
} }
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - (void)updateParallelViewWithOffset:(CGPoint)contentOffset {
if([keyPath isEqualToString:@"contentOffset"]) {
if (self.cutOffAtMaxSetContentOffSet) {
self.cutOffAtMaxSetContentOffSet = NO;
} else {
[self scrollViewDidScroll:[[change valueForKey:NSKeyValueChangeNewKey] CGPointValue]];
}
}
}
- (void)scrollViewDidScroll:(CGPoint)contentOffset {
CGFloat yOffset = contentOffset.y; CGFloat yOffset = contentOffset.y;
if (yOffset<0 && yOffset>self.viewHeight*(self.displayRadio-1.f)) { if (yOffset<0 && yOffset>self.viewHeight*(self.displayRadio-1.f)) {
self.embededScrollView.contentOffset = CGPointMake(0.f, -yOffset*0.5f); self.embededScrollView.contentOffset = CGPointMake(0.f, -yOffset*0.5f);
@@ -209,21 +160,4 @@ static char UITableViewZGParallelViewCutOffAtMaxSetContentOffSet;
} }
} }
- (void)dealloc{ @end
if (self.isObserving) {
self.isObserving = NO;
}
}
@end
@implementation ZGScrollView
@synthesize tableView;
- (void)dealloc {
if ([self.tableView isObserving] == YES) {
self.tableView.isObserving = NO;//!!Remove KVO Observer
}
}
@end

View File

@@ -51,6 +51,14 @@
} }
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView == self.tableView) {
[self.tableView updateParallelViewWithOffset:scrollView.contentOffset];
}
if (scrollView == self.headerScrollView) {
[self darkerTheBackground:scrollView.contentOffset.x];
}
}
//===================================================================== //=====================================================================
@@ -59,11 +67,6 @@
// //
//===================================================================== //=====================================================================
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self darkerTheBackground:scrollView.contentOffset.x];
}
- (void)customizeUI{ - (void)customizeUI{
self.headerScrollView.contentSize = self.contontView.frame.size; self.headerScrollView.contentSize = self.contontView.frame.size;
self.headerScrollView.scrollsToTop = NO; self.headerScrollView.scrollsToTop = NO;

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12C60" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="gTM-y4-tcx"> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="3084" systemVersion="12D78" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="gTM-y4-tcx">
<dependencies> <dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="2083"/>
</dependencies> </dependencies>