使用CoreAnimation动画帧

dus*_*ins 4 core-animation objective-c cakeyframeanimation ios

我正在尝试在滚动视图中为某些子视图的位置设置动画.我想在它们滑入时创建一个特定的效果.我只是通过更改UIView上的帧来使用隐式动画,但是当它们滑入时它看起来太机器人了.因此我希望能够创建一个弹跳效果,它们会滑入,向远处移动,然后回到预期的位置,几乎是滚动视图到达终点时的样子,恰恰相反.

无论如何,我无法通过动画来改变框架的原点.我不确定我缺少什么,因为如果我切换出动画的内容(将第一个//*设置为/*),它可以很好地改变不透明度.

- (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)];
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*rke 5

您无法为CALayer的框架设置动画.