2011-05-18 70 views
4

我试图在scrollview中设置一些子视图的位置。我想创建一个特定的效果,因为他们滑入。我只是通过更改UIView上的框架来使用隐式动画,但是当它们滑入时它看起来太机器人。所以我希望创建一个反弹效果,让它们在幻灯片中滑入,稍微走远,然后返回到其预期的位置,非常接近滚动视图到达结尾时的样子,恰好相反。CoreAnimation动画帧

无论如何,我不能让它动画改变框架的起源。我不确定我错过了什么,因为如果我切换出正在进行动画制作(将第一个// *改为/ *),它可以很好地改变不透明度。

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

您是否尝试过,只是为了踢腿,而不是用动画中心? – 2011-05-18 06:14:31

+0

@Josh Caswell - 是的,我可以做的很好。 – dustins 2011-05-18 12:29:23

回答

5

您不能为CALayer的框架设置动画。

+0

谢谢,这让我疯狂! – dustins 2011-05-18 12:27:51

+1

我知道我曾经在某个地方见过,但[类参考](http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/doc/ uid/TP40006816-CH3-SW5)说它是_is_动画!文档错误? – 2011-05-18 18:28:58

+0

@JoshCaswell你已经链接到UIView的框架属性。我不知道它是否可以动画。但是,在@dustins发布的代码中,他特别将动画添加到图层。也许他也应该在UIView上使用基于块的动画方法?我没有直接在UIView对象上使用Core Animation,所以我不能肯定地说。 – 2011-05-18 23:31:01