繁体   English   中英

使用CoreAnimation动画帧

[英]Animating Frame with CoreAnimation

我正在尝试在滚动视图中为某些子视图的位置设置动画。 我想在它们滑入时创建一个特定的效果。我只是通过更改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)];
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM