Skip to content

Commit

Permalink
Add ScrollAction and ControllerTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
galenlin committed Jul 27, 2017
1 parent db33109 commit 9ade2c0
Show file tree
Hide file tree
Showing 30 changed files with 1,087 additions and 3 deletions.
14 changes: 14 additions & 0 deletions PBKit/PBAnimation/PBAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// PBAnimation.h
// Pods
//
// Created by galen on 17/7/24.
//
//

#import "PBFrameAnimation.h"

#import "PBScrollAction.h"
#import "PBScrollProgressAction.h"
#import "PBScrollTriggerAction.h"
#import "UIScrollView+PBScrollAction.h"
1 change: 0 additions & 1 deletion PBKit/PBAnimation/PBAnimationMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//

#import "PBAnimationMapper.h"
#import "Pbind+API.h"
#import "UIView+PBAnimation.h"

@implementation PBAnimationMapper
Expand Down
19 changes: 19 additions & 0 deletions PBKit/PBAnimation/PBFrameAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// PBFrameAnimation.h
// Pods
//
// Created by galen on 17/7/24.
//
//

#import <Foundation/Foundation.h>

@interface PBFrameAnimation : NSObject

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(CGFloat progress))animations;

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(CGFloat progress))animations completion:(void (^)(BOOL finished))completion;

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay animations:(void (^)(CGFloat progress))animations completion:(void (^)(BOOL finished))completion;

@end
81 changes: 81 additions & 0 deletions PBKit/PBAnimation/PBFrameAnimation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// PBFrameAnimation.m
// Pods
//
// Created by galen on 17/7/24.
//
//

#import "PBFrameAnimation.h"

@implementation PBFrameAnimation
{
NSTimeInterval _duration;
NSTimeInterval _delay;
CADisplayLink *_link;
CFTimeInterval _timestamp;
void (^_animations)(CGFloat progress);
void (^_complection)(BOOL finished);
}

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(CGFloat))animations {
[self animateWithDuration:duration animations:animations completion:nil];
}

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(CGFloat))animations completion:(void (^)(BOOL))completion {
[self animateWithDuration:duration delay:0.f animations:animations completion:completion];
}

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay animations:(void (^)(CGFloat progress))animations completion:(void (^)(BOOL finished))completion {
if (duration == 0) {
if (animations) {
animations(1.f);
}
if (completion) {
completion(YES);
}
return;
}

PBFrameAnimation *anim = [[PBFrameAnimation alloc] init];
anim->_duration = duration;
anim->_delay = delay;
anim->_animations = animations;
anim->_complection = completion;
[anim run];
}

- (instancetype)init {
if (self = [super init]) {
_link = [CADisplayLink displayLinkWithTarget:self selector:@selector(onDisplay:)];
}
return self;
}

- (void)run {
_timestamp = CACurrentMediaTime();
[_link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)onDisplay:(CADisplayLink *)link {
CGFloat progress = (CACurrentMediaTime() - _timestamp) / _duration;
NSLog(@"==== %.2f", progress);
BOOL complected = NO;
if (progress >= 1.f) {
progress = 1.f;
complected = YES;
[link invalidate];
}

if (_animations) {
_animations(progress);
}

if (complected) {
if (_complection) {
_complection(YES);
}
}
}

@end
14 changes: 14 additions & 0 deletions PBKit/PBAnimation/ScrollAction/NSNumber+PBScrollAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// NSNumber+PBScrollAction.h
// PBScrollMonitor
//
// Created by galen on 17/7/20.
// Copyright © 2017年 galen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "PBScrollActionProgressing.h"

@interface NSNumber (PBScrollAction) <PBScrollActionProgressing>

@end
23 changes: 23 additions & 0 deletions PBKit/PBAnimation/ScrollAction/NSNumber+PBScrollAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NSNumber+PBScrollAction.m
// PBScrollMonitor
//
// Created by galen on 17/7/20.
// Copyright © 2017年 galen. All rights reserved.
//

#import "NSNumber+PBScrollAction.h"

@implementation NSNumber (PBScrollAction)

+ (id)valueWithProgress:(double)progress fromValue:(id)fromValue toValue:(id)toValue {
if ([fromValue isEqualToNumber:toValue]) {
return fromValue;
}

double from = [fromValue doubleValue];
double to = [toValue doubleValue];
return @(from + progress * (to - from));
}

@end
29 changes: 29 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// PBScrollAction.h
// Pods
//
// Created by galen on 17/7/24.
//
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "PBScrollActionMapper.h"

@protocol PBScrollAction <NSObject>

- (BOOL)canRunActionWithScrollView:(UIScrollView *)scrollView;

- (void)actionWithScrollView:(UIScrollView *)scrollView;

@end

@interface PBScrollAction : NSObject <PBScrollAction>

+ (instancetype)actionWithMapper:(PBScrollActionMapper *)mapper;

@property (nonatomic, strong) id target;

@property (nonatomic, strong) NSString *keyPath;

@end
41 changes: 41 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// PBScrollAction.m
// Pods
//
// Created by galen on 17/7/24.
//
//

#import "PBScrollAction.h"
#import <Pbind/Pbind.h>

@implementation PBScrollAction
{
PBScrollActionMapper *_mapper;
}

+ (instancetype)actionWithMapper:(PBScrollActionMapper *)mapper {
PBScrollAction *action = [[mapper.actionClass alloc] init];
action->_mapper = mapper;
return action;
}

- (void)_internalActionWithScrollView:(UIScrollView *)scrollView {
if (_mapper != nil) {
[_mapper setPropertiesToObject:self transform:nil];
self.target = _mapper.target;
[_mapper mapPropertiesToObject:self withData:scrollView.rootData context:scrollView];
}

if ([self respondsToSelector:@selector(canRunActionWithScrollView:)]) {
if (![self canRunActionWithScrollView:scrollView]) {
return;
}
}

if ([self respondsToSelector:@selector(actionWithScrollView:)]) {
[self actionWithScrollView:scrollView];
}
}

@end
41 changes: 41 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollActionMapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// PBScrollActionMapper.h
// Pods
//
// Created by galen on 17/7/24.
//
//

#import <Pbind/Pbind.h>

@interface PBScrollActionMapper : PBMapper

#pragma mark - Plisting
///=============================================================================
/// @name Plisting
///=============================================================================

/**
The scroll action type, supports:
- progress (DEFAULT)
- evaluate
- trigger
which would dispatch the action to `PB[Type]ScrollAction'
*/
@property (nonatomic, strong) NSString *type;

/**
The action plist used to map properties to `PB[Type]ScrollAction'
*/
@property (nonatomic, strong) NSString *action;

@property (nonatomic, strong) id target;

#pragma mark - Caching
///=============================================================================
/// @name Caching
///=============================================================================

@property (nonatomic, weak) Class actionClass;

@end
36 changes: 36 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollActionMapper.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// PBScrollActionMapper.m
// Pods
//
// Created by galen on 17/7/24.
//
//

#import "PBScrollActionMapper.h"
#import "PBScrollProgressAction.h"

@implementation PBScrollActionMapper

- (void)setType:(NSString *)type {
_type = type;
if (type == nil || type.length < 2) {
_actionClass = nil;
return;
}

NSString *clazz = [NSString stringWithFormat:@"PBScroll%c%@Action", toupper([type characterAtIndex:0]), [type substringFromIndex:1]];
_actionClass = NSClassFromString(clazz);
}

- (void)setAction:(NSString *)action {
_viewProperties = [PBMapperProperties propertiesWithDictionary:PBPlist(action)];
}

- (Class)actionClass {
if (_actionClass != nil) {
return _actionClass;
}
return [PBScrollProgressAction class];
}

@end
15 changes: 15 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollActionProgressing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// PBScrollActionProgressing.h
// Pods
//
// Created by galen on 17/7/24.
//
//

#import <Foundation/Foundation.h>

@protocol PBScrollActionProgressing <NSObject>

+ (id)valueWithProgress:(double)progress fromValue:(id)fromValue toValue:(id)toValue;

@end
24 changes: 24 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollEvaluateAction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// PBScrollEvaluateAction.h
// Pods
//
// Created by galen on 17/7/24.
//
//

#import "PBScrollAction.h"

@interface PBScrollEvaluateAction : PBScrollAction

/**
The Javascript for evaluating the value for the key of the target.
@discussion The built-in variables are:
- offset, scrollView.contentOffset
- inset, scrollView.contentInsets
*/
@property (nonatomic, strong) NSString *value;

@end
21 changes: 21 additions & 0 deletions PBKit/PBAnimation/ScrollAction/PBScrollEvaluateAction.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// PBScrollEvaluateAction.m
// Pods
//
// Created by galen on 17/7/24.
//
//

#import "PBScrollEvaluateAction.h"

@implementation PBScrollEvaluateAction

- (BOOL)canRunActionWithScrollView:(UIScrollView *)scrollView {
return YES;
}

- (void)actionWithScrollView:(UIScrollView *)scrollView {

}

@end
Loading

0 comments on commit 9ade2c0

Please sign in to comment.