mirror of
https://github.com/zhigang1992/HHPanningTableViewCell.git
synced 2026-01-12 09:04:24 +08:00
Optional custom action trigger. Optional velocity. Optional max pan distance.
This commit is contained in:
@@ -28,12 +28,22 @@
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@class HHPanningTableViewCell;
|
||||
|
||||
|
||||
typedef enum {
|
||||
HHPanningTableViewCellDirectionRight = UISwipeGestureRecognizerDirectionRight,
|
||||
HHPanningTableViewCellDirectionLeft = UISwipeGestureRecognizerDirectionLeft,
|
||||
} HHPanningTableViewCellDirection;
|
||||
|
||||
@protocol HHPanningTableViewCellDelegate <NSObject>
|
||||
@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<HHPanningTableViewCellDelegate> delegate;
|
||||
|
||||
- (BOOL)isDrawerRevealed;
|
||||
- (void)setDrawerRevealed:(BOOL)revealed animated:(BOOL)animated;
|
||||
|
||||
@@ -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 () <UIGestureRecognizerDelegate>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -27,9 +27,10 @@
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "HHPanningTableViewCell.h"
|
||||
|
||||
|
||||
@interface TableViewController : UITableViewController
|
||||
@interface TableViewController : UITableViewController <HHPanningTableViewCellDelegate>
|
||||
|
||||
- (id)init;
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user