Last active
December 31, 2015 17:49
-
-
Save xdream86/8023210 to your computer and use it in GitHub Desktop.
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
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController | |
@property (nonatomic, strong) CABasicAnimation *animationCopy; | |
@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
#import "ViewController.h" | |
@interface ViewController () | |
@property (weak, nonatomic) IBOutlet UIView *square; | |
@end | |
@implementation ViewController | |
/** | |
* 注册进出前后台观察者 | |
*/ | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(appWillResignActive:) | |
name:UIApplicationWillResignActiveNotification | |
object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self | |
selector:@selector(appWillEnterForeground:) | |
name:UIApplicationWillEnterForegroundNotification | |
object:nil]; | |
} | |
/** | |
* 待入后台之时备份当前动画,并暂停图层动画 | |
*/ | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
-(void)appWillResignActive:(NSNotification*)note { | |
self.animationCopy = [[_square.layer animationForKey:@"movingAnimation"] copy]; | |
[self pauseLayer:_square.layer]; | |
} | |
/** | |
* 取出备份动画,加入图层继续运行 | |
*/ | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
-(void)appWillEnterForeground:(NSNotification*)note { | |
if (self.animationCopy != nil) { | |
[_square.layer addAnimation:self.animationCopy forKey:@"movingAnimation"]; // re-add the core animation to the view | |
self.animationCopy = nil; | |
} | |
[self resumeLayer:_square.layer]; | |
} | |
/** | |
* 将正方形视图从上至下移动 | |
*/ | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (IBAction)startButtonPressed:(id)sender { | |
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"]; | |
animation.toValue = [NSNumber numberWithInt:350]; | |
animation.duration = 10; | |
[self.square.layer addAnimation:animation forKey:@"movingAnimation"]; | |
} | |
/** | |
* from: https://developer.apple.com/library/ios/qa/qa1673/_index.html#//apple_ref/doc/uid/DTS40010053 | |
*/ | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
-(void)pauseLayer:(CALayer*)layer { | |
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; | |
layer.speed = 0.0; | |
layer.timeOffset = pausedTime; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
-(void)resumeLayer:(CALayer*)layer { | |
CFTimeInterval pausedTime = [layer timeOffset]; | |
layer.speed = 1.0; | |
layer.timeOffset = 0.0; | |
layer.beginTime = 0.0; | |
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime; | |
layer.beginTime = timeSincePause; | |
} | |
/////////////////////////////////////////////////////////////////////////////////////////////////// | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment