我只是关门,延迟2秒/调用方法然后打开它们。 一个来自左侧,另一个来自右侧。 我首先使用了UIView动画块,但随后用户在动画期间离开应用程序时,它的完成块永远不会被调用。
在动画中发生中断时,我找不到任何抱怨此类行为的人因此无法解决问题,现在我希望CAAnimation的animationDidStop或CATransaction的完成块无论如何都会被调用100%。
修改
尝试下面的代码,即使我无法使延迟工作,它仍然有效。 (CACurrentMediaTime()+延迟使第二个动画立即结束)最重要的是,即使动画中断,我也很高兴看到animationDidStop会被调用。 (如果用户离开应用程序或同时触发VC的动画,则基于块的UIView动画和完成块永远不会被调用) 我很想知道为什么UIView动画会发生这种情况,以及是否有解决方案。
我应该坚持使用这种方法并尝试修复延迟或提出其他方法吗? CAAnimationGroup在这种情况下不起作用,因为当整个组完成时会调用animationDidStop,因此在第一个动画结束时无法调用方法。
-(void)action {
CABasicAnimation *animDoorLeftClose = [CABasicAnimation animationWithKeyPath:@"position.x"];
animDoorLeftClose.fromValue = [NSNumber numberWithFloat:self.doorLeft0.position.x];
animDoorLeftClose.toValue = [NSNumber numberWithFloat:self.doorLeft0.position.x + self.frame.size.width/2];
animDoorLeftClose.duration = 0.2;
animDoorLeftClose.beginTime = 0.0;
self.doorLeft0.position = CGPointMake(self.doorLeft0.position.x + self.frame.size.width/2, self.doorLeft0.position.y);
[self.doorLeft0 addAnimation:animDoorLeftClose forKey:@"leftClosing"];
CABasicAnimation *animDoorRightClose = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animDoorRightClose setValue:@"doorClosing" forKey:@"id"];
animDoorRightClose.fromValue = [NSNumber numberWithFloat:self.doorRight0.position.x];
animDoorRightClose.toValue = [NSNumber numberWithFloat:self.doorRight0.position.x - self.frame.size.width/2];
animDoorRightClose.duration = 0.2;
animDoorRightClose.beginTime = 0.0;
animDoorRightClose.delegate = self;
self.doorRight0.position = CGPointMake(self.doorRight0.position.x - self.frame.size.width/2, self.doorRight0.position.y);
[self.doorRight0 addAnimation:animDoorRightClose forKey:@"rightClosing"]; }
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if([[anim valueForKey:@"id"] isEqual:@"doorClosing"]) {
[self.delegate shipTOmap];
}
[self doorClosed];
}
else if ([[anim valueForKey:@"id"] isEqual:@"doorOpening"]) {
[self doorOpened];
}}
-(void)doorClosed {
CABasicAnimation *animDoorLeftOpen = [CABasicAnimation animationWithKeyPath:@"position.x"];
animDoorLeftOpen.fromValue = [NSNumber numberWithFloat:[self.doorLeft0.presentationLayer position].x];
animDoorLeftOpen.toValue = [NSNumber numberWithFloat:[self.doorLeft0.presentationLayer position].x - self.frame.size.width/2];
animDoorLeftOpen.duration = 0.4;
animDoorLeftOpen.beginTime = CACurrentMediaTime()+0.2;
self.doorLeft0.position = CGPointMake([self.doorLeft0.presentationLayer position].x - self.frame.size.width/2, self.doorLeft0.position.y);
[self.doorLeft0 addAnimation:animDoorLeftOpen forKey:@"leftOpening"];
CABasicAnimation *animDoorRightOpen = [CABasicAnimation animationWithKeyPath:@"position.x"];
[animDoorRightOpen setValue:@"doorOpening" forKey:@"id"];
animDoorRightOpen.fromValue = [NSNumber numberWithFloat:[self.doorRight0.presentationLayer position].x];
animDoorRightOpen.toValue = [NSNumber numberWithFloat:[self.doorRight0.presentationLayer position].x + self.frame.size.width/2];
animDoorRightOpen.duration = 0.4;
animDoorRightOpen.beginTime = CACurrentMediaTime()+0.2;
animDoorRightOpen.delegate = self;
self.doorRight0.position = CGPointMake([self.doorRight0.presentationLayer position].x + self.frame.size.width/2, self.doorRight0.position.y);
[self.doorRight0 addAnimation:animDoorRightOpen forKey:@"rightOpening"];
}