From c75f1685c51dc2cf62aafeba4c3ebe787070a1d9 Mon Sep 17 00:00:00 2001 From: cacaodev Date: Mon, 30 Mar 2015 23:37:15 +0200 Subject: [PATCH] _CPObjectAnimator & CPViewAnimator Make subclassing easier. For a specific animator, can choose the implementation to invoke by subclassing - (id) forwardingTargetForSelector: The pattern for the animator class, if no -(id)animator is implemented, is now Animator by default and it searches for a Animator if necessary. NEW: Added support for CPAnimationTriggerOrderIn|Out animated key. --- AppKit/CoreAnimation/CPViewAnimator.j | 49 ++++++++++++++++++++++-- AppKit/CoreAnimation/_CPObjectAnimator.j | 17 ++++---- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/AppKit/CoreAnimation/CPViewAnimator.j b/AppKit/CoreAnimation/CPViewAnimator.j index e9ef83098..0a2eecd20 100644 --- a/AppKit/CoreAnimation/CPViewAnimator.j +++ b/AppKit/CoreAnimation/CPViewAnimator.j @@ -8,6 +8,36 @@ { } +- (void)viewWillMoveToSuperview:(CPView)aSuperview +{ + var orderInAnim = [self animationForKey:@"CPAnimationTriggerOrderIn"]; + + if (orderInAnim && [orderInAnim isKindOfClass:[CAPropertyAnimation class]]) + { + [_target setValue:[orderInAnim fromValue] forKeyPath:[orderInAnim keyPath]]; + } + + [_target viewWillMoveToSuperview:aSuperview]; +} + +- (void)viewDidMoveToSuperview +{ + var orderInAnim = [self animationForKey:@"CPAnimationTriggerOrderIn"]; + + if (orderInAnim && [orderInAnim isKindOfClass:[CAPropertyAnimation class]]) + { + [self _setTargetValue:YES withKeyPath:@"CPAnimationTriggerOrderIn" fallback:nil completion:function() + { + [_target setValue:[orderInAnim toValue] forKeyPath:[orderInAnim keyPath]]; + }]; + } + else + { + [_target viewDidMoveToSuperview]; + } + +} + - (void)removeFromSuperview { [self _setTargetValue:nil withKeyPath:@"CPAnimationTriggerOrderOut" setter:_cmd]; @@ -61,6 +91,7 @@ } - (void)_setTargetValue:(id)aTargetValue withKeyPath:(CPString)aKeyPath fallback:(Function)fallback completion:(Function)completion +- (void)_setTargetValue:(id)aTargetValue withKeyPath:(CPString)aKeyPath fallback:(Function)fallback completion:(Function)completion { var animation = [_target animationForKey:aKeyPath], context = [CPAnimationContext currentContext]; @@ -126,22 +157,32 @@ var CPVIEW_PROPERTIES_DESCRIPTOR = @{ return [CPVIEW_PROPERTIES_DESCRIPTOR objectForKey:aKeyPath]; } -- (id)DOMElementForKeyPath:(CPString)aKeyPath ++ (Class)animatorClass { - return _DOMElement; + var anim_class = CPClassFromString(CPStringFromClass(self) + "Animator"); + + if (anim_class) + return anim_class; + + return [super animatorClass]; } - (id)animator { if (!_animator) - _animator = [[CPViewAnimator alloc] initWithTarget:self]; + _animator = [[[[self class] animatorClass] alloc] initWithTarget:self]; return _animator; } +- (id)DOMElementForKeyPath:(CPString)aKeyPath +{ + return _DOMElement; +} + + (CAAnimation)defaultAnimationForKey:(CPString)aKey { - if ([[self class] cssPropertiesForKeyPath:aKey] !== nil) + if ([self cssPropertiesForKeyPath:aKey] !== nil) return [CAAnimation animation]; return nil; diff --git a/AppKit/CoreAnimation/_CPObjectAnimator.j b/AppKit/CoreAnimation/_CPObjectAnimator.j index 0e04ae2d3..2e82e1bc0 100644 --- a/AppKit/CoreAnimation/_CPObjectAnimator.j +++ b/AppKit/CoreAnimation/_CPObjectAnimator.j @@ -47,25 +47,26 @@ var _supportsCSSAnimations = null; return [_target isEqual:anObject]; } -- (CPMethodSignature)methodSignatureForSelector:(SEL)aSelector -{ - return [_target methodSignatureForSelector:aSelector]; -} - - (id)forwardingTargetForSelector:(SEL)aSelector { return _target; } +- (CPMethodSignature)methodSignatureForSelector:(SEL)aSelector +{ + return [_target methodSignatureForSelector:aSelector]; +} + - (void)forwardInvocation:(CPInvocation)anInvocation { - [anInvocation setTarget:_target]; - [anInvocation invoke]; + var target = [self forwardingTargetForSelector:[anInvocation selector]]; + [anInvocation invokeWithTarget:target]; + return; } - (void)doesNotRecognizeSelector:(SEL)aSelector { - [_target doesNotRecognizeSelector:aSelector]; + [CPException raise:CPInvalidArgumentException reason:@"Animator does not recognize selector " + CPStringFromSelector(aSelector)]; } - (CPString)description