在iPad上呈现带有垂直旋转动画的formSheet modalViewController

时间:2012-09-12 09:28:56

标签: ipad animation centering presentmodalviewcontroller screen-rotation

我在splitViewDetailController中使用了带有splitViewController和tableViewController的iPad应用程序。 我想在使用自定义动画点击tableCell之后显示detailViewController(带有UIModalPresentationFormSheet)。动画应该是tableViewCell的垂直旋转,旋转单元应该转换为detailViewController视图。整个动画应该像iTunes播客中的动画一样。

这是我动画的代码:

CGRect rectInCell = [[self.cellView superview] convertRect:self.cellView.frame toView:cell];
CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:[self.tableView indexPathForCell:cell]];
CGRect rectInSuperview = [self.tableView convertRect:rectInTableView toView:[[[APP_DELEGATE window] subviews] lastObject]];

CGRect imgRect = CGRectMake(rectInSuperview.origin.x+rectInCell.origin.x, rectInSuperview.origin.y+rectInCell.origin.y, rectInCell.size.width, rectInCell.size.height);

self.originalRelativeFrame = imgRect;
self.originalFrame      = self.cellView.frame;
self.originalSuperview  = self.cellView.superview;
self.originalTransform  = self.cellView.transform;

[self.cellView setFrame:imgRect];
if (self.whiteView == nil) {
    self.whiteView = [[UIView alloc] init];
    [self.whiteView setBackgroundColor:[UIColor whiteColor]];
}
[self.whiteView setFrame:imgRect];
[self.whiteView setAlpha:0];

self.cellView.layer.zPosition = self.cellView.frame.size.width;
self.whiteView.layer.zPosition = self.cellView.frame.size.width;

[[[[APP_DELEGATE window] subviews] lastObject] addSubview:self.whiteView];
[[[[APP_DELEGATE window] subviews] lastObject] addSubview:self.cellView];


CATransform3D rotation = CATransform3DMakeRotation(M_PI/2, 0, 1, 0);
CATransform3D scale = CATransform3DMakeScale(1.5,1.5,1);//(540.0/self.cellView.bounds.size.width, 620.0/self.cellView.bounds.size.height, 1);
CGPoint finCent = CGPointMake([[[[APP_DELEGATE window] subviews] lastObject] bounds].size.width/2.0, [[[[APP_DELEGATE window] subviews] lastObject] bounds].size.height/2.0);
//(finCent.x > self.cellView.center.x) ? (finCent.x - self.cellView.center.x) : (self.cellView.center.x - finCent.x);
CGPoint center = CGPointMake((self.cellView.center.x - finCent.x) / 2.0 ,(self.cellView.center.y - finCent.y )/2.0);

[UIView animateWithDuration:ANIMATION_DURATION_HALF delay:0 options:UIViewAnimationOptionCurveEaseOut
                 animations:^{

                     [self.cellView.layer setTransform:CATransform3DConcat(scale, rotation)];
                     [self.cellView setCenter:CGPointMake(self.cellView.center.x - center.x, self.cellView.center.y - center.y)];
                     //[self.cellView setAlpha:0];

                     [self.whiteView.layer setTransform:CATransform3DConcat(scale, rotation)];
                     [self.whiteView setCenter:CGPointMake(self.whiteView.center.x - center.x, self.whiteView.center.y - center.y)];
                     //[self.whiteView setAlpha:1];

                     [self.blackView setAlpha:0.3];

                 }
                 completion:^(BOOL finished) {

                     [self.whiteView setAlpha:1];
                     [self.cellView setAlpha:0];

                     [UIView animateWithDuration:ANIMATION_DURATION_HALF delay:0 options:UIViewAnimationOptionCurveLinear
                                      animations:^{

                                          CATransform3D rotation = CATransform3DMakeRotation(M_PI, 0, 1, 0);
                                          CATransform3D scale = CATransform3DMakeScale(540.0/self.cellView.bounds.size.width, 620.0/self.cellView.bounds.size.height, 1);
                                          CGPoint finCent = CGPointMake([[[[APP_DELEGATE window] subviews] lastObject] bounds].size.width/2.0, [[[[APP_DELEGATE window] subviews] lastObject] bounds].size.height/2.0);

                                          [self.cellView.layer setTransform:CATransform3DConcat(scale, rotation)];
                                          [self.cellView setCenter:finCent];

                                          [self.whiteView.layer setTransform:CATransform3DConcat(scale, rotation)];
                                          [self.whiteView setCenter:finCent];

                                      }
                                      completion:^(BOOL finished) {

                                          [self presentModalViewController:self.modalCtrl animated:YES];


                                      }];

                 }];

但问题是轮换申请。旋转应用程序后,我的detailViewController后面的whiteView不会停留在窗口中间。

我知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

当用户旋转设备时,您应该重新排列视图的布局。

UIViewController类提供了几种方法来解决这个问题:

– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:

查看UIViewController reference

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                            duration:(NSTimeInterval)duration {

       CGRect imgRect = <CALC YOUR NEW FRAME HERE TO ACCOUNT FOR THE DEVICE ORIENTATION>;
       [self.whiteView setFrame:imgRect];
}

计算新帧时,可以使用toInterfaceOrientation参数来了解哪个方向。另一个可能使事情更简单的选择是定义视图center