簡體   English   中英

在UIView動畫期間,CALayer Shadows消失

[英]CALayer Shadows disappearing during a UIView animation

所以我目前正在測試UIView動畫,我注意到CALayer渲染的陰影(使用[view.layer setShadowStuffHere] )在動畫開始時消失,並在動畫結束時重新出現。 有什么方法可以保持這些陰影並讓陰影與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];

}

事實證明,當你進行UIView動畫時,它會忽略你視圖的clipsToBounds屬性並且無論如何都會剪輯它(至少對於翻轉類型的動畫),所以如果你想讓你的陰影始終可見,你需要有一個足夠大的視圖框,包含所有內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM