CTAssetThumbnailStacks.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "CTAssetThumbnailStacks.h"
  23. #import "CTAssetThumbnailView.h"
  24. @interface CTAssetThumbnailStacks ()
  25. @property (nonatomic, copy) NSArray *thumbnailViews;
  26. @property (nonatomic, assign) UIEdgeInsets edgeInsets;
  27. @property (nonatomic, assign) BOOL didSetupConstraints;
  28. @end
  29. @implementation CTAssetThumbnailStacks
  30. - (instancetype)initWithFrame:(CGRect)frame
  31. {
  32. if (self = [super initWithFrame:frame])
  33. {
  34. _edgeInsets = UIEdgeInsetsMake(4.0, 0, 0, 0);
  35. self.opaque = YES;
  36. self.clipsToBounds = YES;
  37. self.isAccessibilityElement = NO;
  38. [self setupViews];
  39. }
  40. return self;
  41. }
  42. #pragma mark - Setup
  43. - (void)setupViews
  44. {
  45. NSMutableArray *thumbnailViews = [NSMutableArray new];
  46. for (NSUInteger index = 0; index < 3; index++)
  47. {
  48. CTAssetThumbnailView *thumbnailView = [CTAssetThumbnailView newAutoLayoutView];
  49. thumbnailView.showsDuration = NO;
  50. thumbnailView.layer.borderColor = [UIColor whiteColor].CGColor;
  51. thumbnailView.layer.borderWidth = 0.5f;
  52. [thumbnailViews addObject:thumbnailView];
  53. [self insertSubview:thumbnailView atIndex:0];
  54. }
  55. self.thumbnailViews = [NSArray arrayWithArray:thumbnailViews];
  56. }
  57. #pragma markt - Setters
  58. - (void)setThumbnailSize:(CGSize)thumbnailSize
  59. {
  60. _thumbnailSize = thumbnailSize;
  61. [self setNeedsUpdateConstraints];
  62. [self updateConstraintsIfNeeded];
  63. }
  64. #pragma mark - Update auto layout constraints
  65. - (void)updateConstraints
  66. {
  67. if (!self.didSetupConstraints)
  68. {
  69. for (NSUInteger index = 0; index < self.thumbnailViews.count; index++)
  70. {
  71. CTAssetThumbnailView *thumbnailView = [self thumbnailAtIndex:index];
  72. CGFloat delta = self.edgeInsets.top / 2;
  73. CGSize size = self.thumbnailSize;
  74. size.width -= index * delta * 2;
  75. size.height -= index * delta * 2;
  76. CGFloat inset = (index * delta * 3);
  77. [NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
  78. [thumbnailView autoSetDimensionsToSize:size];
  79. }];
  80. [NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
  81. [thumbnailView autoAlignAxisToSuperviewAxis:ALAxisVertical];
  82. [thumbnailView autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:inset];
  83. }];
  84. }
  85. self.didSetupConstraints = YES;
  86. }
  87. [super updateConstraints];
  88. }
  89. - (CTAssetThumbnailView *)thumbnailAtIndex:(NSUInteger)index
  90. {
  91. return self.thumbnailViews[index];
  92. }
  93. - (void)setHighlighted:(BOOL)highlighted
  94. {
  95. for (CTAssetThumbnailView *thumbnailView in self.thumbnailViews)
  96. thumbnailView.backgroundColor = CTAssetsPikcerThumbnailBackgroundColor;
  97. }
  98. @end