2012-04-20 42 views
11

Tôi có một hình ảnh UIView với bóng và phần phụ UIImageView.Xoay và thay đổi kích thước của UIView với bóng

Tôi muốn đổi kích thước chế độ xem khi iPad được xoay và tôi đang cố thực hiện điều này trong cuộc gọi lại willRotateToInterfaceOrientation.

Nếu tôi đặt bóng trên UIView theo cách cơ bản, vòng xoay rất biến dạng; vì vậy tôi muốn một số gợi ý từ những người khác về cách thiết lập layer shadow.shadowPath.

Tôi đã thử tạo hoạt ảnh thay đổi kích thước khung bằng cách sử dụng [UIView animateWithDuration:animations] và đặt shadowPath mới trong cùng một khối, nhưng đường dẫn bóng sẽ chuyển sang kích thước mới.

Và nếu tôi không thay đổi shadowPath của lớp trong khối hình động, nó sẽ không thay đổi.

Từ một vài tìm kiếm tôi đã thực hiện, thay đổi hoạt ảnh cho thuộc tính lớp cần được thực hiện với CABasicAnimation.

Vì vậy, tôi nghĩ rằng câu hỏi có thể là "làm cách nào để tạo hoạt ảnh cho kích thước và lớp thay đổi của UIView cùng một lúc?"

Trả lời

8

Có nhiều mã hơn một chút so với mong muốn nhưng một số thứ như thế này sẽ hoạt động.

CGFloat animationDuration = 5.0; 

    // Create the CABasicAnimation for the shadow 
    CABasicAnimation *shadowAnimation = [CABasicAnimation animationWithKeyPath:@"shadowPath"]; 
    shadowAnimation.duration = animationDuration; 
    shadowAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // Match the easing of the UIView block animation 
    shadowAnimation.fromValue = (id)self.shadowedView.layer.shadowPath; 

    // Animate the frame change on the view 
    [UIView animateWithDuration:animationDuration 
         delay:0.0f 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
        self.shadowedView.frame = CGRectMake(self.shadowedView.frame.origin.x, 
                  self.shadowedView.frame.origin.y, 
                  self.shadowedView.frame.size.width * 2., 
                  self.shadowedView.frame.size.height * 2); 
        } completion:nil]; 

    // Set the toValue for the animation to the new frame of the view 
    shadowAnimation.toValue = (id)[UIBezierPath bezierPathWithRect:self.shadowedView.bounds].CGPath; 

    // Add the shadow path animation 
    [self.shadowedView.layer addAnimation:shadowAnimation forKey:@"shadowPath"]; 

    // Set the new shadow path 
    self.shadowedView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.shadowedView.bounds].CGPath; 
Các vấn đề liên quan