Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from zhxnlai/refactoring-adding-rewind
Refactoring adding rewind
- Loading branch information
Showing
41 changed files
with
1,928 additions
and
740 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
BasedOnStyle: LLVM | ||
IndentWidth: 4 | ||
ObjCSpaceAfterProperty: true | ||
ColumnLimit: 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// DefaultDirectionInterpretor.h | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "ZLSwipeableView.h" | ||
|
||
@interface DefaultDirectionInterpretor : NSObject <ZLSwipeableViewDirectionInterpretor> | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// DefaultDirectionInterpretor.m | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import "DefaultDirectionInterpretor.h" | ||
|
||
@implementation DefaultDirectionInterpretor | ||
|
||
- (ZLSwipeableViewSwipeOptions *)interpretDirection:(ZLSwipeableViewDirection)direction | ||
view:(UIView *)view | ||
index:(NSUInteger)index | ||
views:(NSArray<UIView *> *)views | ||
swipeableView:(ZLSwipeableView *)swipeableView { | ||
CGFloat programaticSwipeVelocity = 1000; | ||
CGPoint location = CGPointMake(view.center.x, view.center.y * 0.7); | ||
CGVector directionVector; | ||
switch (direction) { | ||
case ZLSwipeableViewDirectionLeft: | ||
directionVector = CGVectorMake(-programaticSwipeVelocity, 0); | ||
break; | ||
case ZLSwipeableViewDirectionRight: | ||
directionVector = CGVectorMake(programaticSwipeVelocity, 0); | ||
break; | ||
case ZLSwipeableViewDirectionUp: | ||
directionVector = CGVectorMake(0, -programaticSwipeVelocity); | ||
break; | ||
case ZLSwipeableViewDirectionDown: | ||
directionVector = CGVectorMake(0, programaticSwipeVelocity); | ||
break; | ||
default: | ||
directionVector = CGVectorMake(0, 0); | ||
break; | ||
} | ||
return | ||
[[ZLSwipeableViewSwipeOptions alloc] initWithLocation:location direction:directionVector]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// DefaultShouldSwipeDeterminator.h | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "ZLSwipeableView.h" | ||
|
||
@interface DefaultShouldSwipeDeterminator : NSObject <ZLSwipeableViewSwipingDeterminator> | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// DefaultShouldSwipeDeterminator.m | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import "DefaultShouldSwipeDeterminator.h" | ||
#import "Utils.h" | ||
|
||
@implementation DefaultShouldSwipeDeterminator | ||
|
||
- (BOOL)shouldSwipeView:(UIView *)view | ||
movement:(ZLSwipeableViewMovement *)movement | ||
swipeableView:(ZLSwipeableView *)swipeableView { | ||
CGPoint translation = movement.translation; | ||
CGPoint velocity = movement.velocity; | ||
CGRect bounds = swipeableView.bounds; | ||
CGFloat minTranslationInPercent = swipeableView.minTranslationInPercent; | ||
CGFloat minVelocityInPointPerSecond = swipeableView.minVelocityInPointPerSecond; | ||
CGFloat allowedDirection = swipeableView.allowedDirection; | ||
|
||
return [self isDirectionAllowed:translation allowedDirection:allowedDirection] && | ||
[self isTranslation:translation inTheSameDirectionWithVelocity:velocity] && | ||
([self isTranslationLargeEnough:translation | ||
minTranslationInPercent:minTranslationInPercent | ||
bounds:bounds] || | ||
[self isVelocityLargeEnough:velocity | ||
minVelocityInPointPerSecond:minVelocityInPointPerSecond]); | ||
} | ||
|
||
- (BOOL)isTranslation:(CGPoint)p1 inTheSameDirectionWithVelocity:(CGPoint)p2 { | ||
return signNum(p1.x) == signNum(p2.x) && signNum(p1.y) == signNum(p2.y); | ||
} | ||
|
||
- (BOOL)isDirectionAllowed:(CGPoint)translation | ||
allowedDirection:(ZLSwipeableViewDirection)allowedDirection { | ||
return (ZLSwipeableViewDirectionFromPoint(translation) & allowedDirection) != | ||
ZLSwipeableViewDirectionNone; | ||
} | ||
|
||
- (BOOL)isTranslationLargeEnough:(CGPoint)translation | ||
minTranslationInPercent:(CGFloat)minTranslationInPercent | ||
bounds:(CGRect)bounds { | ||
return ABS(translation.x) > minTranslationInPercent * bounds.size.width || | ||
ABS(translation.y) > minTranslationInPercent * bounds.size.height; | ||
} | ||
|
||
- (BOOL)isVelocityLargeEnough:(CGPoint)velocity | ||
minVelocityInPointPerSecond:(CGFloat)minVelocityInPointPerSecond { | ||
return CGPointMagnitude(velocity) > minVelocityInPointPerSecond; | ||
} | ||
|
||
int signNum(CGFloat n) { return (n < 0) ? -1 : (n > 0) ? +1 : 0; } | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// DefaultViewAnimator.h | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "ZLSwipeableView.h" | ||
|
||
@interface DefaultViewAnimator : NSObject <ZLSwipeableViewAnimator> | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// DefaultViewAnimator.m | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import "DefaultViewAnimator.h" | ||
|
||
@implementation DefaultViewAnimator | ||
|
||
- (CGFloat)degreesToRadians:(CGFloat)degrees { | ||
return degrees * M_PI / 180; | ||
} | ||
|
||
- (CGFloat)radiansToDegrees:(CGFloat)radians { | ||
return radians * 180 / M_PI; | ||
} | ||
|
||
- (void)rotateView:(UIView *)view | ||
forDegree:(float)degree | ||
duration:(NSTimeInterval)duration | ||
atOffsetFromCenter:(CGPoint)offset | ||
swipeableView:(ZLSwipeableView *)swipeableView { | ||
float rotationRadian = [self degreesToRadians:degree]; | ||
[UIView animateWithDuration:duration | ||
delay:0 | ||
options:UIViewAnimationOptionAllowUserInteraction | ||
animations:^{ | ||
view.center = [swipeableView convertPoint:swipeableView.center | ||
fromView:swipeableView.superview]; | ||
CGAffineTransform transform = | ||
CGAffineTransformMakeTranslation(offset.x, offset.y); | ||
transform = CGAffineTransformRotate(transform, rotationRadian); | ||
transform = CGAffineTransformTranslate(transform, -offset.x, -offset.y); | ||
view.transform = transform; | ||
} | ||
completion:nil]; | ||
} | ||
|
||
- (void)animateView:(UIView *)view | ||
index:(NSUInteger)index | ||
views:(NSArray<UIView *> *)views | ||
swipeableView:(ZLSwipeableView *)swipeableView { | ||
CGFloat degree = 1; | ||
NSTimeInterval duration = 0.4; | ||
CGPoint offset = CGPointMake(0, CGRectGetHeight(swipeableView.bounds) * 0.3); | ||
switch (index) { | ||
case 0: | ||
[self rotateView:view | ||
forDegree:0 | ||
duration:duration | ||
atOffsetFromCenter:offset | ||
swipeableView:swipeableView]; | ||
break; | ||
case 1: | ||
[self rotateView:view | ||
forDegree:degree | ||
duration:duration | ||
atOffsetFromCenter:offset | ||
swipeableView:swipeableView]; | ||
break; | ||
case 2: | ||
[self rotateView:view | ||
forDegree:-degree | ||
duration:duration | ||
atOffsetFromCenter:offset | ||
swipeableView:swipeableView]; | ||
break; | ||
case 3: | ||
[self rotateView:view | ||
forDegree:0 | ||
duration:duration | ||
atOffsetFromCenter:offset | ||
swipeableView:swipeableView]; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// Scheduler.h | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
typedef void (^Action)(void); | ||
typedef BOOL (^EndCondition)(void); | ||
|
||
@interface Scheduler : NSObject | ||
|
||
@property (nonatomic, strong) NSTimer *timer; | ||
@property (nonatomic, strong) Action action; | ||
@property (nonatomic, strong) EndCondition endCondition; | ||
|
||
- (void)scheduleActionRepeatedly:(Action)action | ||
interval:(NSTimeInterval)interval | ||
endCondition:(EndCondition)endCondition; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Scheduler.m | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import "Scheduler.h" | ||
|
||
@implementation Scheduler | ||
|
||
- (void)scheduleActionRepeatedly:(Action)action | ||
interval:(NSTimeInterval)interval | ||
endCondition:(EndCondition)endCondition { | ||
if (self.timer != nil || interval <= 0) { | ||
return; | ||
} | ||
|
||
self.action = action; | ||
self.endCondition = endCondition; | ||
self.timer = [NSTimer scheduledTimerWithTimeInterval:interval | ||
target:self | ||
selector:@selector(doAction) | ||
userInfo:nil | ||
repeats:YES]; | ||
} | ||
|
||
- (void)doAction { | ||
if (!self.action || !self.endCondition || self.endCondition()) { | ||
[self.timer invalidate]; | ||
self.timer = nil; | ||
return; | ||
} | ||
self.action(); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// Utils.h | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import <UIKit/UIKit.h> | ||
#import "ZLSwipeableView.h" | ||
|
||
CGVector CGVectorFromCGPoint(CGPoint point); | ||
|
||
CGFloat CGPointMagnitude(CGPoint point); | ||
|
||
CGPoint CGPointNormalized(CGPoint point); | ||
|
||
CGPoint CGPointMultiply(CGPoint point, CGFloat factor); | ||
|
||
ZLSwipeableViewDirection ZLSwipeableViewDirectionFromVector(CGVector directionVector); | ||
|
||
ZLSwipeableViewDirection ZLSwipeableViewDirectionFromPoint(CGPoint point); | ||
|
||
@interface Utils : NSObject | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// Utils.m | ||
// ZLSwipeableViewDemo | ||
// | ||
// Created by Zhixuan Lai on 10/25/15. | ||
// Copyright © 2015 Zhixuan Lai. All rights reserved. | ||
// | ||
|
||
#import "Utils.h" | ||
|
||
CGVector CGVectorFromCGPoint(CGPoint point) { return CGVectorMake(point.x, point.y); } | ||
|
||
CGFloat CGPointMagnitude(CGPoint point) { return sqrtf(powf(point.x, 2) + powf(point.y, 2)); } | ||
|
||
CGPoint CGPointNormalized(CGPoint point) { | ||
CGFloat magnitude = CGPointMagnitude(point); | ||
return CGPointMake(point.x / magnitude, point.y / magnitude); | ||
} | ||
|
||
CGPoint CGPointMultiply(CGPoint point, CGFloat factor) { | ||
return CGPointMake(point.x * factor, point.y * factor); | ||
} | ||
|
||
ZLSwipeableViewDirection ZLSwipeableViewDirectionFromVector(CGVector directionVector) { | ||
ZLSwipeableViewDirection direction = ZLSwipeableViewDirectionNone; | ||
if (ABS(directionVector.dx) > ABS(directionVector.dy)) { | ||
if (directionVector.dx > 0) { | ||
direction = ZLSwipeableViewDirectionRight; | ||
} else { | ||
direction = ZLSwipeableViewDirectionLeft; | ||
} | ||
} else { | ||
if (directionVector.dy > 0) { | ||
direction = ZLSwipeableViewDirectionDown; | ||
} else { | ||
direction = ZLSwipeableViewDirectionUp; | ||
} | ||
} | ||
|
||
return direction; | ||
} | ||
|
||
ZLSwipeableViewDirection ZLSwipeableViewDirectionFromPoint(CGPoint point) { | ||
return ZLSwipeableViewDirectionFromVector(CGVectorFromCGPoint(point)); | ||
} | ||
|
||
@implementation Utils | ||
|
||
@end |
Oops, something went wrong.