我有以下基于块的动画:
[UIView animateWithDuration:0.5f delay:0.0f
options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut
animations:^{
[view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)];
NSLog(@"animating");
}completion:^(BOOL finished){
NSLog(@"Completed");
}];
当应用程序从后台返回时,将调用完成块,并且我的动画不会重新启动。我尝试使用以下委托方法重新启动动画:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
[[self viewController] animate];
......
}
但这并没有恢复动画效果。
同样,我尝试了这些问题的答案中列出的方法:
但那里没有任何建议对我有用。当应用程序从后台返回时,还有另一种方法可以恢复基于块的UIView动画吗?
答案 0 :(得分:5)
一位朋友发现了问题,需要在视图从背景返回时启用视图
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
*/
[UIView enableAnimations:YES];
[[self viewController] animate];
......
}
然后在块动画之前需要删除AllAnimations并将layer.transform设置为Identity
hasStarted = YES;
for(UIButton * button in goldenBreakOutButtons){
for (UIView* view in button.subviews) {
if (wasStarted) {
[view.layer removeAllAnimations];
view.layer.transform = CATransform3DIdentity;
}
if ([view isKindOfClass:[UIImageView class]]) {
[UIView animateWithDuration:0.5f delay:0.0f
options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionRepeat
animations:^ {
[view.layer setTransform:CATransform3DMakeScale(1.3f, 1.3f, 1.0f)];
NSLog(@"animating");
}
completion:^(BOOL finished){
if (finished) {
NSLog(@"Completed");
}
}];
}
}
}
答案 1 :(得分:2)
请尝试在ViewController中订阅/取消订阅
标准( UIApplicationWillEnterForegroundNotification )通知
来自 NSNotificationCenter :
-(void) loadView
{
.....
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(restartAnimation)
name:UIApplicationWillEnterForegroundNotification
object:nil];
....
}
- (void) viewDidUnload
{
...
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationWillEnterForegroundNotification
object:nil];
....
}
-(void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void) restartAnimation
{
if(self.logoImageView)
{
[logoImageView.layer removeAllAnimations];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.6];
animation.duration = 1.5f;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.autoreverses = YES;
animation.repeatCount = HUGE_VALF;
[[logoImageView layer] addAnimation:animation forKey:@"hidden"];
}
}