Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
Various code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
alikaragoz committed Jan 15, 2014
1 parent 6fde4ec commit f5c5268
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 46 deletions.
81 changes: 42 additions & 39 deletions MCSwipeTableViewCell/MCSwipeTableViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,52 @@ typedef NS_ENUM(NSUInteger, MCSwipeTableViewCellMode) {

typedef void (^MCSwipeCompletionBlock)(MCSwipeTableViewCell *cell, MCSwipeTableViewCellState state, MCSwipeTableViewCellMode mode);

@protocol MCSwipeTableViewCellDelegate <NSObject>

@optional

// When the user starts swiping the cell this method is called
- (void)swipeTableViewCellDidStartSwiping:(MCSwipeTableViewCell *)cell;

// When the user ends swiping the cell this method is called
- (void)swipeTableViewCellDidEndSwiping:(MCSwipeTableViewCell *)cell;

// When the user is dragging, this method is called and return the dragged percentage from the border
- (void)swipeTableViewCell:(MCSwipeTableViewCell *)cell didSwipWithPercentage:(CGFloat)percentage;

@end
@protocol MCSwipeTableViewCellDelegate;

@interface MCSwipeTableViewCell : UITableViewCell

@property (nonatomic, assign) id <MCSwipeTableViewCellDelegate> delegate;

@property (nonatomic, strong) UIColor *firstColor;
@property (nonatomic, strong) UIColor *secondColor;
@property (nonatomic, strong) UIColor *thirdColor;
@property (nonatomic, strong) UIColor *fourthColor;
@property (nonatomic, strong, readwrite) UIColor *firstColor;
@property (nonatomic, strong, readwrite) UIColor *secondColor;
@property (nonatomic, strong, readwrite) UIColor *thirdColor;
@property (nonatomic, strong, readwrite) UIColor *fourthColor;

@property (nonatomic, strong) UIView *view1;
@property (nonatomic, strong) UIView *view2;
@property (nonatomic, strong) UIView *view3;
@property (nonatomic, strong) UIView *view4;
@property (nonatomic, strong, readwrite) UIView *view1;
@property (nonatomic, strong, readwrite) UIView *view2;
@property (nonatomic, strong, readwrite) UIView *view3;
@property (nonatomic, strong, readwrite) UIView *view4;

@property (nonatomic, copy) MCSwipeCompletionBlock completionBlock1;
@property (nonatomic, copy) MCSwipeCompletionBlock completionBlock2;
@property (nonatomic, copy) MCSwipeCompletionBlock completionBlock3;
@property (nonatomic, copy) MCSwipeCompletionBlock completionBlock4;
@property (nonatomic, copy, readwrite) MCSwipeCompletionBlock completionBlock1;
@property (nonatomic, copy, readwrite) MCSwipeCompletionBlock completionBlock2;
@property (nonatomic, copy, readwrite) MCSwipeCompletionBlock completionBlock3;
@property (nonatomic, copy, readwrite) MCSwipeCompletionBlock completionBlock4;

// Percentage of when the first and second action are activated, respectively
@property (nonatomic, assign) CGFloat firstTrigger;
@property (nonatomic, assign) CGFloat secondTrigger;
@property (nonatomic, assign, readwrite) CGFloat firstTrigger;
@property (nonatomic, assign, readwrite) CGFloat secondTrigger;

// Damping of the spring animation (iOS 7 only)
@property (nonatomic, assign) CGFloat damping;
@property (nonatomic, assign, readwrite) CGFloat damping;

// Velocity of the spring animation (iOS 7 only)
@property (nonatomic, assign) CGFloat velocity;
@property (nonatomic, assign, readwrite) CGFloat velocity;

// Duration of the animation.
@property (nonatomic, assign) NSTimeInterval animationDuration;
@property (nonatomic, assign, readwrite) NSTimeInterval animationDuration;

// Color for background, when any state hasn't triggered yet
@property (nonatomic, strong) UIColor *defaultColor;
@property (nonatomic, strong, readwrite) UIColor *defaultColor;

// Individual mode for states
@property (nonatomic, assign) MCSwipeTableViewCellMode modeForState1;
@property (nonatomic, assign) MCSwipeTableViewCellMode modeForState2;
@property (nonatomic, assign) MCSwipeTableViewCellMode modeForState3;
@property (nonatomic, assign) MCSwipeTableViewCellMode modeForState4;
@property (nonatomic, assign, readwrite) MCSwipeTableViewCellMode modeForState1;
@property (nonatomic, assign, readwrite) MCSwipeTableViewCellMode modeForState2;
@property (nonatomic, assign, readwrite) MCSwipeTableViewCellMode modeForState3;
@property (nonatomic, assign, readwrite) MCSwipeTableViewCellMode modeForState4;

@property (nonatomic, assign) BOOL isDragging;
@property (nonatomic, assign) BOOL shouldDrag;
@property (nonatomic, assign) BOOL shouldAnimatesIcons;
@property (nonatomic, assign, readonly) BOOL isDragging;
@property (nonatomic, assign, readwrite) BOOL shouldDrag;
@property (nonatomic, assign, readwrite) BOOL shouldAnimatesIcons;

- (void)setSwipeGestureWithView:(UIView *)view
color:(UIColor *)color
Expand All @@ -102,3 +89,19 @@ typedef void (^MCSwipeCompletionBlock)(MCSwipeTableViewCell *cell, MCSwipeTableV
- (void)swipeToOriginWithCompletion:(void(^)(void))completion;

@end


@protocol MCSwipeTableViewCellDelegate <NSObject>

@optional

// When the user starts swiping the cell this method is called
- (void)swipeTableViewCellDidStartSwiping:(MCSwipeTableViewCell *)cell;

// When the user ends swiping the cell this method is called
- (void)swipeTableViewCellDidEndSwiping:(MCSwipeTableViewCell *)cell;

// When the user is dragging, this method is called and return the dragged percentage from the border
- (void)swipeTableViewCell:(MCSwipeTableViewCell *)cell didSwipWithPercentage:(CGFloat)percentage;

@end
44 changes: 37 additions & 7 deletions MCSwipeTableViewCell/MCSwipeTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ @interface MCSwipeTableViewCell () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIView *slidingView;
@property (nonatomic, strong) UIView *activeView;

// Initialization
- (void)initializer;
- (void)initDefaults;

// View Manipulation.
- (void)setupSwipingView;
- (void)uninstallSwipingView;
- (void)setViewOfSlidingView:(UIView *)view;

// Percentage
- (CGFloat)offsetWithPercentage:(CGFloat)percentage relativeToWidth:(CGFloat)width;
- (CGFloat)percentageWithOffset:(CGFloat)offset relativeToWidth:(CGFloat)width;
- (NSTimeInterval)animationDurationWithVelocity:(CGPoint)velocity;
- (MCSwipeTableViewCellDirection)directionWithPercentage:(CGFloat)percentage;
- (UIView *)viewWithPercentage:(CGFloat)percentage;
- (CGFloat)alphaWithPercentage:(CGFloat)percentage;
- (UIColor *)colorWithPercentage:(CGFloat)percentage;
- (MCSwipeTableViewCellState)stateWithPercentage:(CGFloat)percentage;

// Movement
- (void)animateWithOffset:(CGFloat)offset;
- (void)slideViewWithPercentage:(CGFloat)percentage view:(UIView *)view isDragging:(BOOL)isDragging;
- (void)moveWithDuration:(NSTimeInterval)duration andDirection:(MCSwipeTableViewCellDirection)direction;

// Utilities
- (UIImage *)imageWithView:(UIView *)view;

// Delegate Notification.
- (void)notifyDelegate;

@end

@implementation MCSwipeTableViewCell
Expand Down Expand Up @@ -65,7 +95,7 @@ - (void)initializer {

[self initDefaults];

// Setup Gesture Reco
// Setup Gesture Recognizer.
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestureRecognizer:)];
[self addGestureRecognizer:_panGestureRecognizer];
_panGestureRecognizer.delegate = self;
Expand Down Expand Up @@ -248,7 +278,7 @@ - (void)handlePanGestureRecognizer:(UIPanGestureRecognizer *)gesture {
[self animateWithOffset:CGRectGetMinX(_contentScreenshotView.frame)];
[gesture setTranslation:CGPointZero inView:self];

// Notifying the delegate that we are dragging with an offset percentage
// Notifying the delegate that we are dragging with an offset percentage.
if ([_delegate respondsToSelector:@selector(swipeTableViewCell:didSwipWithPercentage:)]) {
[_delegate swipeTableViewCell:self didSwipWithPercentage:percentage];
}
Expand Down Expand Up @@ -466,7 +496,7 @@ - (void)animateWithOffset:(CGFloat)offset {

UIView *view = [self viewWithPercentage:percentage];

// View Position
// View Position.
if (view) {
[self setViewOfSlidingView:view];
_slidingView.alpha = [self alphaWithPercentage:percentage];
Expand Down Expand Up @@ -604,7 +634,7 @@ - (void)swipeToOriginWithCompletion:(void(^)(void))completion {
_slidingView.alpha = 0;
[self slideViewWithPercentage:0 view:_activeView isDragging:NO];

// Setting back the color to the default
// Setting back the color to the default.
_colorIndicatorView.backgroundColor = self.defaultColor;

} completion:^(BOOL finished1) {
Expand Down Expand Up @@ -637,9 +667,9 @@ - (UIImage *)imageWithView:(UIView *)view {
short scale = [[UIScreen mainScreen] scale];
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, scale);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
return image;
}

#pragma mark - Delegate Notification
Expand Down Expand Up @@ -674,7 +704,7 @@ - (void)notifyDelegate {
break;
}

// We notify the delegate that we just ended dragging
// We notify the delegate that we just ended dragging.
if ([_delegate respondsToSelector:@selector(swipeTableViewCellDidEndSwiping:)]) {
[_delegate swipeTableViewCellDidEndSwiping:self];
}
Expand Down

0 comments on commit f5c5268

Please sign in to comment.