2013-04-04 68 views
1

我一直在嘗試做CALayer動畫。但動畫後它恢復回來。我發現了幾個解決方案,如removedOnCompleteion = YESfill mode = kCAFillModesForwards,但沒有人幫助我。這裏是代碼。需要一點幫助同胞thx。CALayer動畫還原

void(^ myblock)(NSDictionary *) = ^(NSDictionary *dict){ 

     NSMutableArray *arrOfPt=  [dict objectForKey:@"Path"]; 

     CGPoint p1 = [[arrOfPt objectAtIndex:0] CGPointValue]; 

     CGPoint cp1 = [[arrOfPt objectAtIndex:1] CGPointValue]; 

//  CGPoint cp1 = [[arrOfPt objectAtIndex:1] CGPointValue]; 
     CGPoint cp2 = [[arrOfPt objectAtIndex:2] CGPointValue]; 
     CGPoint p2 = [[arrOfPt objectAtIndex:3] CGPointValue]; 

     NSMutableArray *arrayTime = [dict objectForKey:@"Time"]; 
     CGFloat time = [[arrayTime objectAtIndex:0] floatValue]; 

     pointerLayer.opacity = 1; 
//  NSArray *oneObjPt = [NSArray arrayWithObjects:[arrOfPt objectAtIndex:0],[arrOfPt lastObject],nil]; 
     //   NSArray *oneObjTime = [NSArray arrayWithObjects:[arrayTime objectAtIndex:0],[arrayTime lastObject],nil]; 
//  NSArray *oneObjTime = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:1.0],nil]; 
     CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 

//  [keyFrameAnimation setCalculationMode:kCAAnimationPaced]; 
     keyFrameAnimation.duration = 1; 
     keyFrameAnimation.removedOnCompletion = NO; 
     [keyFrameAnimation setFillMode:kCAFillModeForwards]; 

     CGMutablePathRef fpath = CGPathCreateMutable(); 

     CGPathMoveToPoint(fpath, NULL, p1.x, p1.y); 
     CGPathAddCurveToPoint(fpath, NULL, cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y); 
     keyFrameAnimation.path = fpath; 

     [pointerLayer addAnimation:keyFrameAnimation forKey:@"pos"]; 
     CGPoint newLoc = pointerLayer.position;// this is just to verify the layer's location 

回答

1

我找到了一些解決方案,如removedOnCompleteion = YES和填充模式= kCAFillModesForwards

這些都不是解決辦法。

CGPoint newLoc = pointerLayer.position;

這不會有什麼好處;添加動畫並不會改變位置。動畫發生在後面

我不完全確定你在做什麼,但它可以幫助你閱讀我對Core Animation的解釋。動畫只是一種幻覺;這是一個與「表示層」一起玩的遊戲。它是由層移動到它將在動畫後的位置,以添加看起來會使其移動的動畫。

我爲確保您的核心動漫作品,讓你一個公式化的方法,你想去的地方:

http://www.apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation

+0

THX隊友,我沒有實際工作 – Azerue 2013-04-05 06:18:09

0

我似乎無法重現此 - 授予我用一些測試數據:

CGPoint cp1 = CGPointMake(0, 0); 
CGPoint cp2 = CGPointMake(100, 100); 
CGPoint cp3 = CGPointMake(85, 150); 
CGPoint cp4 = CGPointMake(50, 50); 
CGFloat time = 2.0f; 
CAKeyframeAnimation *keyFrameAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
keyFrameAnimation.duration = time; 
keyFrameAnimation.removedOnCompletion = NO; 
[keyFrameAnimation setFillMode:kCAFillModeForwards]; 

CGMutablePathRef fpath = CGPathCreateMutable(); 

CGPathMoveToPoint(fpath, NULL, cp1.x, cp1.y); 
CGPathAddCurveToPoint(fpath, NULL, cp2.x, cp2.y, cp3.x, cp3.y, cp4.x, cp4.y); 
keyFrameAnimation.path = fpath; 
[testView.layer addAnimation:keyFrameAnimation forKey:@"pos"]; 
CFRelease(fpath); 

,輸出:

http://glassofcocoa.com/code/AnimationTest.mp4

可能貴點的一個陣列中可能是首發位置?