【问题标题】:Move layer with CAKeyframeAnimation - layer back to first position使用 CAKeyframeAnimation 移动图层 - 图层回到第一个位置
【发布时间】:2013-05-05 07:17:10
【问题描述】:

首先,我使用该代码将我的图层从一个点移动到另一个点:

        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. 当我取消注释“完成”中的代码时: 我的图层从第一个坐标移动到最后一个坐标,就像它不知道之间的位置:/

【问题讨论】:

    标签: objective-c cakeyframeanimation


    【解决方案1】:

    这是错误的:

    [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 上设置一个委托。

    【讨论】:

    • 立即表示在 addAnimation 之前设置帧?我将代理设置为路径动画有问题,这不起作用:/:[pathAnimation setDelegate:self]; - (void)moveAnimationDone:(NSString *)animationID 完成:(NSNumber *)完成上下文:(void *)context { }
    • 主要问题是为什么当我取消注释 ballTo.ballLayer.frame = CGRectMake(endPt.x, endPt.y, self.ballSize, self.ballSize);我的图层直接移动到最后一点?我只希望当我使用动画委托时,这个问题可以自行解决......
    • 立即表示立即。将动画添加到图层并设置图层最终位置。在一起。
    • 代理有问题,因为您设置了代理但使用了错误的操作方法。它是 animationDidStop:finished:
    • 这个代码示例在 iOS 8 上对我不起作用。我通过添加 CATransaction.setDisableActions(true) 来修复它。 @matt 提供的书中也描述了它
    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 2015-12-26
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    相关资源
    最近更新 更多