Iphone CALayer在尝试设置动画时会立即移动?
下面的代码处理一堆Iphone CALayer在尝试设置动画时会立即移动?,iphone,animation,core-animation,core-graphics,calayer,Iphone,Animation,Core Animation,Core Graphics,Calayer,下面的代码处理一堆calayer。每次将新层推到堆栈上时,函数都会被锁定,所有现有层都会在屏幕上向下移动,以便为新层腾出空间。一旦最后一层完成动画制作,该功能将再次解锁,以便推送新层 我的问题是每次代码在newLayer上运行初始动画时。换句话说,与其将新层定位在CGPointMake(0,0-offset)上,然后将其动画化为CGPointMake(0,currentLayer.position.y+offset),它会立即显示在其最终位置。我错过什么了吗?谢谢 -(void)addNewLa
calayer
。每次将新层推到堆栈上时,函数都会被锁定,所有现有层都会在屏幕上向下移动,以便为新层腾出空间。一旦最后一层完成动画制作,该功能将再次解锁,以便推送新层
我的问题是每次代码在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;
}
}
-(void)addNewLayerWithHeight:(float)layerHeight{
如果(!animationLocked){
animationLocked=是;
//偏移量是每个现有层需要向下移动的数量
int offset=图层亮度;
//创建新图层
CALayer*newLayer=[CALayer layer];
[newLayer-setBounds:CGRectMake(0,0,self.view.layer.bounds.size.width,layerHeight)];
[newLayer setancorpoint:CGPointMake(0,0)];
[新图层设置位置:CGPointMake(0,0-偏移)];
[newLayer setBackgroundColor:[[UIColor redColor]CGColor]];
//将新图层添加到视图的图层和LayerRay
[self.view.layer addSublayer:newLayer];
[layerArray插入对象:新层索引:0];
//循环遍历所有层并将它们移动到新位置。。。
对于(int i=0;i,您必须在动画中设置新的层位置,而不是直接在层上设置
CAlayer *layer = ...
CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation"];]
positionAnimation.fromValue = oldPOsition
positionAnimation.toValue = newPosition
positionAnimation.duration = n;
positionAnimation.delegate = self;
[layerToAnimate addAnimation:layerAnimation forKey:@"transform.translation"]
你已经很接近了。我会把你循环中的代码改成:
for(int i=0;i<[layerArray count]; i++)
{
CALayer *currentLayer = [layerArray objectAtIndex:i];
CGPoint endPoint = CGPointMake(0, currentLayer.position.y + offset);
CGPoint currentPoint = [currentLayer position];
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
[anim setFromValue:[NSValue valueWithCGPoint:currentPoint]];
[anim setToValue:[NSValue valueWithCGPoint:endPoint]];
[anim setDelegate:self];
[anim setDuration:1.0];
[anim setValue:@"stackLayer" forKey:@"kind"];
[anim setValue:[NSNumber numberWithInt:i] forKey:@"index"];
[currentLayer setPosition:endPoint];
[currentLayer addAnimation:anim forKey:@"position"];
}
for(int i=0;问题是,您正在绘制的是一个显式动画,动画完成后,层会恢复回其原始位置。