使用CoreAnimation制作帧动画

4
我想要对滚动视图中的一些子视图的位置进行动画处理。我希望创建一个特定效果,使它们滑入时具有反弹效果,就像当滚动视图到达末尾时看起来的那样,只是相反的方向。之前,我通过更改UIView上的frame使用隐式动画,但当它们滑入时,它看起来太机械了。无论如何,我无法让它动画改变框架的原点。我不确定自己错过了什么,因为如果我切换正在进行动画处理的内容(将第一个//*更改为/*),仅通过更改透明度就可以很好地工作。
- (void)slideInStories;
{
    float scrollViewContentWidth = 0;

    for (StoryViewController *storyController in storyControllers) {
        NSMutableArray *animationPoints = [NSMutableArray array];
        CGRect viewFrame = storyController.view.frame; 

        //*
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"frame"];
        [animationPoints addObject:[NSValue valueWithCGRect:viewFrame]];

        viewFrame.origin.x = scrollViewContentWidth - 10;
        [animationPoints addObject:[NSValue valueWithCGRect:viewFrame]];

        viewFrame.origin.x = scrollViewContentWidth;
        [animationPoints addObject:[NSValue valueWithCGRect:viewFrame]];
        /*/
        CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        [animationPoints addObject:[NSNumber numberWithFloat:0.75]];
        [animationPoints addObject:[NSNumber numberWithFloat:0.0]];
        [animationPoints addObject:[NSNumber numberWithFloat:0.75]];
        //*/

        [animation setValues:animationPoints];

        [animation setDuration:4.0];

        viewFrame.origin.x = scrollViewContentWidth;
        scrollViewContentWidth += viewFrame.size.width;
        [storyController.view.layer setFrame:viewFrame];
        [storyController.view.layer setOpacity:1.0];
        [storyController.view.layer addAnimation:animation forKey:nil];
    }

    [scrollView setContentSize:CGSizeMake(scrollViewContentWidth, 187.0f)];
}

你试过没有,就是为了好玩,尝试对center进行动画效果吗? - jscs
@Josh Caswell - 是的,我可以很好地完成这个职位。 - dustins
1个回答

5

您无法对CALayer的帧进行动画处理。


谢谢,这让我疯了! - dustins
1
我知道我在某个地方见过那个,但是class reference上说它是可以动画的!是文档错误吗? - jscs
@JoshCaswell 您已经链接到了UIView的frame属性。我不知道它是否可动画。然而,在@dustins发布的代码中,他特别将动画添加到了图层中。也许他还应该在UIView上使用基于块的动画方法?我没有直接在UIView对象上使用核心动画,所以我不能确定。 - Daniel Burke
将图层进行动画处理,而不是视图 - 你说得对。感谢指出错误。 - jscs

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