如何在主页button被按下后在背景中animationCABasicAnimation?
我是ios开发新手。 我在我的项目中使用轮子图像。animation在前景模式下工作正常。 之后,我按下了主页button.now我重新启动应用程序轮animation无法正常工作。 这是我的代码:
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; animation.duration = 1.0f; animation.repeatCount = INFINITY; [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"];
尝试这个,
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addAnimation:) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void)addAnimation:(NSNotification *)notificaiton { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat: 2*M_PI]; animation.toValue = [NSNumber numberWithFloat:0.0f]; animation.duration = 4.0f; animation.repeatCount = INFINITY; [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"]; [imageRight.layer addAnimation:animation forKey:@"SpinAnimation"]; }
啊我想通了 – 使用这个和所有的情况下,像进入后台停止将被固定。
animation.removedOnCompletion = false
尝试这个,
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addAnimation:) name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void)addAnimation:(NSNotification *)notificaiton { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat: 2*M_PI]; animation.toValue = [NSNumber numberWithFloat:0.0f]; animation.duration = 4.0f; animation.repeatCount = INFINITY; [imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"]; [imageRight.layer addAnimation:animation forKey:@"SpinAnimation"]; }
当你离开应用程序时,所有animation都会从其图层中删除:系统会调用每个图层的removeAllAnimations。 所以,如果你想继续animation,那么你可以听取UIApplicationDidBecomeActiveNotification并再次开始animation。
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; if (![_imageLeft.layer animationForKey:@"SpinAnimation"]) { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat: 2*M_PI]; animation.toValue = [NSNumber numberWithFloat:0.0f]; animation.duration = 4.0f; animation.repeatCount = INFINITY; [_imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"]; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addAnimation:) name:UIApplicationDidBecomeActiveNotification object:nil]; // Do any additional setup after loading the view, typically from a nib. } - (void)addAnimation:(NSNotification *)notificaiton { if (_imageLeft && ![_imageLeft.layer animationForKey:@"SpinAnimation"]) { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat: 2*M_PI]; animation.toValue = [NSNumber numberWithFloat:0.0f]; animation.duration = 4.0f; animation.repeatCount = INFINITY; [_imageLeft.layer addAnimation:animation forKey:@"SpinAnimation"]; } } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
当应用程序进入后台时,系统会从其图层中移除所有animation。 在你的viewWillAppear:
方法中,注册UIApplicationDidBecomeActiveNotification
。 当您观察通知时,请再次添加animation。 取消注册viewWillDisappear:
的通知viewWillDisappear:
。