Init version

This commit is contained in:
Kyle Fang
2013-02-26 14:16:56 +08:00
parent aac3ce224a
commit 94a1cc4264
4 changed files with 311 additions and 13 deletions

View File

@@ -8,7 +8,9 @@
#import "DemoTableViewController.h"
@interface DemoTableViewController ()
#import "UITableView+ZGPullDrag.h"
@interface DemoTableViewController () <ZGPullViewDelegate, ZGDragViewDelegate>
@end
@@ -34,12 +36,49 @@
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *pullView = [[UIView alloc] initWithFrame:CGRectMake(-320, 0, 320, 80)];
pullView.backgroundColor = [UIColor greenColor];
pullView.alpha = 0;
[self.tableView addZGPullView:pullView];
self.tableView.ZGPullViewDelegate = self;
UIView *dragView = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 80)];
dragView.backgroundColor = [UIColor greenColor];
dragView.alpha = 0;
[self.tableView addZGDragView:dragView];
self.tableView.ZGDragViewDelegate = self;
}
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
- (void)pullView:(UIView *)pullView Show:(CGFloat)shownPixels ofTotal:(CGFloat)totalPixels{
CGFloat progress = MIN(shownPixels / totalPixels, 1.f);
pullView.alpha = progress;
CGRect frame = pullView.frame;
frame.origin.x = -320+320*progress;
pullView.frame = frame;
}
- (void)pullView:(UIView *)pullView hangForCompletionBlock:(void (^)())completed{
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completed();
});
}
- (void)dragView:(UIView *)dragView Show:(CGFloat)showPixels ofTotal:(CGFloat)totalPixels{
CGFloat progress = MIN(showPixels/totalPixels, 1.f);
dragView.alpha = progress;
CGRect frame = dragView.frame;
frame.origin.x = 320-320*progress;
dragView.frame = frame;
}
- (void)dragView:(UIView *)dragView hangForCompletionBlock:(void (^)())completed{
int64_t delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
completed();
});
}
@end

View File

@@ -22,7 +22,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="IXW-cb-TtO">
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 1" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="IXW-cb-TtO">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
@@ -40,7 +40,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="bkS-i6-mHt">
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 2" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="bkS-i6-mHt">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
@@ -58,7 +58,7 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="c1U-ml-gn8">
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 3" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="c1U-ml-gn8">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
@@ -69,14 +69,18 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="Ba0-OE-ICM" style="IBUITableViewCellStyleDefault" id="nc0-cI-m2u">
</cells>
</tableViewSection>
<tableViewSection id="XNc-2C-n9T">
<cells>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="6SP-Nc-1nC" style="IBUITableViewCellStyleDefault" id="5zo-tU-UaG">
<rect key="frame" x="0.0" y="240" width="320" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="Ba0-OE-ICM">
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 1" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="6SP-Nc-1nC">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
@@ -87,14 +91,90 @@
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="l3E-pD-kH4" style="IBUITableViewCellStyleDefault" id="hPR-Eh-fxD">
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="XMz-Od-LM1" style="IBUITableViewCellStyleDefault" id="HHG-NU-Jas">
<rect key="frame" x="0.0" y="320" width="320" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="l3E-pD-kH4">
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 2" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="XMz-Od-LM1">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="dyy-or-5vt" style="IBUITableViewCellStyleDefault" id="pgv-CL-BMK">
<rect key="frame" x="0.0" y="400" width="320" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 3" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="dyy-or-5vt">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection id="I2f-um-1kX">
<cells>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="GYl-mV-GbX" style="IBUITableViewCellStyleDefault" id="JSu-7f-vQ6">
<rect key="frame" x="0.0" y="480" width="320" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 1" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="GYl-mV-GbX">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="B86-GL-KPV" style="IBUITableViewCellStyleDefault" id="vcg-JS-JXD">
<rect key="frame" x="0.0" y="560" width="320" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 2" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="B86-GL-KPV">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
</tableViewCell>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="hhj-fC-yqP" style="IBUITableViewCellStyleDefault" id="cc3-Li-KlS">
<rect key="frame" x="0.0" y="640" width="320" height="80"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="0.0" y="0.0" width="320" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Hello 3" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="hhj-fC-yqP">
<rect key="frame" x="10" y="0.0" width="300" height="79"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="20"/>
@@ -119,6 +199,11 @@
<point key="canvasLocation" x="295" y="131"/>
</scene>
</scenes>
<classes>
<class className="DemoTableViewController" superclassName="UITableViewController">
<source key="sourceIdentifier" type="project" relativePath="./Classes/DemoTableViewController.h"/>
</class>
</classes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>

View File

@@ -8,6 +8,21 @@
#import <UIKit/UIKit.h>
@protocol ZGDragViewDelegate <NSObject>
- (void)dragView:(UIView *)dragView Show:(CGFloat )showPixels ofTotal:(CGFloat )totalPixels;
- (void)dragView:(UIView *)dragView hangForCompletionBlock:(void (^)())completed;
@end
@protocol ZGPullViewDelegate <NSObject>
- (void)pullView:(UIView *)pullView Show:(CGFloat )shownPixels ofTotal:(CGFloat )totalPixels;
- (void)pullView:(UIView *)pullView hangForCompletionBlock:(void (^)())completed;
@end
@interface UITableView (ZGPullDrag)
- (void)addZGPullView:(UIView *)pullView;
- (void)addZGDragView:(UIView *)dragView;
@property (nonatomic) id <ZGPullViewDelegate> ZGPullViewDelegate;
@property (nonatomic) id <ZGDragViewDelegate> ZGDragViewDelegate;
@end

View File

@@ -7,7 +7,166 @@
//
#import "UITableView+ZGPullDrag.h"
#import <objc/runtime.h>
static char UITableViewZGPullViewDelegate;
static char UITableViewZGDragViewDelegate;
static char UITableViewZGPullDragViewObserving;
static char UITableViewZGPullView;
static char UITableViewZGDragView;
@interface UITableView (ZGPullDragPropertyCategory)
@property (nonatomic) BOOL isObserving;
@property (nonatomic, assign) UIView *pullView;
@property (nonatomic, assign) UIView *dragView;
@end
@implementation UITableView (ZGPullDragPropertyCategory)
- (void)setIsObserving:(BOOL)isObserving {
if (self.isObserving == YES && isObserving == NO) {
@try {
[self removeObserver:self forKeyPath:@"contentOffset"];
}
@catch (NSException *exception) {
//It's not observing
}
} else if (self.isObserving == NO && isObserving == YES) {
[self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}
[self willChangeValueForKey:@"isObserving"];
objc_setAssociatedObject(self, &UITableViewZGPullDragViewObserving,
[NSNumber numberWithBool:isObserving],
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"isObserving"];
}
- (BOOL)isObserving {
NSNumber *number = objc_getAssociatedObject(self, &UITableViewZGPullDragViewObserving);
if (number == nil) {
return NO;
} else {
return [number boolValue];
}
}
- (void)setPullView:(UIView *)pullView{
[self willChangeValueForKey:@"pullView"];
objc_setAssociatedObject(self, &UITableViewZGPullView, pullView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"pullView"];
}
- (UIView *)pullView{
return objc_getAssociatedObject(self, &UITableViewZGPullView);
}
- (void)setDragView:(UIView *)dragView{
[self willChangeValueForKey:@"dragView"];
objc_setAssociatedObject(self, &UITableViewZGDragView, dragView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"dragView"];
}
- (UIView *)dragView{
return objc_getAssociatedObject(self, &UITableViewZGDragView);
}
@end
@implementation UITableView (ZGPullDrag)
@dynamic ZGDragViewDelegate, ZGPullViewDelegate;
- (void)setZGPullViewDelegate:(id<ZGPullViewDelegate>)ZGPullViewDelegate{
[self willChangeValueForKey:@"ZGPullViewDelegate"];
objc_setAssociatedObject(self, &UITableViewZGPullViewDelegate, ZGPullViewDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"ZGPullViewDelegate"];
}
- (id<ZGPullViewDelegate>)ZGPullViewDelegate{
return objc_getAssociatedObject(self, &UITableViewZGPullViewDelegate);
}
- (void)setZGDragViewDelegate:(id<ZGDragViewDelegate>)ZGDragViewDelegate{
[self willChangeValueForKey:@"ZGDragViewDelegate"];
objc_setAssociatedObject(self, &UITableViewZGDragViewDelegate, ZGDragViewDelegate, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"ZGDragViewDelegate"];
}
- (id<ZGDragViewDelegate>)ZGDragViewDelegate{
return objc_getAssociatedObject(self, &UITableViewZGDragViewDelegate);
}
- (void)addZGPullView:(UIView *)pullView{
pullView.frame = CGRectOffset(pullView.frame, -pullView.frame.origin.x, -pullView.frame.origin.y-pullView.frame.size.height);
[self addSubview:pullView];
self.pullView = pullView;
self.isObserving = YES;
}
- (void)addZGDragView:(UIView *)dragView{
[self layoutIfNeeded];
CGFloat originY = MAX(self.frame.size.height, self.contentSize.height);
dragView.frame = CGRectOffset(dragView.frame, -dragView.frame.origin.x, -dragView.frame.origin.y+originY);
[self addSubview:dragView];
self.dragView = dragView;
self.isObserving = YES;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqualToString:@"contentOffset"]) {
[self scrollViewDidScroll:[[change valueForKey:NSKeyValueChangeNewKey] CGPointValue]];
}
}
- (void)scrollViewDidScroll:(CGPoint )contentOffset {
CGFloat yOffset = contentOffset.y;
if (yOffset<0) {
[self pullViewHandler:-yOffset];
} else if (self.dragView.frame.origin.y == self.frame.size.height) {
[self dragViewHandler:yOffset];
} else if (self.dragView.frame.origin.y == self.contentSize.height && yOffset > self.dragView.frame.origin.y - self.frame.size.height) {
[self dragViewHandler:yOffset-(self.dragView.frame.origin.y - self.frame.size.height)];
}
}
- (void)pullViewHandler:(CGFloat )visiblePixels{
[self.ZGPullViewDelegate pullView:self.pullView Show:visiblePixels ofTotal:self.pullView.frame.size.height];
if (visiblePixels>self.pullView.frame.size.height && !self.isDragging) {
[UIView animateWithDuration:0.1
animations:^{
self.contentInset = UIEdgeInsetsMake(self.pullView.frame.size.height, 0, 0, 0);
} completion:^(BOOL finished) {
[self.ZGPullViewDelegate pullView:self.pullView hangForCompletionBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[UIView animateWithDuration:0.2 animations:^{
self.contentInset = UIEdgeInsetsZero;
}];
}];
}];
}];
}
}
- (void)dragViewHandler:(CGFloat )visiblePixels{
[self.ZGDragViewDelegate dragView:self.dragView Show:visiblePixels ofTotal:self.dragView.frame.size.height];
if (visiblePixels>self.dragView.frame.size.height && !self.isDragging) {
[UIView animateWithDuration:0.1
animations:^{
self.contentInset = UIEdgeInsetsMake(0, 0, self.dragView.frame.size.height, 0);
} completion:^(BOOL finished) {
[self.ZGDragViewDelegate dragView:self.dragView hangForCompletionBlock:^{
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[UIView animateWithDuration:0.2 animations:^{
self.contentInset = UIEdgeInsetsZero;
}];
}];
}];
}];
}
}
@end