ZXVideoPlayerTimeIndicatorView.m 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // ZXVideoPlayerTimeIndicatorView.m
  3. // ZXVideoPlayer
  4. //
  5. // Created by Shawn on 16/4/21.
  6. // Copyright © 2016年 Shawn. All rights reserved.
  7. //
  8. #import "ZXVideoPlayerTimeIndicatorView.h"
  9. #import "ZXVideoPlayerControlView.h"
  10. static const CGFloat kViewSpacing = 15.0;
  11. static const CGFloat kTimeIndicatorAutoFadeOutTimeInterval = 1.0;
  12. @interface ZXVideoPlayerTimeIndicatorView ()
  13. @property (nonatomic, strong, readwrite) UIImageView *arrowImageView;
  14. @property (nonatomic, strong, readwrite) UILabel *timeLabel;
  15. @end
  16. @implementation ZXVideoPlayerTimeIndicatorView
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.hidden = YES;
  22. self.layer.cornerRadius = 5;
  23. self.layer.masksToBounds = YES;
  24. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
  25. [self createTimeIndicator];
  26. }
  27. return self;
  28. }
  29. - (void)setLabelText:(NSString *)labelText
  30. {
  31. // _labelText = [labelText copy];
  32. self.hidden = NO;
  33. self.timeLabel.text = labelText;
  34. // 防止重叠显示
  35. if (self.superview.accessibilityIdentifier) {
  36. ZXVideoPlayerControlView *playerView = (ZXVideoPlayerControlView *)self.superview;
  37. playerView.brightnessIndicatorView.hidden = YES;
  38. playerView.volumeIndicatorView.hidden = YES;
  39. } else {
  40. self.superview.accessibilityIdentifier = @"";
  41. }
  42. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(animateHide) object:nil];
  43. [self performSelector:@selector(animateHide) withObject:nil afterDelay:kTimeIndicatorAutoFadeOutTimeInterval];
  44. }
  45. - (void)layoutSubviews
  46. {
  47. [super layoutSubviews];
  48. if (self.playState == ZXTimeIndicatorPlayStateRewind) {
  49. [self.arrowImageView setImage:[UIImage imageNamed:@"zx-video-player-rewind"]];
  50. } else {
  51. [self.arrowImageView setImage:[UIImage imageNamed:@"zx-video-player-fastForward"]];
  52. }
  53. }
  54. - (void)createTimeIndicator
  55. {
  56. CGFloat margin = (kVideoTimeIndicatorViewSide - 24 - 12 - kViewSpacing) / 2;
  57. _arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kVideoTimeIndicatorViewSide - 44) / 2, margin, 44, 24)];
  58. [self addSubview:_arrowImageView];
  59. _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, margin + 24 + kViewSpacing, kVideoTimeIndicatorViewSide, 12)];
  60. _timeLabel.textColor = [UIColor whiteColor];
  61. _timeLabel.backgroundColor = [UIColor clearColor];
  62. _timeLabel.font = [UIFont systemFontOfSize:12];
  63. _timeLabel.textAlignment = NSTextAlignmentCenter;
  64. [self addSubview:_timeLabel];
  65. }
  66. - (void)animateHide
  67. {
  68. [UIView animateWithDuration:.3 animations:^{
  69. self.alpha = 0;
  70. } completion:^(BOOL finished) {
  71. self.hidden = YES;
  72. self.alpha = 1;
  73. self.superview.accessibilityIdentifier = nil;
  74. }];
  75. }
  76. @end