CAKeyframeAnimation:沿路径移动UIView

5

这是我移动UIview的代码,将其向下移动30px,然后向上移动到y = 10,但是此动画无法正常工作。这是我第一次尝试创建CAKeyframeAnimation,所以有人可以帮我正确编写它吗?另外,我不想让我的对象返回原始位置,而是停留在动画结束的位置。

CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, self.logo.frame.origin.y, self.logo.frame.size.width, self.logo.frame.size.height));
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, self.logo.frame.origin.y-30, self.logo.frame.size.width, self.logo.frame.size.height));
    CGPathAddRect(thePath, NULL, CGRectMake(self.logo.frame.origin.x, 100, self.logo.frame.size.width, self.logo.frame.size.height));


    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
    AniLoc.path = thePath;
    AniLoc.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
                      [NSNumber numberWithFloat:0.3f],
                      [NSNumber numberWithFloat:1.0f],nil];
    AniLoc.duration = 2.0;

    CFRelease(thePath);

    [self.logo.layer addAnimation:AniLoc forKey:nil];

它以什么方式“不工作”? - jrturton
动画没有移动任何东西。 - Ayaz Alavi
1个回答

1

我不太确定为什么您的方法不起作用,但我可以建议另一种解决方案。 您可以更改位置键以移动UIView,而不是驱动框架:

    CGMutablePathRef thePath = CGPathCreateMutable();
    CGPathMoveToPoint(thePath, NULL, self.logo.frame.origin.x, self.logo.frame.origin.y); // initial point, notice the function
    CGPathAddLineToPoint(thePath, NULL, self.logo.frame.origin.x - 30, self.logo.frame.origin.y);
    CGPathAddLineToPoint(thePath, NULL, 100, self.logo.frame.origin.y);

    CAKeyframeAnimation* AniLoc = [CAKeyframeAnimation animationWithKeyPath:@"position"]; // notice key change

    // rest is the same
    AniLoc.path = thePath;
    AniLoc.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    AniLoc.keyTimes= [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],
                      [NSNumber numberWithFloat:0.3f],
                      [NSNumber numberWithFloat:1.0f],nil];
    AniLoc.duration = 2.0;

    CFRelease(thePath);

    [self.logo.layer addAnimation:AniLoc forKey:nil];

希望这能帮到你。


1
它无法正常工作,因为frame是一个派生属性。如果您需要对frame进行动画处理,可以对bounds和position进行动画处理。 - alecail

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接