2013-05-04 71 views
1

起初,我的層從點移動到另一個點我使用該代碼:移動 - 層回到第一位置

 CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"]; 
     anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
     anim.fromValue = [NSValue valueWithCGPoint:startPt]; 
     anim.toValue = [NSValue valueWithCGPoint:endPt]; 
     anim.repeatCount = 0; 
     anim.duration = 1.0; 
     [self.firstBall.ballLayer addAnimation:anim forKey:@"position"]; 

而這個動畫清晰的第一層後,在其他地方重新繪製。

NSInteger firstBallIndex = [self.fieldsArray indexOfObject:self.firstBall]; 
      NSInteger secondBallIndex = [self.fieldsArray indexOfObject:self.secondBall]; 
      Field* ballFrom = [self.fieldsArray objectAtIndex:firstBallIndex]; 
      Field* ballTo = [self.fieldsArray objectAtIndex:secondBallIndex]; 
      ballTo.ballLayer = ballFrom.ballLayer; 
      CGPoint startPt = CGPointMake(self.firstBall.ballCoordinates.x,self.firstBall.ballCoordinates.y); 
    CGPoint endPt = CGPointMake(self.secondBall.ballCoordinates.x,self.secondBall.ballCoordinates.y); 
    ballTo.ballLayer.frame = CGRectMake(endPt.x, endPt.y, self.ballSize, self.ballSize); 
      ballFrom.ballLayer = nil; 
      [self.fieldsArray replaceObjectAtIndex:firstBallIndex withObject:ballFrom]; 
      [self.fieldsArray replaceObjectAtIndex:secondBallIndex withObject:ballTo]; 

但接下來我嘗試爲我的圖層設置動畫以移動很少的時間。所以我使用該代碼:

CGMutablePathRef path = CGPathCreateMutable(); 
    NSMutableArray * pathArray = [algorithm CreatePath]; 
    CGPathMoveToPoint(path, NULL, self.firstBall.ballCoordinates.x, self.firstBall.ballCoordinates.y); 
     for (NSValue * pointValue in pathArray) { 
      CGPoint point = [pointValue CGPointValue]; 
      Field* field = [self FindFieldWithPoint:point]; 
      CGPathAddLineToPoint(path, NULL, field.ballCoordinates.x, field.ballCoordinates.y); 
     } 
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.duration = 2.0; 
pathAnimation.path = path; 
[UIView animateWithDuration:5 
         delay:5 
        options:UIViewAnimationOptionAllowUserInteraction 
       animations:^{ 
        [self.firstBall.ballLayer addAnimation:pathAnimation forKey:@"position"]; 
       } 
       completion:^ (BOOL finished) 
{ 
    if (finished) { 


     ballTo.ballLayer.frame = CGRectMake(endPt.x, endPt.y, self.ballSize, self.ballSize); 
     ballFrom.ballLayer = nil; 
     [self.fieldsArray replaceObjectAtIndex:firstBallIndex withObject:ballFrom]; 
     [self.fieldsArray replaceObjectAtIndex:secondBallIndex withObject:ballTo]; 
    } 
}]; 

現在:

  1. 當我的評論在「完成」代碼: 我的層動畫確定,從點移動到另一個點,但最終回第一座標

  2. 當我在「完成」取消註釋代碼: 從第一座標我的圖層移動到最後,就像不知道之間的地方:/

回答

3

這是錯誤的:

[UIView animateWithDuration:5 // and so on 

不要試圖層動畫與觀看動畫這種方式結合。只需將動畫添加到圖層並退後一步。 :)

CAKeyframeAnimation *pathAnimation = 
    [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
pathAnimation.duration = 2.0; 
pathAnimation.path = path; 
[self.firstBall.ballLayer addAnimation:pathAnimation forKey:@"position"]; 

和設置您的最終位置立即後防止跳回:

ballTo.ballLayer.frame = 
    CGRectMake(endPt.x, endPt.y, self.ballSize, self.ballSize); 

如果您仍然需要聽到當動畫結束,然後設定一個代表您pathAnimation

+0

立即表示在addAnimation之前設置幀?我有問題與設置委託路徑動畫,這不工作:/:[pathAnimation setDelegate:self]; - (void)moveAnimationDone :(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { } – netmajor 2013-05-05 09:49:32

+0

主要問題是爲什麼當我取消註釋ballTo.ballLayer.frame = CGRectMake(endPt.x,endPt .y,self.ballSize,self.ballSize); 我的圖層直接移動到最後一點?我只希望當我使用en動畫代表這個問題解決了它自己... – netmajor 2013-05-05 10:44:20

+0

立即意味着立即。將動畫添加到圖層並設置圖層的最終位置。一起。 – matt 2013-05-05 13:03:44