2010-08-04 30 views
6

Vì vậy, tôi hiện đang thử nghiệm hoạt ảnh UIView và tôi nhận thấy rằng bóng được hiển thị bởi CALayer (sử dụng [view.layer setShadowStuffHere]) biến mất ở đầu hoạt ảnh và xuất hiện lại ở cuối hoạt ảnh. Có cách nào tôi có thể giữ những bóng tối và có bóng tối animate cùng với UIView? Tôi đã cố gắng sử dụng bóng mà không có đường viền biên giới, nhưng đó là một ý tưởng tồi tệ, vì tốc độ khung hình giảm xuống như một tảng đá trong quá trình hoạt hình, và tôi đã không nhận được bóng tối.CALayer Shadows biến mất trong một hoạt ảnh UIView

Mã tôi đang sử dụng ngay bây giờ ở bên dưới. Ban đầu bạn nhìn thấy một cái nhìn màu đỏ, và khi khai thác, nó sẽ lật sang một cái nhìn xanh lớn hơn. Kết quả cuối cùng phải là một cái gì đó tương tự như các ứng dụng âm nhạc iPad; khi một album được chọn, nó sẽ lật qua để lộ một cái nhìn ở mặt sau.

- (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]; 

} 

Trả lời

12

Nó chỉ ra rằng khi bạn đang làm một hình ảnh động UIView, nó bỏ qua xem của bạn sở hữu clipsToBounds và clip nó anyways (ít nhất là cho các hình ảnh động kiểu lật), vì vậy nếu bạn muốn bóng tối của bạn luôn luôn được hiển thị , bạn sẽ cần phải có một khung nhìn đủ lớn chứa mọi thứ.

+0

Bạn có thể giải thích một chút không? – sole007

+0

@ sole007: Câu trả lời cụ thể này có thể quá cũ để có liên quan đến ngày hôm nay (và tôi chưa từng có bất kỳ sự cố nào xảy ra với vấn đề này), nhưng trong cơ hội cần thiết, vấn đề là ngay cả khi clipToBounds được đặt để sai, và bạn sử dụng CALayers để render bóng bên ngoài các giới hạn cụ thể của khung nhìn, dựa vào clipsToBounds là sai cho những bóng tối xuất hiện, nó sẽ không quan trọng và những bóng đó sẽ không xuất hiện. Giải pháp là sau đó cho khung giới hạn để chứa các bóng chứ không phải là các bóng nằm bên ngoài giới hạn. –

Các vấn đề liên quan