Animation 动画结束时回调?
我在同时制作多个Calayer动画时遇到了一些问题,希望有人能给我指出正确的方向 我的应用程序包含一个CALayer数组。每个层的位置都被设置为Animation 动画结束时回调?,animation,core-animation,calayer,cabasicanimation,Animation,Core Animation,Calayer,Cabasicanimation,我在同时制作多个Calayer动画时遇到了一些问题,希望有人能给我指出正确的方向 我的应用程序包含一个CALayer数组。每个层的位置都被设置为(previousLayer.position.y+previousLayer.bounds.height),这基本上把它们放在类似于表格的位置。然后我有一个方法,每次调用它时,都会向堆栈中添加一个新层,并将其Y位置设置为0。然后,阵列中所有其他层的Y位置被新层的高度偏移(基本上将所有旧层向下推) 我遇到的问题是在上一个动画完成之前阻止添加新层。有没有办
(previousLayer.position.y+previousLayer.bounds.height)
,这基本上把它们放在类似于表格的位置。然后我有一个方法,每次调用它时,都会向堆栈中添加一个新层,并将其Y位置设置为0。然后,阵列中所有其他层的Y位置被新层的高度偏移(基本上将所有旧层向下推)
我遇到的问题是在上一个动画完成之前阻止添加新层。有没有办法判断隐式动画何时完成?或者,如果我使用
CABasicAnimation
和animationdidfish
,是否有办法在调用animationdidfish
时判断哪个对象完成了动画制作?可以为动画对象上的关键点设置任意值。这意味着您可以将正在设置动画的层与动画相关联,然后在-animationDidStop:finished中查询它:您可以通过以下方式创建动画:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
// set other fields...
[anim setValue:layerToAnimate forKey:@"layer"];
// Start the animation
[layerToAnimate addAnimation:anim forKey:nil];
然后在动画停止时检查该值:
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
CALayer *animatedLayer = [animation valueForKey:@"layer"];
// do something with the layer based on some condition...
// spin off the next animation...
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[animatedLayer setPosition:position];
[CATransaction commit];
}
这是显式动画,但它应该能满足您的需要。您可以为动画对象上的关键点设置任意值。这意味着您可以将正在设置动画的层与动画相关联,然后在-animationDidStop:finished中查询它:您可以通过以下方式创建动画:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
// set other fields...
[anim setValue:layerToAnimate forKey:@"layer"];
// Start the animation
[layerToAnimate addAnimation:anim forKey:nil];
然后在动画停止时检查该值:
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag
{
CALayer *animatedLayer = [animation valueForKey:@"layer"];
// do something with the layer based on some condition...
// spin off the next animation...
[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue
forKey:kCATransactionDisableActions];
[animatedLayer setPosition:position];
[CATransaction commit];
}
这是显式动画,但它应该能满足您的需要。结果是,我没有直接将CABasicAnimation添加到CALayer中,而是将其添加到层的“动作”字典中。。。这会在动画结束后将层保留在其最终位置,但仍会调用“animationDidFinish”方法
-(void)startAnimation {
if(!animating){
animating = YES;
for(int i=0; i<[tweets count]; i++) {
//get the layer
CETweetLayer *currentLayer = [tweets objectAtIndex:i];
//setup the orgin and target y coordinates
float targetY = currentLayer.position.y + offset;
//setup the animation
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.delegate = self;
currentLayer.actions = [NSDictionary dictionaryWithObject:anim forKey:@"position"];
currentLayer.position = CGPointMake(self.bounds.size.width/2, targetY);
}
}
}
结果是,我没有直接将cabasicaniation添加到CALayer中,而是将其添加到层的“动作”字典中。。。这会在动画结束后将层保留在其最终位置,但仍会调用“animationDidFinish”方法
-(void)startAnimation {
if(!animating){
animating = YES;
for(int i=0; i<[tweets count]; i++) {
//get the layer
CETweetLayer *currentLayer = [tweets objectAtIndex:i];
//setup the orgin and target y coordinates
float targetY = currentLayer.position.y + offset;
//setup the animation
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.delegate = self;
currentLayer.actions = [NSDictionary dictionaryWithObject:anim forKey:@"position"];
currentLayer.position = CGPointMake(self.bounds.size.width/2, targetY);
}
}
}
您可以尝试在
CATransaction
中包围代码。以下是Swift 3中的情况:
CATransaction.begin()
CATransaction.setCompletionBlock({
// run after the animations
})
// animtations
CATransaction.commit()
您可以尝试在
CATransaction
中包围代码。以下是Swift 3中的情况:
CATransaction.begin()
CATransaction.setCompletionBlock({
// run after the animations
})
// animtations
CATransaction.commit()
这基本上奏效了。代码被调用,我可以从键中获取层对象。但是,当我尝试将layer.position设置为动画结束时层应该位于的位置时,我遇到了闪烁效果。在调用我的“layer.position=”之前,动画似乎结束,层位置被设置回原点位置。知道我该怎么处理吗?知道。如果要在动画结束时设置position属性,则必须在-animationDidStop:finished:方法中的事务中禁用隐式动画。我将用相关代码更新我的答案。你发布的代码与我的代码相同,并导致闪烁。我刚刚想出了如何绕过它,并将在几秒钟后发布代码。谢谢你的帮助!对我来说很有效,但是我必须把它放在动画中,这才是最有效的。代码被调用,我可以从键中获取层对象。但是,当我尝试将layer.position设置为动画结束时层应该位于的位置时,我遇到了闪烁效果。在调用我的“layer.position=”之前,动画似乎结束,层位置被设置回原点位置。知道我该怎么处理吗?知道。如果要在动画结束时设置position属性,则必须在-animationDidStop:finished:方法中的事务中禁用隐式动画。我将用相关代码更新我的答案。你发布的代码与我的代码相同,并导致闪烁。我刚刚想出了如何绕过它,并将在几秒钟后发布代码。谢谢你的帮助!这是最整洁的解决方案,效果非常好。这是最整洁的解决方案,效果非常好。