Skip to content

Commit

Permalink
Add support for partial reveals of variable width.
Browse files Browse the repository at this point in the history
Tweak the bounce animation a bit.
  • Loading branch information
chadpod committed Jul 19, 2012
1 parent fd45530 commit 730d487
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
1 change: 1 addition & 0 deletions vendor/ZKRevealingTableViewCell.h
Expand Up @@ -51,5 +51,6 @@ typedef enum {
@property (nonatomic, assign) id <ZKRevealingTableViewCellDelegate> delegate;
@property (nonatomic, assign) ZKRevealingTableViewCellDirection direction;
@property (nonatomic, assign) BOOL shouldBounce;
@property CGFloat pixelsToReveal;

@end
90 changes: 69 additions & 21 deletions vendor/ZKRevealingTableViewCell.m
Expand Up @@ -67,6 +67,7 @@ @implementation ZKRevealingTableViewCell
@synthesize direction = _direction;
@synthesize delegate = _delegate;
@synthesize shouldBounce = _shouldBounce;
@synthesize pixelsToReveal = _pixelsToReveal;
@synthesize backView = _backView;

#pragma mark - Lifecycle
Expand All @@ -77,16 +78,39 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
if (self) {
self.direction = ZKRevealingTableViewCellDirectionBoth;
self.shouldBounce = YES;
self.pixelsToReveal = 0;

self._panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_pan:)] autorelease];
self._panGesture.delegate = self;

[self addGestureRecognizer:self._panGesture];

self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.backgroundColor = [UIColor clearColor];

UIView *backgroundView = [[[UIView alloc] initWithFrame:self.contentView.frame] autorelease];
backgroundView.backgroundColor = [UIColor greenColor];
backgroundView.backgroundColor = [UIColor clearColor];
self.backView = backgroundView;
}
return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
self.direction = ZKRevealingTableViewCellDirectionBoth;
self.shouldBounce = YES;
self.pixelsToReveal = 0;

self._panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(_pan:)] autorelease];
self._panGesture.delegate = self;

[self addGestureRecognizer:self._panGesture];

self.contentView.backgroundColor = [UIColor clearColor];

UIView *backgroundView = [[[UIView alloc] initWithFrame:self.contentView.frame] autorelease];
backgroundView.backgroundColor = [UIColor clearColor];
self.backView = backgroundView;
}
return self;
Expand Down Expand Up @@ -187,12 +211,21 @@ - (void)_pan:(UIPanGestureRecognizer *)recognizer
if ((newCenterPosition < originalCenter && !self._shouldDragLeft) || (newCenterPosition > originalCenter && !self._shouldDragRight))
newCenterPosition = originalCenter;

// Let's not go waaay out of bounds
if (newCenterPosition > self.bounds.size.width + originalCenter)
newCenterPosition = self.bounds.size.width + originalCenter;

else if (newCenterPosition < -originalCenter)
newCenterPosition = -originalCenter;
if (self.pixelsToReveal != 0) {
// Let's not go waaay out of bounds
if (newCenterPosition > originalCenter + self.pixelsToReveal)
newCenterPosition = originalCenter + self.pixelsToReveal;

else if (newCenterPosition < originalCenter - self.pixelsToReveal)
newCenterPosition = originalCenter - self.pixelsToReveal;
}else {
// Let's not go waaay out of bounds
if (newCenterPosition > self.bounds.size.width + originalCenter)
newCenterPosition = self.bounds.size.width + originalCenter;

else if (newCenterPosition < -originalCenter)
newCenterPosition = -originalCenter;
}

CGPoint center = self.contentView.center;
center.x = newCenterPosition;
Expand Down Expand Up @@ -272,7 +305,7 @@ - (CGFloat)_bounceMultiplier
}

#pragma mark - Sliding
#define kBOUNCE_DISTANCE 20.0
#define kBOUNCE_DISTANCE 7.0

- (void)_slideInContentViewFromDirection:(ZKRevealingTableViewCellDirection)direction offsetMultiplier:(CGFloat)multiplier
{
Expand Down Expand Up @@ -301,12 +334,12 @@ - (void)_slideInContentViewFromDirection:(ZKRevealingTableViewCellDirection)dire
completion:^(BOOL f) {

[UIView animateWithDuration:0.1 delay:0
options:UIViewAnimationCurveLinear
options:UIViewAnimationCurveEaseOut
animations:^{ self.contentView.frame = CGRectOffset(self.contentView.frame, bounceDistance, 0); }
completion:^(BOOL f) {

[UIView animateWithDuration:0.1 delay:0
options:UIViewAnimationCurveLinear
options:UIViewAnimationCurveEaseIn
animations:^{ self.contentView.frame = CGRectOffset(self.contentView.frame, -bounceDistance, 0); }
completion:NULL];
}
Expand All @@ -318,16 +351,31 @@ - (void)_slideOutContentViewInDirection:(ZKRevealingTableViewCellDirection)direc
{
CGFloat x;

switch (direction) {
case ZKRevealingTableViewCellDirectionLeft:
x = - self._originalCenter;
break;
case ZKRevealingTableViewCellDirectionRight:
x = self.contentView.frame.size.width + self._originalCenter;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:direction] forKey:@"direction"]];
break;
if (self.pixelsToReveal != 0) {
switch (direction) {
case ZKRevealingTableViewCellDirectionLeft:
x = self._originalCenter - self.pixelsToReveal;
break;
case ZKRevealingTableViewCellDirectionRight:
x = self._originalCenter + self.pixelsToReveal;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:direction] forKey:@"direction"]];
break;
}
}
else {
switch (direction) {
case ZKRevealingTableViewCellDirectionLeft:
x = - self._originalCenter;
break;
case ZKRevealingTableViewCellDirectionRight:
x = self.contentView.frame.size.width + self._originalCenter;
break;
default:
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Unhandled gesture direction" userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:direction] forKey:@"direction"]];
break;
}
}

[UIView animateWithDuration:0.2
Expand Down

0 comments on commit 730d487

Please sign in to comment.