首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAKeyframeanimation沿路径移动UIView

CAKeyframeanimation沿路径移动UIView
EN

Stack Overflow用户
提问于 2012-01-09 21:35:31
回答 1查看 3.1K关注 0票数 5

这是我的代码,将UIview 30px向下移动,然后向上移动到y=10,但此动画不起作用。这是我第一次尝试创建CAKeyframeAnimation,因此任何人都可以帮助我正确编写它。此外,我希望我的对象不会回到原来的地方,而是停留在动画结束的地方。

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

发布于 2012-01-16 19:25:34

我不太确定为什么你的方法不起作用,但我可以提出另一种解决方案。您可以更改位置关键点来移动UIView,而不是驱动帧:

代码语言:javascript
复制
    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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8789225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档