【发布时间】:2010-07-22 18:55:57
【问题描述】:
下面的代码处理CALayers 的堆栈。每次将新层压入堆栈时,该功能都会被锁定,所有现有层都会在屏幕上向下移动,以便为新层腾出空间。最后一层完成动画后,该功能将再次解锁,以便可以推送新层。
我的问题是每次这段代码在newLayer 上运行初始动画。换句话说,不是将新图层定位在CGPointMake(0, 0-offset),然后将其动画到CGPointMake(0, currentLayer.position.y + offset),而是立即显示在其最终位置。我错过了什么吗?谢谢!
-(void)addNewLayerWithHeight:(float)layerHeight {
if(!animationLocked) {
animationLocked = YES;
//Offset is the ammount that each existing layer will need to be moved down by
int offset = layerHeight;
//Create the new layer
CALayer *newLayer = [CALayer layer];
[newLayer setBounds:CGRectMake(0, 0, self.view.layer.bounds.size.width, layerHeight)];
[newLayer setAnchorPoint:CGPointMake(0, 0)];
[newLayer setPosition:CGPointMake(0, 0-offset)];
[newLayer setBackgroundColor:[[UIColor redColor] CGColor]];
//Add the new layer to the view's layer and to layerArray
[self.view.layer addSublayer:newLayer];
[layerArray insertObject:newLayer atIndex:0];
//loop through all layers and move them to their new position...
for(int i=0;i<[layerArray count]; i++) {
CALayer *currentLayer = [layerArray objectAtIndex:i];
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
[anim setValue:@"stackLayer" forKey:@"kind"];
[anim setValue:[NSNumber numberWithInt:i] forKey:@"index"];
[anim setDelegate:self];
[anim setDuration:1.0];
currentLayer.actions = [NSDictionary dictionaryWithObject:anim forKey:@"position"];
currentLayer.position = CGPointMake(0, currentLayer.position.y + offset);
}
}
}
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//Make sure the last layer finished animating...
if([[anim valueForKey:@"kind"] isEqual:@"stackLayer"] && [[anim valueForKey:@"index"] isEqual:[NSNumber numberWithInt:[layerArray count]-1]]) {
animationLocked = NO;
}
}
【问题讨论】:
标签: iphone animation core-animation core-graphics calayer