Ios 当应用程序出现在前台时,如何重新启动基于块的动画?
我有以下基于块的动画:Ios 当应用程序出现在前台时,如何重新启动基于块的动画?,ios,cocoa-touch,uiview,core-animation,Ios,Cocoa Touch,Uiview,Core Animation,我有以下基于块的动画: [UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionRepeat|UIViewAnimationOptionAutoreverse|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionCurveEaseInOut animat
[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动画?一位朋友解决了问题,需要在视图从后台返回时启用动画
- (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];
......
}
然后在块动画之前,需要移除动画并将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");
}
}];
}
}
}
请尝试在ViewController中订阅/取消订阅到
标准(UIApplicationWillEnterForegroundNotification)通知
来自通知中心:
默认情况下,它已被订阅为UIApplicationWillEnterForegroundNotification。在应用程序返回前一轮之前,正在调用它。您到底在哪里找到了
[UIView启用说明:是]
?我在任何地方都看不到这种方法:(有点晚了,但请尝试[UIView SetAnimationEnabled:YES];
-(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"];
}
}