2012-01-09 93 views
5

这里是我的代码移动UIview 30px然后到y = 10但这个动画不起作用。这是我第一次尝试创建CAKeyframeAnimation,因此任何人都可以帮助我正确写入它。另外我希望我的对象不会回到原来的位置,而是呆在那里动画结束的地方。CAKeyframeanimation沿着路径移动UIView

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]; 
+0

它以什么方式“不工作”? – jrturton 2012-01-09 14:06:41

+0

动画没有移动任何东西 – 2012-01-09 14:13:57

回答

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

它不工作,因为框架是派生属性。如果您需要为框架设置动画,则可以为边界和位置设置动画。 – alecail 2013-06-12 05:41:35