Created
May 19, 2013 15:13
-
-
Save bendytree/5607941 to your computer and use it in GitHub Desktop.
Adds an animation to a view. Then monitors `viewWillAppear` and `appWillEnterForeground` to re-add the animation whenever necessary.
This file contains hidden or 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
| #import <UIKit/UIKit.h> | |
| @interface UIViewController (AlwaysAnimation) | |
| - (void) alwaysAnimate:(CABasicAnimation*)animation view:(UIView*)view; | |
| @end |
This file contains hidden or 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
| // | |
| // UIViewController+animation.m | |
| // Speaky | |
| // | |
| // Created by Joshua Wright on 5/19/13. | |
| // Copyright (c) 2013 Joshua Wright. All rights reserved. | |
| // | |
| #import "UIViewController+AlwaysAnimation.h" | |
| #import <QuartzCore/QuartzCore.h> | |
| #import <objc/runtime.h> | |
| @interface AlwaysAnimation : NSObject | |
| @property (retain) CABasicAnimation* animation; | |
| @property (weak) UIView* view; | |
| - (void) addAnimation; | |
| @end | |
| @implementation AlwaysAnimation | |
| - (void) addAnimation | |
| { | |
| [self.view.layer addAnimation:self.animation forKey:@"alwaysAnimated"]; | |
| } | |
| @end | |
| @implementation UIViewController (AlwaysAnimation) | |
| - (void) reAddAlwaysAnimations | |
| { | |
| NSMutableArray* animations = objc_getAssociatedObject(self, "alwaysAnimations"); | |
| for(AlwaysAnimation* animation in animations){ | |
| [animation addAnimation]; | |
| } | |
| } | |
| - (void) viewWillAppear_AlwaysAnimation:(BOOL)animated | |
| { | |
| [self viewWillAppear_AlwaysAnimation:animated]; | |
| [self reAddAlwaysAnimations]; | |
| } | |
| - (void) viewDidUnload_AlwaysAnimation | |
| { | |
| [self viewDidUnload_AlwaysAnimation]; | |
| [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| objc_setAssociatedObject(self, "alwaysAnimations", nil, OBJC_ASSOCIATION_RETAIN); | |
| } | |
| - (void) alwaysAnimate:(CABasicAnimation*)animation view:(UIView*)view | |
| { | |
| NSMutableArray* animations = objc_getAssociatedObject(self, "alwaysAnimations"); | |
| if(animations == nil) | |
| { | |
| //viewWillAppear | |
| method_exchangeImplementations( | |
| class_getInstanceMethod([self class], @selector(viewWillAppear:)), | |
| class_getInstanceMethod([self class], @selector(viewWillAppear_AlwaysAnimation:))); | |
| //viewDidUnload | |
| method_exchangeImplementations( | |
| class_getInstanceMethod([self class], @selector(viewDidUnload)), | |
| class_getInstanceMethod([self class], @selector(viewDidUnload_AlwaysAnimation:))); | |
| if(animations == nil){ | |
| animations = [NSMutableArray array]; | |
| objc_setAssociatedObject(self, "alwaysAnimations", animations, OBJC_ASSOCIATION_RETAIN); | |
| } | |
| //appEnterForeground | |
| [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reAddAlwaysAnimations) name:UIApplicationWillEnterForegroundNotification object:nil]; | |
| } | |
| AlwaysAnimation* alwaysAnimation = [[AlwaysAnimation alloc] init]; | |
| alwaysAnimation.view = view; | |
| alwaysAnimation.animation = animation; | |
| [animations addObject:alwaysAnimation]; | |
| [alwaysAnimation addAnimation]; | |
| } | |
| @end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment