CTAssetsGridViewCell.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "CTAssetsPickerDefines.h"
  22. #import "CTAssetsGridViewCell.h"
  23. #import "CTAssetsGridSelectedView.h"
  24. #import "PHAsset+CTAssetsPickerController.h"
  25. #import "NSDateFormatter+CTAssetsPickerController.h"
  26. #import "UIImage+CTAssetsPickerController.h"
  27. @interface CTAssetsGridViewCell ()
  28. @property (nonatomic, strong) PHAsset *asset;
  29. @property (nonatomic, strong) UIImageView *disabledImageView;
  30. @property (nonatomic, strong) UIView *disabledView;
  31. @property (nonatomic, strong) UIView *highlightedView;
  32. @property (nonatomic, strong) CTAssetsGridSelectedView *selectedView;
  33. @property (nonatomic, assign) BOOL didSetupConstraints;
  34. @end
  35. @implementation CTAssetsGridViewCell
  36. - (instancetype)initWithFrame:(CGRect)frame
  37. {
  38. if (self = [super initWithFrame:frame])
  39. {
  40. self.opaque = YES;
  41. self.isAccessibilityElement = YES;
  42. self.accessibilityTraits = UIAccessibilityTraitImage;
  43. self.enabled = YES;
  44. self.showsSelectionIndex = NO;
  45. [self setupViews];
  46. }
  47. return self;
  48. }
  49. #pragma mark - Setup
  50. - (void)setupViews
  51. {
  52. CTAssetThumbnailView *thumbnailView = [CTAssetThumbnailView newAutoLayoutView];
  53. self.backgroundView = thumbnailView;
  54. UIImage *disabledImage = [UIImage ctassetsPickerImageNamed:@"GridDisabledAsset"];
  55. disabledImage = [disabledImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
  56. UIImageView *disabledImageView = [[UIImageView alloc] initWithImage:disabledImage];
  57. disabledImageView.tintColor = CTAssetsPikcerThumbnailTintColor;
  58. self.disabledImageView = disabledImageView;
  59. UIView *disabledView = [UIView newAutoLayoutView];
  60. disabledView.backgroundColor = CTAssetsGridViewCellDisabledColor;
  61. disabledView.hidden = YES;
  62. [disabledView addSubview:self.disabledImageView];
  63. self.disabledView = disabledView;
  64. [self addSubview:self.disabledView];
  65. UIView *highlightedView = [UIView newAutoLayoutView];
  66. highlightedView.backgroundColor = CTAssetsGridViewCellHighlightedColor;
  67. highlightedView.hidden = YES;
  68. self.highlightedView = highlightedView;
  69. [self addSubview:self.highlightedView];
  70. CTAssetsGridSelectedView *selectedView = [CTAssetsGridSelectedView newAutoLayoutView];
  71. selectedView.hidden = YES;
  72. self.selectedView = selectedView;
  73. [self addSubview:self.selectedView];
  74. }
  75. #pragma mark - Apperance
  76. - (UIColor *)disabledColor
  77. {
  78. return self.disabledView.backgroundColor;
  79. }
  80. - (void)setDisabledColor:(UIColor *)disabledColor
  81. {
  82. UIColor *color = (disabledColor) ? disabledColor : CTAssetsGridViewCellDisabledColor;
  83. self.disabledView.backgroundColor = color;
  84. }
  85. - (UIColor *)highlightedColor
  86. {
  87. return self.highlightedView.backgroundColor;
  88. }
  89. - (void)setHighlightedColor:(UIColor *)highlightedColor
  90. {
  91. UIColor *color = (highlightedColor) ? highlightedColor : CTAssetsGridViewCellHighlightedColor;
  92. self.highlightedView.backgroundColor = color;
  93. }
  94. #pragma mark - Accessors
  95. - (void)setEnabled:(BOOL)enabled
  96. {
  97. _enabled = enabled;
  98. self.disabledView.hidden = enabled;
  99. }
  100. - (void)setHighlighted:(BOOL)highlighted
  101. {
  102. super.highlighted = highlighted;
  103. self.highlightedView.hidden = !highlighted;
  104. }
  105. - (void)setSelected:(BOOL)selected
  106. {
  107. super.selected = selected;
  108. self.selectedView.hidden = !selected;
  109. }
  110. - (void)setShowsSelectionIndex:(BOOL)showsSelectionIndex
  111. {
  112. _showsSelectionIndex = showsSelectionIndex;
  113. self.selectedView.showsSelectionIndex = showsSelectionIndex;
  114. }
  115. - (void)setSelectionIndex:(NSUInteger)selectionIndex
  116. {
  117. _selectionIndex = selectionIndex;
  118. self.selectedView.selectionIndex = selectionIndex;
  119. }
  120. #pragma mark - Update auto layout constraints
  121. - (void)updateConstraints
  122. {
  123. if (!self.didSetupConstraints)
  124. {
  125. [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
  126. [self.backgroundView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  127. [self.disabledView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  128. [self.highlightedView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  129. [self.selectedView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
  130. }];
  131. [self.disabledImageView autoCenterInSuperview];
  132. self.didSetupConstraints = YES;
  133. }
  134. [super updateConstraints];
  135. }
  136. - (void)bind:(PHAsset *)asset
  137. {
  138. self.asset = asset;
  139. [self setNeedsUpdateConstraints];
  140. [self updateConstraintsIfNeeded];
  141. }
  142. #pragma mark - Accessibility Label
  143. - (NSString *)accessibilityLabel
  144. {
  145. if (self.selectedView.accessibilityLabel)
  146. return [NSString stringWithFormat:@"%@, %@", self.selectedView.accessibilityLabel, self.asset.accessibilityLabel];
  147. else
  148. return self.asset.accessibilityLabel;
  149. }
  150. @end