CTAssetPlayButton.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. MIT License (MIT)
  3. Copyright (c) 2015 Clement CN Tsang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #import <PureLayout/PureLayout.h>
  21. #import "CTAssetPlayButton.h"
  22. #import "NSBundle+CTAssetsPickerController.h"
  23. #import "UIImage+CTAssetsPickerController.h"
  24. @interface CTAssetPlayButton ()
  25. @property (nonatomic, strong) UIVisualEffectView *blurView;
  26. @property (nonatomic, strong) UIVisualEffectView *vibrancyView;
  27. @property (nonatomic, strong) UIView *vibrancyFill;
  28. @property (nonatomic, strong) UIView *highlightedView;
  29. @property (nonatomic, strong) UIView *colorView;
  30. @property (nonatomic, strong) UIImageView *glyphMask;
  31. @property (nonatomic, strong) UIImageView *buttonMask;
  32. @property (nonatomic, assign) BOOL didSetupConstraints;
  33. @end
  34. @implementation CTAssetPlayButton
  35. - (instancetype)initWithFrame:(CGRect)frame
  36. {
  37. self = [super initWithFrame:frame];
  38. if (self)
  39. {
  40. self.isAccessibilityElement = YES;
  41. self.accessibilityTraits = UIAccessibilityTraitButton;
  42. [self setupViews];
  43. [self localize];
  44. }
  45. return self;
  46. }
  47. #pragma mark - Setup
  48. - (void)setupViews
  49. {
  50. [self setupEffectViews];
  51. [self setupHightlightedView];
  52. [self setupColorView];
  53. [self setupMaskViews];
  54. }
  55. - (void)setupEffectViews
  56. {
  57. // Blur effect
  58. UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
  59. UIVisualEffectView *blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  60. blurView.userInteractionEnabled = NO;
  61. self.blurView = blurView;
  62. // Vibrancy effect
  63. UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
  64. UIVisualEffectView *vibrancyView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
  65. vibrancyView.userInteractionEnabled = NO;
  66. self.vibrancyView = vibrancyView;
  67. UIView *vibrancyFill = [UIView newAutoLayoutView];
  68. vibrancyFill.backgroundColor = [UIColor whiteColor];
  69. vibrancyFill.userInteractionEnabled = NO;
  70. self.vibrancyFill = vibrancyFill;
  71. // Add fill to the vibrancy view
  72. [vibrancyView.contentView addSubview:self.vibrancyFill];
  73. [blurView.contentView addSubview:self.vibrancyView];
  74. [self addSubview:blurView];
  75. }
  76. - (void)setupHightlightedView
  77. {
  78. UIView *highlightedView = [UIView newAutoLayoutView];
  79. highlightedView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
  80. highlightedView.userInteractionEnabled = NO;
  81. highlightedView.hidden = YES;
  82. self.highlightedView = highlightedView;
  83. [self addSubview:self.highlightedView];
  84. }
  85. - (void)setupColorView
  86. {
  87. UIView *colorView = [UIView newAutoLayoutView];
  88. colorView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
  89. colorView.userInteractionEnabled = NO;
  90. self.colorView = colorView;
  91. [self addSubview:self.colorView];
  92. }
  93. - (void)setupMaskViews
  94. {
  95. UIImage *glyphMaskImage = [UIImage ctassetsPickerImageNamed:@"VideoPlayGlyphMask"];
  96. UIImageView *glyphMask = [[UIImageView alloc] initWithImage:glyphMaskImage];
  97. glyphMask.userInteractionEnabled = NO;
  98. self.colorView.maskView = glyphMask;
  99. UIImage *buttonMaskImage = [UIImage ctassetsPickerImageNamed:@"VideoPlayButtonMask"];
  100. UIImageView *buttonMask = [[UIImageView alloc] initWithImage:buttonMaskImage];
  101. buttonMask.userInteractionEnabled = NO;
  102. self.maskView = buttonMask;
  103. }
  104. - (void)localize
  105. {
  106. self.accessibilityLabel = CTAssetsPickerLocalizedString(@"Play", nil);
  107. }
  108. - (void)updateConstraints
  109. {
  110. if (!self.didSetupConstraints)
  111. {
  112. CGSize size = [UIImage ctassetsPickerImageNamed:@"VideoPlayButtonMask"].size;
  113. [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
  114. [self autoSetDimensionsToSize:size];
  115. }];
  116. [self.blurView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  117. [self.vibrancyView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  118. [self.vibrancyFill autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  119. [self.highlightedView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  120. [self.colorView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  121. self.didSetupConstraints = YES;
  122. }
  123. [super updateConstraints];
  124. }
  125. #pragma mark - States
  126. - (void)setHighlighted:(BOOL)highlighted
  127. {
  128. super.highlighted = highlighted;
  129. self.highlightedView.hidden = !highlighted;
  130. }
  131. @end