CAKeyframeanimation沿路径移动UIView

时间:2012-01-09 13:35:31

标签: iphone ios core-animation

这是我的代码,将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];

1 个答案:

答案 0 :(得分: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];

我希望这会对你有所帮助。