2011-05-18 69 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