Added StickToTop style and enhanced the API

This commit is contained in:
Kyle Fang
2013-02-26 08:43:48 +08:00
parent 69f0abc896
commit 285586a801
4 changed files with 72 additions and 42 deletions

View File

@@ -8,10 +8,19 @@
#import <UIKit/UIKit.h>
enum {
ZGScrollViewStyleDefault = 0,
ZGScrollViewStyleCutOffAtMax,
ZGScrollViewStyleStickToTheTop = 10
};
typedef NSUInteger ZGScrollViewStyle;
@interface UITableView (ZGParallelView)
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd;
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio;
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio cutOffAtMax:(BOOL)cutOffAtMax;
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio cutOffAtMax:(BOOL)cutOffAtMax DEPRECATED_ATTRIBUTE;
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat )displayRadio headerViewStyle:(ZGScrollViewStyle )style;
@end

View File

@@ -17,7 +17,7 @@
static char UITableViewZGParallelViewDisplayRadio;
static char UITableViewZGParallelViewViewHeight;
static char UITableViewZGParallelViewCutOffAtMax;
static char UITableViewZGParallelViewStyle;
static char UITableViewZGParallelViewEmbededScrollView;
static char UITableViewZGParallelViewIsObserving;
@@ -25,14 +25,14 @@ static char UITableViewZGParallelViewIsObserving;
@interface UITableView (ZGParallelViewPri)
@property (nonatomic, assign) CGFloat displayRadio;
@property (nonatomic, assign) CGFloat viewHeight;
@property (nonatomic, assign) BOOL cutOffAtMax;
@property (nonatomic, assign) ZGScrollViewStyle parallelViewStyle;
@property (nonatomic, strong) ZGScrollView *embededScrollView;
@property (nonatomic, assign) BOOL isObserving;
@end
@implementation UITableView (ZGParallelViewPri)
@dynamic displayRadio, viewHeight, cutOffAtMax, embededScrollView, isObserving;
@dynamic displayRadio, viewHeight, parallelViewStyle, embededScrollView, isObserving;
- (void)setDisplayRadio:(CGFloat)displayRadio {
[self willChangeValueForKey:@"displayRadio"];
@@ -60,18 +60,18 @@ static char UITableViewZGParallelViewIsObserving;
return [number floatValue];
}
- (void)setCutOffAtMax:(BOOL)cutOffAtMax{
[self willChangeValueForKey:@"cutOffAtMax"];
objc_setAssociatedObject(self, &UITableViewZGParallelViewCutOffAtMax, [NSNumber numberWithBool:cutOffAtMax], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"cutOffAtMax"];
- (void)setParallelViewStyle:(ZGScrollViewStyle)parallelViewStyle{
[self willChangeValueForKey:@"parallelViewStyle"];
objc_setAssociatedObject(self, &UITableViewZGParallelViewStyle, [NSNumber numberWithInteger:parallelViewStyle], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[self didChangeValueForKey:@"parallelViewStyle"];
}
- (BOOL)cutOffAtMax{
NSNumber *number = objc_getAssociatedObject(self, &UITableViewZGParallelViewCutOffAtMax);
- (ZGScrollViewStyle )parallelViewStyle{
NSNumber *number = objc_getAssociatedObject(self, &UITableViewZGParallelViewStyle);
if (number == nil) {
return NO;
return ZGScrollViewStyleDefault;
} else {
return [number boolValue];
return [number integerValue];
}
}
@@ -119,14 +119,18 @@ static char UITableViewZGParallelViewIsObserving;
#define DEFAULT_DISPLAY_RADIO 0.5f
@implementation UITableView (ZGParallelView)
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd {
[self addParallelViewWithUIView:aViewToAdd withDisplayRadio:DEFAULT_DISPLAY_RADIO];
[self addParallelViewWithUIView:aViewToAdd withDisplayRadio:DEFAULT_DISPLAY_RADIO headerViewStyle:ZGScrollViewStyleDefault];
}
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat)displayRadio{
[self addParallelViewWithUIView:aViewToAdd withDisplayRadio:displayRadio cutOffAtMax:NO];
[self addParallelViewWithUIView:aViewToAdd withDisplayRadio:displayRadio headerViewStyle:ZGScrollViewStyleDefault];
}
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat)aDisplayRadio cutOffAtMax:(BOOL)cutOffAtMax{
[self addParallelViewWithUIView:aViewToAdd withDisplayRadio:aDisplayRadio headerViewStyle:ZGScrollViewStyleCutOffAtMax];
}
- (void)addParallelViewWithUIView:(UIView *)aViewToAdd withDisplayRadio:(CGFloat)aDisplayRadio headerViewStyle:(ZGScrollViewStyle)parallelViewStyle{
NSAssert(aViewToAdd != nil, @"aViewToAdd can not be nil");
aViewToAdd.frame = CGRectOffset(aViewToAdd.frame, -aViewToAdd.frame.origin.x, -aViewToAdd.frame.origin.y);
@@ -136,7 +140,7 @@ static char UITableViewZGParallelViewIsObserving;
self.displayRadio = aDisplayRadio;
}
self.viewHeight = aViewToAdd.frame.size.height;
self.cutOffAtMax = cutOffAtMax;
self.parallelViewStyle = parallelViewStyle;
self.embededScrollView = [[ZGScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.viewHeight)];
self.embededScrollView.scrollsToTop = NO;
self.embededScrollView.tableView = self;
@@ -157,10 +161,19 @@ static char UITableViewZGParallelViewIsObserving;
CGFloat yOffset = self.contentOffset.y;
if (yOffset<0 && yOffset>self.viewHeight*(self.displayRadio-1.f)) {
self.embededScrollView.contentOffset = CGPointMake(0.f, -yOffset*0.5f);
}
if (self.cutOffAtMax && yOffset<self.viewHeight*(self.displayRadio-1.f)) {
self.contentOffset = CGPointMake(0.f, self.viewHeight*(self.displayRadio-1.f));
} else if (yOffset<self.viewHeight*(self.displayRadio-1.f)) {
switch (self.parallelViewStyle) {
case ZGScrollViewStyleCutOffAtMax:
self.contentOffset = CGPointMake(0.f, self.viewHeight*(self.displayRadio-1.f));
break;
case ZGScrollViewStyleStickToTheTop:{
self.embededScrollView.frame = CGRectMake(0, yOffset, self.embededScrollView.frame.size.width, self.embededScrollView.frame.size.height);
self.embededScrollView.contentOffset = CGPointMake(0.f, -self.viewHeight*(self.displayRadio-1.f)*0.5);
break;
}
default:
break;
}
}
}

View File

@@ -27,9 +27,8 @@
[self customizeUI];
#warning init with or without displayRadio. This is the first line of code
//[self.tableView addParallelViewWithUIView:self.awesomeZG];
[self.tableView addParallelViewWithUIView:self.awesomeZG withDisplayRadio:0.7 cutOffAtMax:YES];
[self.tableView addParallelViewWithUIView:self.awesomeZG withDisplayRadio:0.7 headerViewStyle:ZGScrollViewStyleDefault];
/** below is just a test for kvo remove, you can ignore it
@@ -44,6 +43,23 @@
*/
}
- (IBAction)SwitchParallelViewStyle:(UISegmentedControl *)sender {
ZGScrollViewStyle PVStyle = nil;
switch (sender.selectedSegmentIndex) {
case 0:
PVStyle = ZGScrollViewStyleDefault;
break;
case 1:
PVStyle = ZGScrollViewStyleCutOffAtMax;
break;
case 2:
PVStyle = ZGScrollViewStyleStickToTheTop;
break;
default:
break;
}
[self.tableView addParallelViewWithUIView:self.awesomeZG withDisplayRadio:0.7 headerViewStyle:PVStyle];
}

View File

@@ -15,20 +15,25 @@
<sections>
<tableViewSection footerTitle="ZGParallelViewForTable looks like Parallax. yet, sepcailly design for UITableView. you can use it with two code." id="HxM-9n-icu">
<cells>
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" textLabel="wzz-RT-3UN" style="IBUITableViewCellStyleDefault" id="jK1-Og-j9t">
<tableViewCell contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" id="jK1-Og-j9t">
<rect key="frame" x="0.0" y="10" width="320" height="46"/>
<autoresizingMask key="autoresizingMask"/>
<view key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center">
<rect key="frame" x="10" y="1" width="300" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="left" text="Work with either UITableView style" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="wzz-RT-3UN">
<rect key="frame" x="10" y="0.0" width="280" height="43"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" type="boldSystem" pointSize="17"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<color key="highlightedColor" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</label>
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="bar" selectedSegmentIndex="0" id="k0d-nd-H2p">
<rect key="frame" x="20" y="7" width="260" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<segments>
<segment title="Default"/>
<segment title="CutOff"/>
<segment title="Stick"/>
</segments>
<connections>
<action selector="SwitchParallelViewStyle:" destination="gTM-y4-tcx" eventType="valueChanged" id="R8C-Pl-syc"/>
</connections>
</segmentedControl>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
@@ -124,19 +129,6 @@ as long as you don't stop.</string>
<resources>
<image name="ZG.png" width="1024" height="1024"/>
</resources>
<classes>
<class className="DemoTableViewController" superclassName="UITableViewController">
<source key="sourceIdentifier" type="project" relativePath="./Classes/DemoTableViewController.h"/>
<relationships>
<relationship kind="action" name="pageChanged:" candidateClass="UIPageControl"/>
<relationship kind="outlet" name="avatar" candidateClass="UIImageView"/>
<relationship kind="outlet" name="awesomeZG" candidateClass="UIView"/>
<relationship kind="outlet" name="contontView" candidateClass="UIView"/>
<relationship kind="outlet" name="headerPageControl" candidateClass="UIPageControl"/>
<relationship kind="outlet" name="headerScrollView" candidateClass="UIScrollView"/>
</relationships>
</class>
</classes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>