|
10 | 10 | #import <MediaPlayer/MediaPlayer.h>
|
11 | 11 |
|
12 | 12 | @implementation XOSplashVideoController {
|
| 13 | + NSURL *_portraitUrl; |
| 14 | + NSString *_portraitImageName; |
| 15 | + NSURL *_landscapeUrl; |
| 16 | + NSString *_landscapeImageName; |
13 | 17 | MPMoviePlayerController *_player;
|
14 | 18 | UIImageView *_backgroundImageView;
|
15 | 19 | }
|
16 | 20 |
|
17 | 21 | @synthesize delegate = _delegate;
|
18 | 22 |
|
19 |
| -- (id)initWithVideoURL:(NSURL *)url |
20 |
| - imageName:(NSString *)imageName |
| 23 | +- (id)initWithVideoPortraitUrl:(NSURL *)portraitUrl |
| 24 | + portraitImageName:(NSString *)portraitImageName |
| 25 | + landscapeUrl:(NSURL *)landscapeUrl |
| 26 | + landscapeImageName:(NSString *)landscapeImageName |
21 | 27 | delegate:(NSObject<XOSplashVideoDelegate> *)delegate;
|
22 | 28 | {
|
| 29 | + NSLog(@"init"); |
23 | 30 | self = [super init];
|
24 | 31 | if (self) {
|
| 32 | + _portraitUrl = portraitUrl; |
| 33 | + _portraitImageName = portraitImageName; |
| 34 | + _landscapeUrl = landscapeUrl; |
| 35 | + _landscapeImageName = landscapeImageName; |
25 | 36 | _delegate = delegate;
|
26 | 37 |
|
27 | 38 | self.wantsFullScreenLayout = YES;
|
| 39 | + } |
| 40 | + return self; |
| 41 | +} |
28 | 42 |
|
29 |
| - CGRect frame = [[UIScreen mainScreen] bounds]; |
30 |
| - UIApplication *application = [UIApplication sharedApplication]; |
31 |
| - UIWindow *window = application.delegate.window; |
32 |
| - |
33 |
| - // put a background image in the window, so that it'll show as soon as the splash |
34 |
| - // goes away, this fixes most of the black flash |
35 |
| - UIImage *image = [UIImage imageNamed:imageName]; |
36 |
| - _backgroundImageView = [[UIImageView alloc] initWithImage:image]; |
37 |
| - CGRect backgroundFrame = frame; |
38 |
| - if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { |
39 |
| - // shift the background frame down to account for the 20px cut out of the image |
40 |
| - backgroundFrame.origin.y += 20; |
41 |
| - backgroundFrame.size.height -= 20; |
42 |
| - } |
43 |
| - _backgroundImageView.frame = backgroundFrame; |
44 |
| - _backgroundImageView.userInteractionEnabled = NO; |
45 |
| - [window addSubview:_backgroundImageView]; |
| 43 | +- (void)viewDidLayoutSubviews |
| 44 | +{ |
| 45 | + NSLog(@"viewDidLayoutSubviews"); |
| 46 | + [super viewDidLayoutSubviews]; |
| 47 | + if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) { |
| 48 | + // this won't be called if we're in portrait button bottom, so we need to call it manually |
| 49 | + [self didRotateFromInterfaceOrientation:UIInterfaceOrientationPortrait]; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation |
| 54 | +{ |
| 55 | + if (_backgroundImageView) { |
| 56 | + // once we've started don't allow rotates |
| 57 | + return NO; |
| 58 | + } |
| 59 | + switch (toInterfaceOrientation) { |
| 60 | + case UIInterfaceOrientationPortrait: |
| 61 | + case UIInterfaceOrientationPortraitUpsideDown: |
| 62 | + NSLog(@"shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation: portrait"); |
| 63 | + return _portraitUrl && _portraitImageName; |
| 64 | + case UIInterfaceOrientationLandscapeLeft: |
| 65 | + case UIInterfaceOrientationLandscapeRight: |
| 66 | + NSLog(@"shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation: landscape"); |
| 67 | + return _landscapeUrl && _landscapeImageName; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { |
| 72 | + NSLog(@"didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation"); |
| 73 | + |
| 74 | + CGRect frame = [[UIScreen mainScreen] bounds]; |
| 75 | + UIApplication *application = [UIApplication sharedApplication]; |
| 76 | + UIWindow *window = application.delegate.window; |
| 77 | + |
| 78 | + NSURL *url = _portraitUrl; |
| 79 | + NSString *imageName = _portraitImageName; |
| 80 | + |
| 81 | + UIInterfaceOrientation orientation = [application statusBarOrientation]; |
| 82 | + if (UIInterfaceOrientationIsLandscape(orientation)) { |
| 83 | + url = _landscapeUrl; |
| 84 | + imageName = _landscapeImageName; |
46 | 85 |
|
47 |
| - _player = [[MPMoviePlayerController alloc] initWithContentURL:url]; |
48 |
| - // video doesn't need to be shifted down |
49 |
| - _player.view.frame = frame; |
50 |
| - _player.useApplicationAudioSession = NO; |
51 |
| - _player.controlStyle = MPMovieControlStyleNone; |
52 |
| - _player.scalingMode = MPMovieScalingModeNone; |
53 |
| - // we're going to install it once it's loaded and play it then |
54 |
| - _player.shouldAutoplay = NO; |
55 |
| - // there's still a little bit of black flash left when the player is inserted |
56 |
| - // as it starts to play, adding the splash image to the background of the player |
57 |
| - // will get rid of it |
58 |
| - UIImageView *playerBackground = [[UIImageView alloc] initWithImage:image]; |
59 |
| - playerBackground.frame = backgroundFrame; |
60 |
| - _player.view.userInteractionEnabled = NO; |
61 |
| - [_player.backgroundView addSubview:playerBackground]; |
62 |
| - |
63 |
| - // tell us when the video has loaded |
64 |
| - [[NSNotificationCenter defaultCenter] addObserver:self |
65 |
| - selector:@selector(splashStateDidChange:) |
66 |
| - name:MPMoviePlayerLoadStateDidChangeNotification |
67 |
| - object:_player]; |
68 |
| - |
69 |
| - // tell us when the video has finished playing |
70 |
| - [[NSNotificationCenter defaultCenter] addObserver:self |
71 |
| - selector:@selector(splashDidFinish:) |
72 |
| - name:MPMoviePlayerPlaybackDidFinishNotification |
73 |
| - object:_player]; |
| 86 | + CGFloat tmp = frame.size.width; |
| 87 | + frame.size.width = frame.size.height; |
| 88 | + frame.size.height = tmp; |
| 89 | + tmp = (frame.size.width - frame.size.height) / 2; |
| 90 | + frame.origin.x = -tmp; |
| 91 | + frame.origin.y = tmp; |
| 92 | + CGFloat rotation = orientation == UIInterfaceOrientationLandscapeLeft ? -M_PI_2 : M_PI_2; |
| 93 | + window.transform = CGAffineTransformMakeRotation(rotation); |
| 94 | + } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { |
| 95 | + window.transform = CGAffineTransformMakeRotation(M_PI); |
74 | 96 | }
|
75 |
| - return self; |
| 97 | + |
| 98 | + // put a background image in the window, so that it'll show as soon as the splash |
| 99 | + // goes away, this fixes most of the black flash |
| 100 | + UIImage *image = [UIImage imageNamed:imageName]; |
| 101 | + _backgroundImageView = [[UIImageView alloc] initWithImage:image]; |
| 102 | + CGRect backgroundFrame = frame; |
| 103 | + if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { |
| 104 | + // shift the background frame down to account for the 20px cut out of the image |
| 105 | + backgroundFrame.origin.y += 20; |
| 106 | + backgroundFrame.size.height -= 20; |
| 107 | + } |
| 108 | + _backgroundImageView.frame = backgroundFrame; |
| 109 | + _backgroundImageView.userInteractionEnabled = NO; |
| 110 | + [window addSubview:_backgroundImageView]; |
| 111 | + |
| 112 | + _player = [[MPMoviePlayerController alloc] initWithContentURL:url]; |
| 113 | + // video doesn't need to be shifted down |
| 114 | + _player.view.frame = frame; |
| 115 | + _player.useApplicationAudioSession = NO; |
| 116 | + _player.controlStyle = MPMovieControlStyleNone; |
| 117 | + _player.scalingMode = MPMovieScalingModeNone; |
| 118 | + // we're going to install it once it's loaded and play it then |
| 119 | + _player.shouldAutoplay = NO; |
| 120 | + // there's still a little bit of black flash left when the player is inserted |
| 121 | + // as it starts to play, adding the splash image to the background of the player |
| 122 | + // will get rid of it |
| 123 | + UIImageView *playerBackground = [[UIImageView alloc] initWithImage:image]; |
| 124 | + CGSize imageSize = image.size; |
| 125 | + if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { |
| 126 | + playerBackground.frame = CGRectMake(0, 20, imageSize.width, imageSize.height); |
| 127 | + } |
| 128 | + _player.view.userInteractionEnabled = NO; |
| 129 | + [_player.backgroundView addSubview:playerBackground]; |
| 130 | + |
| 131 | + // tell us when the video has loaded |
| 132 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 133 | + selector:@selector(splashStateDidChange:) |
| 134 | + name:MPMoviePlayerLoadStateDidChangeNotification |
| 135 | + object:_player]; |
| 136 | + |
| 137 | + // tell us when the video has finished playing |
| 138 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 139 | + selector:@selector(splashDidFinish:) |
| 140 | + name:MPMoviePlayerPlaybackDidFinishNotification |
| 141 | + object:_player]; |
| 142 | + |
| 143 | + [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; |
76 | 144 | }
|
77 | 145 |
|
78 | 146 | - (void)splashStateDidChange:(NSNotification *)notification
|
@@ -109,6 +177,8 @@ - (void)splashDidFinish:(NSNotification *)notification
|
109 | 177 | // take our player out of the window, we're done with it
|
110 | 178 | [_player.view removeFromSuperview];
|
111 | 179 | _player = nil;
|
| 180 | + |
| 181 | + [UIApplication sharedApplication].delegate.window.transform = CGAffineTransformIdentity; |
112 | 182 | }
|
113 | 183 |
|
114 | 184 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
|
|
0 commit comments