Skip to content

Commit

Permalink
Merge pull request #56 from zhxnlai/refactoring-adding-rewind
Browse files Browse the repository at this point in the history
Refactoring adding rewind
  • Loading branch information
zhxnlai committed Oct 26, 2015
2 parents 5c3ca69 + 600c7fe commit 8a6ce99
Show file tree
Hide file tree
Showing 41 changed files with 1,928 additions and 740 deletions.
1 change: 1 addition & 0 deletions .clang-format
Expand Up @@ -2,3 +2,4 @@
BasedOnStyle: LLVM
IndentWidth: 4
ObjCSpaceAfterProperty: true
ColumnLimit: 100
14 changes: 14 additions & 0 deletions ZLSwipeableView/DefaultDirectionInterpretor.h
@@ -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
42 changes: 42 additions & 0 deletions ZLSwipeableView/DefaultDirectionInterpretor.m
@@ -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
14 changes: 14 additions & 0 deletions ZLSwipeableView/DefaultShouldSwipeDeterminator.h
@@ -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
57 changes: 57 additions & 0 deletions ZLSwipeableView/DefaultShouldSwipeDeterminator.m
@@ -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
14 changes: 14 additions & 0 deletions ZLSwipeableView/DefaultViewAnimator.h
@@ -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
83 changes: 83 additions & 0 deletions ZLSwipeableView/DefaultViewAnimator.m
@@ -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
Expand Up @@ -8,6 +8,6 @@

#import <UIKit/UIKit.h>

@interface ZLPanGestureRecognizer : UIPanGestureRecognizer
@interface PanGestureRecognizer : UIPanGestureRecognizer

@end
Expand Up @@ -6,8 +6,8 @@
// Copyright (c) 2014 Zhixuan Lai. All rights reserved.
//

#import "ZLPanGestureRecognizer.h"
#import "PanGestureRecognizer.h"

@implementation ZLPanGestureRecognizer
@implementation PanGestureRecognizer

@end
24 changes: 24 additions & 0 deletions ZLSwipeableView/Scheduler.h
@@ -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
38 changes: 38 additions & 0 deletions ZLSwipeableView/Scheduler.m
@@ -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
26 changes: 26 additions & 0 deletions ZLSwipeableView/Utils.h
@@ -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
49 changes: 49 additions & 0 deletions ZLSwipeableView/Utils.m
@@ -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

0 comments on commit 8a6ce99

Please sign in to comment.