2010-08-04 80 views
6

因此,我目前正在測試UIView動畫,並且我注意到CALayer呈現的陰影(使用[view.layer setShadowStuffHere])在動畫開始時消失並重新出現在動畫結束處。有沒有什麼辦法可以保持這些陰影,並使陰影與UIView一起動畫?我嘗試使用沒有邊框路徑的陰影,但這是一個糟糕的主意,因爲在動畫過程中,幀速率像石頭一樣下降,反正我也沒有得到陰影。CALayer Shadows在UIView動畫中消失

我現在使用的代碼如下。最初你會看到一個紅色的視圖,當點擊時,它會翻轉成一個更大的藍色視圖。最終結果應該與iPad音樂應用程序類似;當選擇一張專輯時,它會翻轉顯示背面的視圖。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UITapGestureRecognizer * tapRec; 

    // Drop shadow Path creation 
    CGFloat x1 = 0; 
    CGFloat y1 = 0; 
    CGFloat x2 = 100; 
    CGFloat y2 = 100; 

    frontBorderPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(frontBorderPath, NULL, x1, y1); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x2, y1); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x2, y2); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x1, y2); 
    CGPathAddLineToPoint(frontBorderPath, NULL, x1, y1); 

    x2 = 400; 
    y2 = 400; 
    backBorderPath = CGPathCreateMutable(); 
    CGPathMoveToPoint(backBorderPath, NULL, x1, y1); 
    CGPathAddLineToPoint(backBorderPath, NULL, x2, y1); 
    CGPathAddLineToPoint(backBorderPath, NULL, x2, y2); 
    CGPathAddLineToPoint(backBorderPath, NULL, x1, y2); 
    CGPathAddLineToPoint(backBorderPath, NULL, x1, y1); 

    containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; 
    containerView.clipsToBounds = NO; 
    [containerView.layer setShadowPath:frontBorderPath]; 
    [containerView.layer setShadowOpacity:1]; 
    [containerView.layer setShadowOffset:CGSizeMake(0,0)]; 
    [containerView.layer setShadowRadius:3]; 
    [containerView.layer setShouldRasterize:NO]; 
    [self.view addSubview:containerView]; 

    tapRec = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(frontTap)] autorelease]; 
    frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    frontView.backgroundColor = [UIColor redColor]; 
    [frontView addGestureRecognizer:tapRec]; 
    [containerView addSubview:frontView]; 

    tapRec = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backTap)] autorelease]; 
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)]; 
    backView.backgroundColor = [UIColor blueColor]; 
    [backView addGestureRecognizer:tapRec];  
} 

- (void)frontTap{ 
    NSLog(@"fronttap"); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:containerView cache:YES]; 

    [frontView removeFromSuperview]; 
    [containerView addSubview:backView]; 
    containerView.frame = CGRectMake(100, 100, 400, 400); 
    [containerView.layer setShadowPath:backBorderPath]; 

    [UIView commitAnimations]; 

} 

- (void)backTap{ 
    NSLog(@"backtap"); 

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.7]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:containerView cache:YES]; 

    [backView removeFromSuperview]; 
    [containerView addSubview:frontView]; 
    containerView.frame = CGRectMake(100, 100, 100, 100); 
    [containerView.layer setShadowPath:frontBorderPath]; 

    [UIView commitAnimations]; 

} 

回答

12

事實證明,當你做一個UIView動畫,它忽略了你的觀點的clipsToBounds財產和剪輯它反正(至少在翻蓋型動畫),所以如果你想你的陰影始終可見,你需要有一個足夠大的包含所有東西的視圖框架。

+0

請您詳細說明一下嗎? – sole007 2015-05-12 08:11:30

+0

@ sole007:這個特殊的答案可能太老了,不適用於今天(我自此以後沒有任何最近的解決方案),但是在需要的時候,問題是即使設置了clipsToBounds爲false,並且您使用CALayers在視圖的特定邊界之外渲染陰影,依靠clipsToBounds對於顯示的陰影爲false,則無關緊要,並且這些陰影不會顯示出來。那麼解決方案是讓視圖邊界包含陰影而不是邊界之外的陰影。 – 2015-09-16 23:57:41