Skip to content

Commit

Permalink
- Pinch to add cell
Browse files Browse the repository at this point in the history
  • Loading branch information
jamztang committed Feb 6, 2012
1 parent ba0a5fd commit a1f9ef5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 41 deletions.
38 changes: 14 additions & 24 deletions JTGestureBasedTableViewDemo/TransformableTableViewCell.m
Expand Up @@ -18,22 +18,14 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code

[self addObserver:self forKeyPath:@"frame"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:nil];



CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1/500.f;
[self.contentView.layer setSublayerTransform:transform];


self.textLabel.layer.anchorPoint = CGPointMake(0.5, 0.0);
self.textLabel.clipsToBounds = NO;

self.detailTextLabel.layer.anchorPoint = CGPointMake(0.5, 1.0);
self.detailTextLabel.clipsToBounds = NO;
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
Expand All @@ -46,26 +38,24 @@ - (void)setSelected:(BOOL)selected animated:(BOOL)animated
// Configure the view for the selected state
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"frame"]) {

CGFloat fraction = self.frame.size.height / self.finishedHeight;

CATransform3D transform = CATransform3DMakeRotation((M_PI / 2) - asinf(fraction), -1, 0, 0);
[self.textLabel.layer setTransform:transform];
[self.detailTextLabel.layer setTransform:CATransform3DMakeRotation((M_PI / 2) - asinf(fraction), 1, 0, 0)];
}
}

- (void)layoutSubviews {
[super layoutSubviews];

CGFloat fraction = 1 / (self.frame.size.height / self.finishedHeight);
CGFloat fraction = (self.contentView.frame.size.height / self.finishedHeight);
fraction = MAX(MIN(1, fraction), 0);

self.textLabel.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
self.detailTextLabel.backgroundColor = [UIColor colorWithWhite:0.6 alpha:1];
CATransform3D transform = CATransform3DMakeRotation((M_PI / 2) - asinf(fraction), -1, 0, 0);
[self.textLabel.layer setTransform:transform];
[self.detailTextLabel.layer setTransform:CATransform3DMakeRotation((M_PI / 2) - asinf(fraction), 1, 0, 0)];

CGFloat labelHeight = self.contentView.frame.size.height/2*fraction;
self.textLabel.backgroundColor = [UIColor colorWithWhite:0.65 + 0.3*fraction alpha:1];
self.detailTextLabel.backgroundColor = [UIColor colorWithWhite:0.7 + 0.275*fraction alpha:1];

fraction = 1 / fraction;

CGFloat labelHeight = (int)(self.contentView.frame.size.height/2*fraction + 0.5);
labelHeight = MIN(MAX(1, labelHeight), 800);

self.textLabel.frame = CGRectMake(0, 0, self.contentView.frame.size.width, labelHeight);
self.detailTextLabel.frame = CGRectMake(0,self.contentView.frame.size.height - labelHeight, self.contentView.frame.size.width, labelHeight);
}
Expand Down
98 changes: 81 additions & 17 deletions JTGestureBasedTableViewDemo/ViewController.m
Expand Up @@ -11,17 +11,24 @@
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *selectedIndexPaths;
@property (nonatomic, strong) NSMutableArray *rows;
@property (nonatomic, strong) NSIndexPath *addingIndexPath;
@property (nonatomic, assign) CGFloat addingRowHeight;
@end

@implementation ViewController
@synthesize selectedIndexPaths;
@synthesize rows;
@synthesize addingIndexPath, addingRowHeight;

#define ADDING_CELL @"Continue to pinch..."
#define COMMITING_CREATE_CELL_HEIGHT 80
#define NORMAL_CELL_FINISHING_HEIGHT 60

#pragma mark - View lifecycle

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.selectedIndexPaths = [NSMutableArray array];
self.rows = [NSMutableArray arrayWithObjects:@"Try to pinch between this cell", @"And this cell to create a new one", nil];
}
return self;
}
Expand All @@ -30,6 +37,8 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPinchGestureRecognizer *recognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureRecognizer:)];
[self.tableView addGestureRecognizer:recognizer];
}

- (void)viewDidUnload
Expand Down Expand Up @@ -69,10 +78,61 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
}
}

#pragma mark Action

- (void)pinchGestureRecognizer:(UIPinchGestureRecognizer *)recognizer {
// NSLog(@"%@", recognizer);
// NSLog(@"%d %f %f", [recognizer numberOfTouches], [recognizer velocity], [recognizer scale]);
if (recognizer.state == UIGestureRecognizerStateEnded || [recognizer numberOfTouches] < 2) {
if (self.addingIndexPath) {
TransformableTableViewCell *cell = (TransformableTableViewCell *)[self.tableView cellForRowAtIndexPath:self.addingIndexPath];
[self.tableView beginUpdates];

if (cell.frame.size.height >= COMMITING_CREATE_CELL_HEIGHT) {
[self.rows replaceObjectAtIndex:self.addingIndexPath.row withObject:@"Added!"];
cell.textLabel.text = @"Added!";
cell.finishedHeight = NORMAL_CELL_FINISHING_HEIGHT;
} else {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.addingIndexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[self.rows removeObject:ADDING_CELL];
}
self.addingIndexPath = nil;
[self.tableView endUpdates];
}
return;
}

CGPoint location1 = [recognizer locationOfTouch:0 inView:self.tableView];
CGPoint location2 = [recognizer locationOfTouch:1 inView:self.tableView];

CGRect rect = (CGRect){location1, location2.x - location1.x, location2.y - location1.y};
NSArray *indexPaths = [self.tableView indexPathsForRowsInRect:rect];

NSIndexPath *firstIndexPath = [indexPaths objectAtIndex:0];
// NSIndexPath *lastIndexPath = [indexPaths lastObject];

if (recognizer.state == UIGestureRecognizerStateBegan) {
self.addingIndexPath = [NSIndexPath indexPathForRow:firstIndexPath.row+1 inSection:firstIndexPath.section];
[self.rows insertObject:ADDING_CELL atIndex:firstIndexPath.row+1];

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:self.addingIndexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[self.tableView endUpdates];

} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGFloat diffRowHeight = ([recognizer scale] - 1) * 44;

if (self.addingRowHeight - diffRowHeight >= 1 || self.addingRowHeight - diffRowHeight <= -1) {
self.addingRowHeight = diffRowHeight;
[self.tableView reloadData];
}
}
}

#pragma mark UITableViewDatasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
return [self.rows count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
Expand All @@ -85,32 +145,36 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
TransformableTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[TransformableTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.finishedHeight = 88;
}
NSObject *object = [self.rows objectAtIndex:indexPath.row];
if ([object isEqual:ADDING_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 = ADDING_CELL;
}
cell.detailTextLabel.text = @" ";
} else {
cell.finishedHeight = NORMAL_CELL_FINISHING_HEIGHT;
cell.textLabel.text = [NSString stringWithFormat:@"%d %@", indexPath.row, (NSString *)object];
cell.detailTextLabel.text = @" ";
}

cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];
cell.detailTextLabel.text = @" ";

return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger index = [self.selectedIndexPaths indexOfObject:indexPath];
if (index != NSNotFound) {
return 88;
if ([indexPath isEqual:self.addingIndexPath]) {
return MAX(1, self.addingRowHeight);
}
return 36;
return NORMAL_CELL_FINISHING_HEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger index = [self.selectedIndexPaths indexOfObject:indexPath];
if (index != NSNotFound) {
[self.selectedIndexPaths removeObjectAtIndex:index];
} else {
[self.selectedIndexPaths addObject:indexPath];
}
[tableView beginUpdates];
[tableView endUpdates];
}
Expand Down

0 comments on commit a1f9ef5

Please sign in to comment.