VLCMovieViewControlPanelViewController.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*****************************************************************************
  2. * VLCMovieViewControlPanelViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan@tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCMovieViewControlPanelViewController.h"
  13. @interface VLCMovieViewControlPanelViewController ()
  14. @property (nonatomic, weak) IBOutlet UIView *playbackControls;
  15. @property (nonatomic, assign) BOOL compactMode;
  16. @end
  17. @implementation VLCMovieViewControlPanelViewController
  18. static const CGFloat maxCompactWidth = 420.0;
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. _playbackSpeedButton.accessibilityLabel = NSLocalizedString(@"PLAYBACK_SPEED", nil);
  22. _playbackSpeedButton.isAccessibilityElement = YES;
  23. _trackSwitcherButton.accessibilityLabel = NSLocalizedString(@"OPEN_TRACK_PANEL", nil);
  24. _trackSwitcherButton.isAccessibilityElement = YES;
  25. _bwdButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
  26. _bwdButton.isAccessibilityElement = YES;
  27. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
  28. [_playPauseButton addGestureRecognizer:longPressRecognizer];
  29. _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
  30. _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
  31. _playPauseButton.isAccessibilityElement = YES;
  32. _fwdButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
  33. _fwdButton.isAccessibilityElement = YES;
  34. _videoFilterButton.accessibilityLabel = NSLocalizedString(@"VIDEO_FILTER", nil);
  35. _videoFilterButton.isAccessibilityElement = YES;
  36. // HACK: get the slider from volume view
  37. UISlider *volumeSlider = nil;
  38. for (id aView in self.volumeView.subviews){
  39. if ([aView isKindOfClass:[UISlider class]]){
  40. volumeSlider = (UISlider *)aView;
  41. break;
  42. }
  43. }
  44. [volumeSlider addTarget:nil action:@selector(volumeSliderAction:) forControlEvents:UIControlEventValueChanged];
  45. [self.volumeView setVolumeThumbImage:[UIImage imageNamed:@"sliderKnob"] forState:UIControlStateNormal];
  46. _compactMode = YES;
  47. [self setupConstraints:YES];
  48. }
  49. - (void)viewWillAppear:(BOOL)animated
  50. {
  51. [super viewWillAppear:animated];
  52. self.volumeView.hidden = NO;
  53. }
  54. - (void)viewDidDisappear:(BOOL)animated
  55. {
  56. [super viewDidDisappear:animated];
  57. self.volumeView.hidden = YES;
  58. }
  59. - (void) viewWillLayoutSubviews {
  60. [super viewWillLayoutSubviews];
  61. [self.view setNeedsUpdateConstraints];
  62. }
  63. - (void) updateViewConstraints {
  64. BOOL compactMode = CGRectGetWidth(self.view.frame) <= maxCompactWidth;
  65. if (self.compactMode != compactMode) {
  66. self.compactMode = compactMode;
  67. [self setupConstraints:compactMode];
  68. }
  69. [super updateViewConstraints];
  70. }
  71. - (void) setupConstraints:(BOOL)compactMode {
  72. UIView *superview = self.view.superview;
  73. NSArray *oldConstraints = [self.view.constraints filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSLayoutConstraint * evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  74. if (!superview) {
  75. return YES;
  76. }
  77. return evaluatedObject.firstItem != superview && evaluatedObject.secondItem != superview;
  78. }]];
  79. [self.view removeConstraints:oldConstraints];
  80. NSDictionary *viewsDict = @{@"speed" : self.playbackSpeedButton,
  81. @"track" : self.trackSwitcherButton,
  82. @"playback" : self.playbackControls,
  83. @"filter" : self.videoFilterButton,
  84. @"actions" : self.moreActionsButton,
  85. @"volume" : self.volumeView,
  86. };
  87. NSMutableArray *constraints = [NSMutableArray array];
  88. if (compactMode) {
  89. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[speed]-8-[track]-(>=8)-[playback]-(>=8)-[filter]-8-[actions]-|"
  90. options:NSLayoutFormatAlignAllCenterY
  91. metrics:nil
  92. views:viewsDict]];
  93. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=10)-[volume]-(>=10)-|"
  94. options:0
  95. metrics:nil
  96. views:viewsDict]];
  97. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[playback]-[volume(==40)]-|"
  98. options:NSLayoutFormatAlignAllCenterX
  99. metrics:nil
  100. views:viewsDict]];
  101. } else {
  102. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[volume]-(>=8)-[playback]-(>=8)-[speed]-8-[track]-8-[filter]-8-[actions]-|"
  103. options:NSLayoutFormatAlignAllCenterY
  104. metrics:nil
  105. views:viewsDict]];
  106. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[playback]-|"
  107. options:0
  108. metrics:nil
  109. views:viewsDict]];
  110. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[volume(==40)]"
  111. options:0
  112. metrics:nil
  113. views:viewsDict]];
  114. }
  115. [constraints addObject:[NSLayoutConstraint constraintWithItem:self.playbackControls
  116. attribute:NSLayoutAttributeCenterX
  117. relatedBy:NSLayoutRelationEqual
  118. toItem:self.view
  119. attribute:NSLayoutAttributeCenterX
  120. multiplier:1.0
  121. constant:0]];
  122. [self.view addConstraints:constraints];
  123. }
  124. // needed for < iOS 8
  125. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  126. [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
  127. [self.view setNeedsUpdateConstraints];
  128. }
  129. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  130. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  131. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  132. [self.view setNeedsUpdateConstraints];
  133. } completion:nil];
  134. }
  135. - (VLCPlaybackController *)playbackController
  136. {
  137. if (!_playbackController) {
  138. _playbackController = [VLCPlaybackController sharedInstance];
  139. }
  140. return _playbackController;
  141. }
  142. - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
  143. {
  144. switch (recognizer.state) {
  145. case UIGestureRecognizerStateBegan:
  146. { UIImage *image = [UIImage imageNamed:@"stopIcon"];
  147. [_playPauseButton setImage:image forState:UIControlStateNormal];
  148. }
  149. break;
  150. case UIGestureRecognizerStateEnded:
  151. [self.playbackController stopPlayback];
  152. break;
  153. case UIGestureRecognizerStateCancelled:
  154. case UIGestureRecognizerStateFailed:
  155. [self updatePlayPauseButton];
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. - (void)updateButtons {
  162. [self updatePlayPauseButton];
  163. self.trackSwitcherButton.hidden = !self.playbackController.currentMediaHasTrackToChooseFrom;
  164. self.videoFilterButton.hidden = self.playbackController.audioOnlyPlaybackSession;
  165. }
  166. - (void)updatePlayPauseButton
  167. {
  168. const BOOL isPlaying = self.playbackController.isPlaying;
  169. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  170. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  171. }
  172. @end