diff --git a/HHPanningTableViewCell/HHPanningTableViewCell.h b/HHPanningTableViewCell/HHPanningTableViewCell.h index f01f8ba..8dd3f0d 100644 --- a/HHPanningTableViewCell/HHPanningTableViewCell.h +++ b/HHPanningTableViewCell/HHPanningTableViewCell.h @@ -28,12 +28,22 @@ #import +@class HHPanningTableViewCell; + typedef enum { HHPanningTableViewCellDirectionRight = UISwipeGestureRecognizerDirectionRight, HHPanningTableViewCellDirectionLeft = UISwipeGestureRecognizerDirectionLeft, } HHPanningTableViewCellDirection; +@protocol HHPanningTableViewCellDelegate +@optional + +//If implemented this this will be triggered instead of fully revealing +- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction; + +@end + @interface HHPanningTableViewCell : UITableViewCell @@ -44,6 +54,7 @@ typedef enum { @property (nonatomic, assign) NSInteger directionMask; @property (nonatomic, assign) BOOL shouldBounce; +@property (nonatomic, strong) id delegate; - (BOOL)isDrawerRevealed; - (void)setDrawerRevealed:(BOOL)revealed animated:(BOOL)animated; diff --git a/HHPanningTableViewCell/HHPanningTableViewCell.m b/HHPanningTableViewCell/HHPanningTableViewCell.m index 5c9b0c4..4ce0715 100644 --- a/HHPanningTableViewCell/HHPanningTableViewCell.m +++ b/HHPanningTableViewCell/HHPanningTableViewCell.m @@ -36,9 +36,11 @@ #define HH_PANNING_ANIMATION_DURATION 0.1f -#define HH_PANNING_BOUNCE_DISTANCE 20.0f -#define HH_PANNING_MINIMUM_PAN 50.0f +#define HH_PANNING_BOUNCE_DISTANCE 10.0f +#define HH_PANNING_MINIMUM_PAN 1.0f +#define HH_PANNING_MAXIMUM_PAN 0.0f //Set to 0.0f for full view width #define HH_PANNING_TRIGGER_OFFSET 100.0f +#define HH_PANNING_USE_VELOCITY YES @interface HHPanningTableViewCell () @@ -418,7 +420,7 @@ static HHPanningTableViewCellDirection HHOppositeDirection(HHPanningTableViewCel containerViewFrame.origin.x = self.panOriginX + totalPanX; - CGFloat width = self.bounds.size.width; + CGFloat width = (HH_PANNING_MAXIMUM_PAN > 0.0f) ? HH_PANNING_MAXIMUM_PAN : self.bounds.size.width; NSInteger directionMask = self.directionMask; CGFloat leftLimit = (directionMask & HHPanningTableViewCellDirectionLeft) ? (-1.0 * width) : 0.0f; CGFloat rightLimit = (directionMask & HHPanningTableViewCellDirectionRight) ? width : 0.0f; @@ -443,6 +445,7 @@ static HHPanningTableViewCellDirection HHOppositeDirection(HHPanningTableViewCel HHPanningTableViewCellDirection panDirection = (totalPanX > 0.0f) ? HHPanningTableViewCellDirectionRight : HHPanningTableViewCellDirectionLeft; HHPanningTableViewCellDirection normalizedPanDirection = drawerRevealed ? HHOppositeDirection(panDirection) : panDirection; NSInteger directionMask = self.directionMask; + BOOL isDelegateTrigger = [self.delegate respondsToSelector:@selector(panningTableViewCellDidTrigger:inDirection:)]; if (drawerRevealed) { directionMask = isOffsetRight ? HHPanningTableViewCellDirectionRight : HHPanningTableViewCellDirectionLeft; @@ -454,7 +457,7 @@ static HHPanningTableViewCellDirection HHOppositeDirection(HHPanningTableViewCel if (fabsf(totalPanX) > triggerOffset) { drawerRevealed = !drawerRevealed; } - else { + else if (HH_PANNING_USE_VELOCITY) { CGPoint velocity = [gestureRecognizer velocityInView:self]; CGFloat velocityX = velocity.x; @@ -470,7 +473,13 @@ static HHPanningTableViewCellDirection HHOppositeDirection(HHPanningTableViewCel direction = isOffsetRight ? HHPanningTableViewCellDirectionRight : HHPanningTableViewCellDirectionLeft; } - [self setDrawerRevealed:drawerRevealed direction:direction animated:YES]; + if (isDelegateTrigger && (drawerRevealed != drawerWasRevealed)) { + [self setDrawerRevealed:NO direction:direction animated:YES]; + [self.delegate panningTableViewCellDidTrigger:self inDirection:panDirection]; + } + else { + [self setDrawerRevealed:drawerRevealed direction:direction animated:YES]; + } self.panning = NO; } diff --git a/HHPanningTableViewCell/TableViewController.h b/HHPanningTableViewCell/TableViewController.h index 10bb812..fdd8852 100644 --- a/HHPanningTableViewCell/TableViewController.h +++ b/HHPanningTableViewCell/TableViewController.h @@ -27,9 +27,10 @@ */ #import +#import "HHPanningTableViewCell.h" -@interface TableViewController : UITableViewController +@interface TableViewController : UITableViewController - (id)init; diff --git a/HHPanningTableViewCell/TableViewController.m b/HHPanningTableViewCell/TableViewController.m index 55597e8..24f4685 100644 --- a/HHPanningTableViewCell/TableViewController.m +++ b/HHPanningTableViewCell/TableViewController.m @@ -48,7 +48,7 @@ self = [super initWithNibName:@"TableViewController" bundle:nil]; if (self != nil) { - self.rowTitles = [NSArray arrayWithObjects:@"Pan direction: None", @"Pan direction: Right", @"Pan direction: Left", @"Pan direction: Both", nil]; + self.rowTitles = [NSArray arrayWithObjects:@"Pan direction: None", @"Pan direction: Right", @"Pan direction: Left", @"Pan direction: Both", @"Custom trigger", nil]; } return self; @@ -111,7 +111,14 @@ drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_dotted"]]; cell.drawerView = drawerView; - cell.directionMask = indexPath.row; + if (indexPath.row < 4) { + cell.directionMask = indexPath.row; + } + else { + cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight; + cell.delegate = self; + } + } cell.textLabel.text = [self.rowTitles objectAtIndex:indexPath.row]; @@ -142,4 +149,17 @@ { } +#pragma mark - +#pragma mark HHPanningTableViewCellDelegate + +- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction +{ + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Custom Action" + message:@"You triggered a custom action" + delegate:nil + cancelButtonTitle:@"OK" + otherButtonTitles:nil]; + [alert show]; +} + @end diff --git a/README.md b/README.md index 9307312..48424bb 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ The code presented here is identical to the one used in the shipped product. * Use HHPanningTableViewCell instances for your table cells * Set a panning direction mask on the cells * Provide a drawer view for the cells +* Optionally, implement the HHPanningTableViewCellDelegate method to trigger your own custom action. * Refer to the demo application for details ## License