Browse files

- Added TransformableTableViewCellStyle to configure between Unfoldin…

…g and PullDown cell style
  • Loading branch information...
1 parent 46bc30d commit cb5e47ada93c28c0ca01d8173107cb91b6645f45 @jamztang committed Mar 4, 2012
View
17 JTGestureBasedTableView/TransformableTableViewCell.h
@@ -8,9 +8,24 @@
#import <UIKit/UIKit.h>
-@interface TransformableTableViewCell : UITableViewCell
+typedef enum {
+ TransformableTableViewCellStyleUnfolding,
+ TransformableTableViewCellStylePullDown,
+} TransformableTableViewCellStyle;
+
+
+@protocol TransformableTableViewCell <NSObject>
@property (nonatomic, assign) CGFloat finishedHeight;
@property (nonatomic, retain) UIColor *tintColor; // default is white color
@end
+
+
+@interface TransformableTableViewCell : UITableViewCell <TransformableTableViewCell>
+
+// Use this factory method instead of
+// - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
++ (TransformableTableViewCell *)transformableTableViewCellWithStyle:(TransformableTableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
+
+@end
View
97 JTGestureBasedTableView/TransformableTableViewCell.m
@@ -10,9 +10,16 @@
#import "UIColor+JTGestureBasedTableViewHelper.h"
#import <QuartzCore/QuartzCore.h>
-@implementation TransformableTableViewCell
-@synthesize finishedHeight, tintColor;
+@interface JTUnfoldingTableViewCell : TransformableTableViewCell
+@end
+
+@interface JTPullDownTableViewCell : TransformableTableViewCell
+@end
+
+#pragma mark -
+
+@implementation JTUnfoldingTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
@@ -37,13 +44,6 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
return self;
}
-- (void)setSelected:(BOOL)selected animated:(BOOL)animated
-{
- [super setSelected:selected animated:animated];
-
- // Configure the view for the selected state
-}
-
- (void)layoutSubviews {
[super layoutSubviews];
@@ -71,3 +71,82 @@ - (void)layoutSubviews {
}
@end
+
+@implementation JTPullDownTableViewCell
+
+- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
+{
+ self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
+ if (self) {
+ // Initialization code
+
+ CATransform3D transform = CATransform3DIdentity;
+ transform.m34 = -1/500.f;
+ [self.contentView.layer setSublayerTransform:transform];
+
+ self.textLabel.layer.anchorPoint = CGPointMake(0.5, 1.0);
+
+ self.selectionStyle = UITableViewCellSelectionStyleNone;
+
+ self.textLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
+
+ self.tintColor = [UIColor whiteColor];
+ }
+ return self;
+}
+
+- (void)layoutSubviews {
+ [super layoutSubviews];
+
+ CGFloat fraction = (self.frame.size.height / self.finishedHeight);
+ fraction = MAX(MIN(1, fraction), 0);
+
+ CGFloat angle = (M_PI / 2) - asinf(fraction);
+ CATransform3D transform = CATransform3DMakeRotation(angle, 1, 0, 0);
+ [self.textLabel.layer setTransform:transform];
+
+ self.textLabel.backgroundColor = [self.tintColor colorWithBrightness:0.3 + 0.7*fraction];
+
+ CGSize contentViewSize = self.contentView.frame.size;
+ CGFloat labelHeight = self.finishedHeight;
+
+ // OPTI: Always accomodate 1 px to the top label to ensure two labels
+ // won't display one px gap in between sometimes for certain angles
+ self.textLabel.frame = CGRectMake(0, contentViewSize.height - labelHeight,
+ contentViewSize.width, labelHeight);
+}
+
+@end
+
+
+
+#pragma mark -
+
+@implementation TransformableTableViewCell
+@synthesize finishedHeight, tintColor;
+
++ (TransformableTableViewCell *)unfoldingTableViewCellWithReuseIdentifier:(NSString *)reuseIdentifier {
+ JTUnfoldingTableViewCell *cell = (id)[[JTUnfoldingTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
+ reuseIdentifier:reuseIdentifier];
+ return cell;
+}
+
++ (TransformableTableViewCell *)pullDownTableViewCellWithReuseIdentifier:(NSString *)reuseIdentifier {
+ JTPullDownTableViewCell *cell = (id)[[JTPullDownTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
+ reuseIdentifier:reuseIdentifier];
+ return cell;
+}
+
++ (TransformableTableViewCell *)transformableTableViewCellWithStyle:(TransformableTableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
+ switch (style) {
+ case TransformableTableViewCellStylePullDown:
+ return [TransformableTableViewCell pullDownTableViewCellWithReuseIdentifier:reuseIdentifier];
+ break;
+ case TransformableTableViewCellStyleUnfolding:
+ default:
+ return [TransformableTableViewCell unfoldingTableViewCellWithReuseIdentifier:reuseIdentifier];
+ break;
+ }
+}
+
+@end
View
70 JTGestureBasedTableViewDemo/ViewController.m
@@ -75,27 +75,61 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
NSObject *object = [self.rows objectAtIndex:indexPath.row];
UIColor *backgroundColor = [[UIColor redColor] colorWithHueOffset:0.12 * indexPath.row / [self tableView:tableView numberOfRowsInSection:indexPath.section]];
if ([object isEqual:ADDING_CELL]) {
- static NSString *cellIdentifier = @"TransformableTableViewCell";
- TransformableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
- if (cell == nil) {
- cell = [[TransformableTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
- cell.textLabel.adjustsFontSizeToFitWidth = YES;
- cell.textLabel.textColor = [UIColor whiteColor];
- cell.textLabel.textAlignment = UITextAlignmentCenter;
- }
-
- // Setup tint color
- cell.tintColor = backgroundColor;
+ NSString *cellIdentifier = nil;
+ TransformableTableViewCell *cell = nil;
+
+ // IndexPath.row == 0 is the case we wanted to pick the pullDown style
+ if (indexPath.row == 0) {
+ cellIdentifier = @"PullDownTableViewCell";
+ cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
+
+ if (cell == nil) {
+ cell = [TransformableTableViewCell transformableTableViewCellWithStyle:TransformableTableViewCellStylePullDown
+ reuseIdentifier:cellIdentifier];
+ cell.textLabel.adjustsFontSizeToFitWidth = YES;
+ cell.textLabel.textColor = [UIColor whiteColor];
+ cell.textLabel.textAlignment = UITextAlignmentCenter;
+ }
+
+ // Setup tint color
+ cell.tintColor = backgroundColor;
+
+ cell.finishedHeight = COMMITING_CREATE_CELL_HEIGHT;
+ if (cell.frame.size.height > COMMITING_CREATE_CELL_HEIGHT) {
+ cell.textLabel.text = @"Release to create cell...";
+ } else {
+ cell.textLabel.text = @"Continue Pulling...";
+ }
+ cell.contentView.backgroundColor = [UIColor clearColor];
+ cell.detailTextLabel.text = @" ";
+ return cell;
- cell.finishedHeight = COMMITING_CREATE_CELL_HEIGHT;
- if (cell.frame.size.height > COMMITING_CREATE_CELL_HEIGHT) {
- cell.textLabel.text = @"Release to create cell...";
} else {
- cell.textLabel.text = (NSString *)object;
+ // Otherwise is the case we wanted to pick the pullDown style
+ cellIdentifier = @"UnfoldingTableViewCell";
+ cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
+
+ if (cell == nil) {
+ cell = [TransformableTableViewCell transformableTableViewCellWithStyle:TransformableTableViewCellStyleUnfolding
+ reuseIdentifier:cellIdentifier];
+ cell.textLabel.adjustsFontSizeToFitWidth = YES;
+ cell.textLabel.textColor = [UIColor whiteColor];
+ cell.textLabel.textAlignment = UITextAlignmentCenter;
+ }
+
+ // Setup tint color
+ cell.tintColor = backgroundColor;
+
+ cell.finishedHeight = COMMITING_CREATE_CELL_HEIGHT;
+ if (cell.frame.size.height > COMMITING_CREATE_CELL_HEIGHT) {
+ cell.textLabel.text = @"Release to create cell...";
+ } else {
+ cell.textLabel.text = @"Continue Pinching...";
+ }
+ cell.contentView.backgroundColor = [UIColor clearColor];
+ cell.detailTextLabel.text = @" ";
+ return cell;
}
- cell.contentView.backgroundColor = [UIColor clearColor];
- cell.detailTextLabel.text = @" ";
- return cell;
} else {
View
BIN demo1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN demo2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cb5e47a

Please sign in to comment.