VLCMovieViewControlPanelViewController.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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) viewWillLayoutSubviews {
  50. [super viewWillLayoutSubviews];
  51. [self.view setNeedsUpdateConstraints];
  52. }
  53. - (void) updateViewConstraints {
  54. BOOL compactMode = CGRectGetWidth(self.view.frame) <= maxCompactWidth;
  55. if (self.compactMode != compactMode) {
  56. self.compactMode = compactMode;
  57. [self setupConstraints:compactMode];
  58. }
  59. [super updateViewConstraints];
  60. }
  61. - (void) setupConstraints:(BOOL)compactMode {
  62. UIView *superview = self.view.superview;
  63. NSArray *oldConstraints = [self.view.constraints filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(NSLayoutConstraint * evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  64. if (!superview) {
  65. return YES;
  66. }
  67. return evaluatedObject.firstItem != superview && evaluatedObject.secondItem != superview;
  68. }]];
  69. [self.view removeConstraints:oldConstraints];
  70. NSDictionary *viewsDict = @{@"speed" : self.playbackSpeedButton,
  71. @"track" : self.trackSwitcherButton,
  72. @"playback" : self.playbackControls,
  73. @"filter" : self.videoFilterButton,
  74. @"actions" : self.moreActionsButton,
  75. @"volume" : self.volumeView,
  76. };
  77. NSMutableArray *constraints = [NSMutableArray array];
  78. if (compactMode) {
  79. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[speed]-8-[track]-(>=8)-[playback]-(>=8)-[filter]-8-[actions]-|"
  80. options:NSLayoutFormatAlignAllCenterY
  81. metrics:nil
  82. views:viewsDict]];
  83. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=10)-[volume]-(>=10)-|"
  84. options:0
  85. metrics:nil
  86. views:viewsDict]];
  87. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[playback]-[volume(==40)]-|"
  88. options:NSLayoutFormatAlignAllCenterX
  89. metrics:nil
  90. views:viewsDict]];
  91. } else {
  92. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[volume]-(>=8)-[playback]-(>=8)-[speed]-8-[track]-8-[filter]-8-[actions]-|"
  93. options:NSLayoutFormatAlignAllCenterY
  94. metrics:nil
  95. views:viewsDict]];
  96. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[playback]-|"
  97. options:0
  98. metrics:nil
  99. views:viewsDict]];
  100. [constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[volume(==40)]"
  101. options:0
  102. metrics:nil
  103. views:viewsDict]];
  104. }
  105. [constraints addObject:[NSLayoutConstraint constraintWithItem:self.playbackControls
  106. attribute:NSLayoutAttributeCenterX
  107. relatedBy:NSLayoutRelationEqual
  108. toItem:self.view
  109. attribute:NSLayoutAttributeCenterX
  110. multiplier:1.0
  111. constant:0]];
  112. [self.view addConstraints:constraints];
  113. }
  114. // needed for < iOS 8
  115. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  116. [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
  117. [self.view setNeedsUpdateConstraints];
  118. }
  119. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  120. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  121. [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
  122. [self.view setNeedsUpdateConstraints];
  123. } completion:nil];
  124. }
  125. - (VLCPlaybackController *)playbackController
  126. {
  127. if (!_playbackController) {
  128. _playbackController = [VLCPlaybackController sharedInstance];
  129. }
  130. return _playbackController;
  131. }
  132. - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
  133. {
  134. switch (recognizer.state) {
  135. case UIGestureRecognizerStateBegan:
  136. { UIImage *image = [UIImage imageNamed:@"stopIcon"];
  137. [_playPauseButton setImage:image forState:UIControlStateNormal];
  138. }
  139. break;
  140. case UIGestureRecognizerStateEnded:
  141. [self.playbackController stopPlayback];
  142. break;
  143. case UIGestureRecognizerStateCancelled:
  144. case UIGestureRecognizerStateFailed:
  145. [self updatePlayPauseButton];
  146. break;
  147. default:
  148. break;
  149. }
  150. }
  151. - (void)updateButtons {
  152. [self updatePlayPauseButton];
  153. self.trackSwitcherButton.hidden = !self.playbackController.currentMediaHasTrackToChooseFrom;
  154. self.videoFilterButton.hidden = self.playbackController.audioOnlyPlaybackSession;
  155. }
  156. - (void)updatePlayPauseButton
  157. {
  158. const BOOL isPlaying = self.playbackController.isPlaying;
  159. UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
  160. [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
  161. }
  162. @end