cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agri...@apache.org
Subject ios commit: [CB-2220] Size the splash screen in the same way as the launch image
Date Thu, 28 Feb 2013 13:31:59 GMT
Updated Branches:
  refs/heads/master 45844ddf7 -> 06dcb91e4


[CB-2220] Size the splash screen in the same way as the launch image

Also fixes [CB-2589] (activity indicator not showing).


Project: http://git-wip-us.apache.org/repos/asf/cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-ios/commit/06dcb91e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-ios/tree/06dcb91e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-ios/diff/06dcb91e

Branch: refs/heads/master
Commit: 06dcb91e458bb7ef2b0ae5cae5e8daa3c1639480
Parents: 45844dd
Author: Andrew Grieve <agrieve@chromium.org>
Authored: Thu Feb 28 08:30:31 2013 -0500
Committer: Andrew Grieve <agrieve@chromium.org>
Committed: Thu Feb 28 08:30:31 2013 -0500

----------------------------------------------------------------------
 CordovaLib/Classes/CDVSplashScreen.h |    3 +-
 CordovaLib/Classes/CDVSplashScreen.m |  135 +++++++++++++++++------------
 2 files changed, 82 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/06dcb91e/CordovaLib/Classes/CDVSplashScreen.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVSplashScreen.h b/CordovaLib/Classes/CDVSplashScreen.h
index a0868a0..704ab43 100644
--- a/CordovaLib/Classes/CDVSplashScreen.h
+++ b/CordovaLib/Classes/CDVSplashScreen.h
@@ -23,7 +23,8 @@
 @interface CDVSplashScreen : CDVPlugin {
     UIActivityIndicatorView* _activityView;
     UIImageView* _imageView;
-    UIView* _parentView;
+    NSString* _curImageName;
+    BOOL _visible;
 }
 
 - (void)show:(CDVInvokedUrlCommand*)command;

http://git-wip-us.apache.org/repos/asf/cordova-ios/blob/06dcb91e/CordovaLib/Classes/CDVSplashScreen.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVSplashScreen.m b/CordovaLib/Classes/CDVSplashScreen.m
index faf152b..cd4ed9d 100644
--- a/CordovaLib/Classes/CDVSplashScreen.m
+++ b/CordovaLib/Classes/CDVSplashScreen.m
@@ -19,9 +19,6 @@
 
 #import "CDVSplashScreen.h"
 
-#define kSplashScreenStateShow 0
-#define kSplashScreenStateHide 1
-
 #define kSplashScreenDurationDefault 0.25f
 
 @implementation CDVSplashScreen
@@ -29,19 +26,18 @@
 - (void)pluginInitialize
 {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pageDidLoad)
name:CDVPageDidLoadNotification object:self.webView];
-    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOrientationWillChange:)
name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
 
-    [self show:nil];
+    [self setVisible:YES];
 }
 
 - (void)show:(CDVInvokedUrlCommand*)command
 {
-    [self updateSplashScreenWithState:kSplashScreenStateShow];
+    [self setVisible:YES];
 }
 
 - (void)hide:(CDVInvokedUrlCommand*)command
 {
-    [self updateSplashScreenWithState:kSplashScreenStateHide];
+    [self setVisible:NO];
 }
 
 - (void)pageDidLoad
@@ -50,16 +46,13 @@
 
     // if value is missing, default to yes
     if ((autoHideSplashScreenValue == nil) || [autoHideSplashScreenValue boolValue]) {
-        [self hide:nil];
+        [self setVisible:NO];
     }
 }
 
-- (void)onOrientationWillChange:(NSNotification*)notification
+- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change
context:(void*)context
 {
-    if (_imageView != nil) {
-        UIInterfaceOrientation orientation = [notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey]
intValue];
-        [self updateSplashImageForOrientation:orientation];
-    }
+    [self updateImage];
 }
 
 - (void)createViews
@@ -83,19 +76,42 @@
         topActivityIndicatorStyle = UIActivityIndicatorViewStyleGray;
     }
 
+    UIView* parentView = self.viewController.view;
     _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:topActivityIndicatorStyle];
-    _activityView.tag = 2;
-    _activityView.center = self.viewController.view.center;
+    _activityView.center = CGPointMake(parentView.bounds.size.width / 2, parentView.bounds.size.height
/ 2);
+    _activityView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin
+        | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
     [_activityView startAnimating];
 
+    // Set the frame & image later.
     _imageView = [[UIImageView alloc] init];
-    [self.viewController.view addSubview:_imageView];
-    [self.viewController.view.superview addSubview:_activityView];
-    [self.viewController.view.superview layoutSubviews];
+    [parentView addSubview:_imageView];
+    [parentView addSubview:_activityView];
+
+    // Frame is required when launching in portrait mode.
+    // Bounds for landscape since it captures the rotation.
+    [parentView addObserver:self forKeyPath:@"frame" options:0 context:nil];
+    [parentView addObserver:self forKeyPath:@"bounds" options:0 context:nil];
+
+    [self updateImage];
+}
+
+- (void)destroyViews
+{
+    [_imageView removeFromSuperview];
+    [_activityView removeFromSuperview];
+    _imageView = nil;
+    _activityView = nil;
+    _curImageName = nil;
+
+    [self.viewController.view removeObserver:self forKeyPath:@"frame"];
+    [self.viewController.view removeObserver:self forKeyPath:@"bounds"];
 }
 
-- (void)updateSplashImageForOrientation:(UIInterfaceOrientation)orientation
+// Sets the view's frame and image.
+- (void)updateImage
 {
+    UIInterfaceOrientation orientation = self.viewController.interfaceOrientation;
     // IPHONE (default)
     NSString* imageName = @"Default";
 
@@ -112,15 +128,44 @@
         }
     }
 
-    CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
-    _imageView.frame = CGRectMake(0, statusBarHeight, _imageView.image.size.width, _imageView.image.size.height);
-    _imageView.image = [UIImage imageNamed:imageName];
+    if (![imageName isEqualToString:_curImageName]) {
+        UIImage* img = [UIImage imageNamed:imageName];
+        _imageView.image = img;
+        _curImageName = imageName;
+    }
+
+    [self updateBounds];
 }
 
-- (void)updateSplashScreenWithState:(int)state
+- (void)updateBounds
 {
-    float toAlpha = state == kSplashScreenStateShow ? 1.0f : 0.0f;
-    BOOL hidden = state == kSplashScreenStateShow ? NO : YES;
+    UIImage* img = _imageView.image;
+    CGRect viewBounds = self.viewController.view.bounds;
+    CGRect imgBounds = CGRectMake(0, 0, img.size.width, img.size.height);
+
+    CGFloat imgAspect = imgBounds.size.width / imgBounds.size.height;
+    CGFloat viewAspect = viewBounds.size.width / viewBounds.size.height;
+
+    // This matches the behaviour of the native splash screen.
+    if (viewAspect > imgAspect) {
+        CGFloat ratio = viewBounds.size.width / imgBounds.size.width;
+        imgBounds.size.height *= ratio;
+        imgBounds.size.width *= ratio;
+    } else {
+        CGFloat ratio = viewBounds.size.height / imgBounds.size.height;
+        imgBounds.size.height *= ratio;
+        imgBounds.size.width *= ratio;
+    }
+
+    _imageView.frame = imgBounds;
+}
+
+- (void)setVisible:(BOOL)visible
+{
+    if (visible == _visible) {
+        return;
+    }
+    _visible = visible;
 
     id fadeSplashScreenValue = [self.commandDelegate.settings objectForKey:@"FadeSplashScreen"];
     id fadeSplashScreenDuration = [self.commandDelegate.settings objectForKey:@"FadeSplashScreenDuration"];
@@ -130,44 +175,24 @@
     if ((fadeSplashScreenValue == nil) || ![fadeSplashScreenValue boolValue]) {
         fadeDuration = 0;
     }
-    if (hidden && (_imageView == nil)) {
-        return;
-    } else if (_imageView == nil) {
-        [self createViews];
-        fadeDuration = 0;
-    }
-
-    if (!hidden) {
-        [self updateSplashImageForOrientation:self.viewController.interfaceOrientation];
-    }
 
-    if (fadeDuration == 0) {
-        [_imageView setHidden:hidden];
-        [_activityView setHidden:hidden];
-    } else {
-        if (state == kSplashScreenStateShow) {
-            // reset states
-            [_imageView setHidden:NO];
-            [_activityView setHidden:NO];
-            [_imageView setAlpha:0.0f];
-            [_activityView setAlpha:0.0f];
+    // Never animate the showing of the splash screen.
+    if (visible) {
+        if (_imageView == nil) {
+            [self createViews];
         }
-
+    } else if (fadeDuration == 0) {
+        [self destroyViews];
+    } else {
         [UIView transitionWithView:self.viewController.view
                           duration:fadeDuration
                            options:UIViewAnimationOptionTransitionNone
                         animations:^(void) {
-                [_imageView setAlpha:toAlpha];
-                [_activityView setAlpha:toAlpha];
+                [_imageView setAlpha:0];
+                [_activityView setAlpha:0];
             }
                         completion:^(BOOL finished) {
-                if (state == kSplashScreenStateHide) {
-                    // Clean-up resources.
-                    [_imageView removeFromSuperview];
-                    [_activityView removeFromSuperview];
-                    _imageView = nil;
-                    _activityView = nil;
-                }
+                [self destroyViews];
             }];
     }
 }


Mime
View raw message