UIView to Form Sheet flip animation rotation issue
- by GSD
I have this view to form sheet flip animation that works great in portrait mode but not in any other orientations. In my code, I use it with a UIView embedded in another UIView, which is in the root view. The animation will be rotated. Anyone has an idea why?
- (void)presentModalViewControllerWithFlipAnimation:(UIViewController *)modalViewController fromView:(UIView *)fromView {
UIWindow* window = [[UIApplication sharedApplication] windows][0];
_fromViewSnapshot = [[UIImageView alloc] initWithFrame:fromView.bounds];
UIGraphicsBeginImageContext(fromView.bounds.size);
[fromView.layer renderInContext:UIGraphicsGetCurrentContext()];
[_fromViewSnapshot setImage:UIGraphicsGetImageFromCurrentImageContext()];
[_fromViewSnapshot sizeToFit];
UIGraphicsEndImageContext();
_fromViewSnapshot.autoresizesSubviews = YES;
_fromViewSnapshot.frame = [fromView.superview convertRect:fromView.frame toView:window];
[window addSubview:_fromViewSnapshot];
fromView.hidden = YES;
modalViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:modalViewController animated:NO completion:nil];
UIView __weak *presentedView = modalViewController.view.superview;
UIGraphicsBeginImageContext(presentedView.bounds.size);
[presentedView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* modalSnapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIView __weak *presentationSuperview = presentedView.superview;
[presentedView removeFromSuperview];
UIImageView* presentationSnapshotView = [[UIImageView alloc] initWithImage:modalSnapshot];
presentationSnapshotView.autoresizingMask = UIViewAutoresizingNone;
[_fromViewSnapshot.superview bringSubviewToFront:_fromViewSnapshot];
[UIView animateWithDuration:kFlipAnimationScaleSpeed
                      delay:0
                    options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionBeginFromCurrentState
                 animations: ^{
                     [UIView animateWithDuration:kFlipAnimationScaleSpeed
                                           delay:0
                                         options:UIViewAnimationOptionBeginFromCurrentState
                                      animations: ^{
                                          _fromViewSnapshot.frame = [_fromViewSnapshot.superview convertRect:presentedView.frame fromView:presentationSuperview];
                                      }
                                      completion:nil];
                 }
                 completion:^(BOOL finished) {
                     [UIView setAnimationBeginsFromCurrentState:YES];
                     [UIView transitionWithView:_fromViewSnapshot
                                       duration:kFlipAnimationSpeed
                                        options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionTransitionFlipFromRight
                                     animations: ^{
                                         presentationSnapshotView.frame = _fromViewSnapshot.bounds;
                                         [_fromViewSnapshot addSubview:presentationSnapshotView];
                                     }
                                     completion:^(BOOL finished) {
                                         [presentationSuperview addSubview:presentedView];
                                         [presentationSnapshotView removeFromSuperview];
                                         [_fromViewSnapshot removeFromSuperview];
                                     }];
                 }];
}
- (void)dismissViewControllerWithFlipAnimation:(UIView*)fromView completion:(void (^)(void))completion {
UIWindow* window = [[UIApplication sharedApplication] windows][0];
UIView __weak *presentedView = self.presentedViewController.view.superview;
UIGraphicsBeginImageContext(presentedView.bounds.size);
[presentedView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* modalSnapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[presentedView removeFromSuperview];
UIImageView* presentationSnapshotView = [[UIImageView alloc] initWithImage:modalSnapshot];
presentationSnapshotView.autoresizingMask = UIViewAutoresizingNone;
presentationSnapshotView.frame = presentedView.frame;
[window addSubview:presentationSnapshotView];
[UIView animateWithDuration:kFlipAnimationScaleSpeed
                      delay:0
                    options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionBeginFromCurrentState
                 animations: ^{
                     [UIView animateWithDuration:kFlipAnimationScaleSpeed
                                           delay:0
                                         options:UIViewAnimationOptionBeginFromCurrentState
                                      animations: ^{
                                          presentationSnapshotView.frame = [fromView.superview convertRect:fromView.frame toView:window];
                                      }
                                      completion:nil];
                 }
                 completion:^(BOOL finished) {
                     [UIView setAnimationBeginsFromCurrentState:YES];
                     [UIView transitionWithView:presentationSnapshotView
                                       duration:kFlipAnimationSpeed
                                        options:UIViewAnimationOptionCurveEaseIn|UIViewAnimationOptionTransitionFlipFromLeft
                                     animations: ^{
                                         _fromViewSnapshot.frame = presentationSnapshotView.bounds;
                                         [presentationSnapshotView setImage:[(UIImageView*)_fromViewSnapshot image]];
                                         [self dismissViewControllerAnimated:NO completion:nil];
                                     }
                                     completion:^(BOOL finished) {
                                         [presentationSnapshotView removeFromSuperview];
                                         _fromViewSnapshot = nil;
                                         completion();
                                     }];
                 }];
}