Skip to content

Commit

Permalink
iOS flip: cover the page with a screenshot so the user doesnt see cha…
Browse files Browse the repository at this point in the history
…nges to the webview before the flip. Also, halfway during the flip, remove the screenshot to reveal the changes.
  • Loading branch information
EddyVerbruggen committed Sep 25, 2014
1 parent 249dbe5 commit e1db8b1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/ios/NativePageTransitions.m
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ - (void) flip:(CDVInvokedUrlCommand*)command {
// duration is passed in ms, but needs to be in sec here
duration = duration / 1000;

// overlay the webview with a screenshot to prevent the user from seeing changes in the webview before the flip kicks in
CGSize viewSize = self.viewController.view.bounds.size;

UIGraphicsBeginImageContextWithOptions(viewSize, YES, 0.0);
[self.viewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];

// Read the UIImage object
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

CGFloat width = self.viewController.view.frame.size.width;
CGFloat height = self.viewController.view.frame.size.height;
[_screenShotImageView setFrame:CGRectMake(0, 0, width, height)];

_screenShotImageView = [[UIImageView alloc]initWithFrame:[self.viewController.view.window frame]];
[_screenShotImageView setImage:image];
[UIApplication.sharedApplication.keyWindow.subviews.lastObject insertSubview:_screenShotImageView aboveSubview:self.webView];

UIViewAnimationOptions animationOptions;
if ([direction isEqualToString:@"right"]) {
animationOptions = UIViewAnimationOptionTransitionFlipFromLeft;
Expand All @@ -246,9 +264,13 @@ - (void) flip:(CDVInvokedUrlCommand*)command {

if ([self loadHrefIfPassed:href]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
// remove the screenshot halfway during the transition
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (duration/2) * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
[_screenShotImageView removeFromSuperview];
});
[UIView transitionWithView:self.viewController.view
duration:duration
options:animationOptions | UIViewAnimationOptionAllowAnimatedContent // that last bit prevents screenshot-based animation (https://developer.apple.com/library/ios/documentation/windowsviews/conceptual/viewpg_iphoneos/animatingviews/animatingviews.html)
options:animationOptions | UIViewAnimationOptionAllowAnimatedContent
animations:^{}
completion:^(BOOL finished) {
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
Expand Down

0 comments on commit e1db8b1

Please sign in to comment.