简体   繁体   English

如何在animationDidStop中识别CAShapeLayer

[英]How to identify CAShapeLayer within the animationDidStop

I have an array of CAShapeLayer . 我有一个CAShapeLayer数组。 At some point I must iterate through that array and start an animation for each layer. 在某些时候,我必须遍历该数组并为每个图层启动动画。 Those animations change the bounds.size.height of the layers to different values (computed in some way). 这些动画将图层的bounds.size.height更改为不同的值(以某种方式计算)。 In the animationDidStop method I'd like to actually change the height of each layer to the animation.toValue value. animationDidStop方法中,我想将每一层的高度实际更改为animation.toValue值。 I need to do this because I want the future animations to start from the new values and not from the initial values. 我需要这样做,因为我希望将来的动画从新值开始,而不是从初始值开始。 This is the loop: 这是循环:

for (int i=0; i<[layersArray count]; i++) {
    newLayerHeight = [self computeNewHeightForLayer:[layersArray objectAtIndex:i];

    CABasicAnimation *myAnim = [CABasicAnimation animationWithKeyPath:@"bounds.size.height"];
    myAnim.delegate = self;
    myAnim.duration = 0.4;
    myAnim.removedOnCompletion = NO;
    myAnim.fillMode = kCAFillModeForwards;
    myAnim.fromValue = [NSNumber numberWithFloat:[layersArray objectAtIndex:i]).bounds.size.height];
    myAnim.toValue = [NSNumber numberWithFloat:newLayerHeight];
    [[layersArray objectAtIndex:i] addAnimation:myAnim forKey:@"changeHeightAnim"];
}

In the animationDidStop method I'd like to do something like this (equivalent to this, actually; the if-else paradigm is not the best): animationDidStop方法中,我想做这样的事情(实际上,等效于此; if-else范例不是最好的):

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if(flag){
        /*
            if(anim is linked to layer0)change the height of layer0;
            if(anim is linked to layer1)change the height of layer1;
            .
            .
            .
            if(anim is linked to layerN-1)change the height of layerN-1;

        */
    }
}

Any idea? 任何想法? Thank you. 谢谢。

Give a specific value for each animation you set to each CAShapeLayer as you loop through your layers array, like so: 在遍历图层数组时,为设置给每个CAShapeLayer每个动画指定一个特定的值,如下所示:

[myAnim setValue:@"layer_1" forKey:@"animation_id"];
[myAnim setValue:@"layer_2" forKey:@"animation_id"]; 
...

In your 'animationDidStop' method, check the value of the animation parameter for the value, like so: 在您的“ animationDidStop”方法中,检查animation参数的值以获取该值,如下所示:

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if([[anim valueForKey:@"animation_id"] isEqual:@"layer_1"]) {
        // do something
    }
    else if([[anim valueForKey:@"animation_id"] isEqual:@"layer_2"]) {
        // do something
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM