RITLPhotosCell.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // YPPhotosCell.m
  3. // YPPhotoDemo
  4. //
  5. // Created by YueWen on 16/7/14.
  6. // Copyright © 2017年 YueWen. All rights reserved.
  7. //
  8. #import "RITLPhotosCell.h"
  9. #import "NSBundle+RITLPhotos.h"
  10. #import <PhotosUI/PhotosUI.h>
  11. #import "Masonry.h"
  12. #import "RITLKit.h"
  13. //static NSString *const RITLPhotosCollectionCellDeselectImageName = @"RITLPhotos.bundle/ritl_deselect";
  14. @interface RITLPhotosCell ()
  15. /// 不能点击进行的遮罩层
  16. @property (nonatomic, strong, readwrite)UIView *shadeView;
  17. @end
  18. @implementation RITLPhotosCell
  19. -(void)prepareForReuse
  20. {
  21. //重置所有数据
  22. self.imageView.image = nil;
  23. self.chooseButton.hidden = false;
  24. self.messageView.hidden = true;
  25. self.messageImageView.image = nil;
  26. self.messageLabel.text = @"";
  27. self.indexLabel.text = @"";
  28. }
  29. -(instancetype)initWithFrame:(CGRect)frame
  30. {
  31. if (self = [super initWithFrame:frame])
  32. {
  33. [self photosCellWillLoad];
  34. }
  35. return self;
  36. }
  37. -(void)awakeFromNib
  38. {
  39. [super awakeFromNib];
  40. [self photosCellWillLoad];
  41. }
  42. - (void)photosCellWillLoad
  43. {
  44. self.backgroundColor = [UIColor whiteColor];
  45. //add subviews
  46. [self addSubImageView];
  47. [self addSubMessageView];
  48. [self addSubMessageImageView];
  49. [self addSubMessageLabel];
  50. [self addChooseControl];
  51. self.indexLabel = ({
  52. UILabel *label = [UILabel new];
  53. label.backgroundColor = RITLColorFromIntRBG(9, 187, 7);
  54. label.text = @"0";
  55. label.font = RITLUtilityFont(RITLFontPingFangSC_Regular, 13);
  56. label.textColor = UIColor.whiteColor;
  57. label.textAlignment = NSTextAlignmentCenter;
  58. label.layer.cornerRadius = 21 / 2.0;
  59. label.layer.masksToBounds = true;
  60. label.hidden = true;
  61. label;
  62. });
  63. [self.contentView addSubview:self.indexLabel];
  64. [self.indexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.width.height.mas_equalTo(21);
  66. make.right.inset(5);
  67. make.top.offset(4);
  68. }];
  69. self.liveBadgeImageView = ({
  70. UIImageView *imageView = [UIImageView new];
  71. imageView.backgroundColor = UIColor.clearColor;
  72. imageView.contentMode = UIViewContentModeScaleAspectFill;
  73. imageView.hidden = true;
  74. if (@available(iOS 9.1,*)) {
  75. imageView.image = [PHLivePhotoView livePhotoBadgeImageWithOptions:PHLivePhotoBadgeOptionsOverContent];
  76. }
  77. imageView;
  78. });
  79. if (RITL_iOS_Version_GreaterThanOrEqualTo(9.1)) {
  80. [self.contentView addSubview:self.liveBadgeImageView];
  81. [self.liveBadgeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  82. make.bottom.inset(3);
  83. make.left.offset(3);
  84. make.width.and.height.mas_equalTo(28);
  85. }];
  86. }
  87. self.shadeView = ({
  88. UIView *view = [UIView new];
  89. view.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0.6];
  90. view.hidden = true;
  91. view;
  92. });
  93. [self.contentView addSubview:self.shadeView];
  94. [self.shadeView mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.edges.offset(0);
  96. }];
  97. }
  98. #pragma mark - CreateSubviews
  99. - (void)addSubImageView
  100. {
  101. //添加imageView
  102. _imageView = [[UIImageView alloc]init];
  103. _imageView.clipsToBounds = true;
  104. _imageView.contentMode = UIViewContentModeScaleAspectFill;
  105. _imageView.backgroundColor = [UIColor whiteColor];
  106. [self.contentView addSubview:_imageView];
  107. [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.edges.equalTo(self.contentView);
  109. }];
  110. }
  111. - (void)addSubMessageView
  112. {
  113. _messageView = [[UIView alloc]init];
  114. [self.contentView addSubview:_messageView];
  115. [_messageView mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.left.right.and.bottom.equalTo(self.contentView);
  117. make.height.equalTo(@(20));
  118. }];
  119. _messageView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.03];
  120. _messageView.hidden = true;
  121. }
  122. - (void)addSubMessageImageView
  123. {
  124. _messageImageView = [[UIImageView alloc]init];
  125. [_messageView addSubview:_messageImageView];
  126. [_messageImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  127. make.left.equalTo(@(5));
  128. make.bottom.equalTo(self.messageView);
  129. make.size.mas_equalTo(CGSizeMake(30, 20));
  130. }];
  131. }
  132. - (void)addSubMessageLabel
  133. {
  134. _messageLabel = [[UILabel alloc]init];
  135. [_messageView addSubview:_messageLabel];
  136. [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.left.equalTo(self.messageImageView.mas_right);
  138. make.right.equalTo(self.messageView).offset(-3);
  139. make.bottom.equalTo(self.messageView);
  140. make.height.mas_equalTo(20);
  141. }];
  142. _messageLabel.font = [UIFont systemFontOfSize:11];
  143. _messageLabel.textAlignment = NSTextAlignmentRight;
  144. _messageLabel.textColor = [UIColor whiteColor];
  145. _messageLabel.text = @"00:25";
  146. }
  147. - (void)addChooseControl
  148. {
  149. _chooseButton = [UIButton new];
  150. [self.contentView addSubview:_chooseButton];
  151. [_chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
  152. make.size.mas_equalTo(CGSizeMake(40, 40));
  153. make.top.offset(0);
  154. make.right.offset(0);
  155. }];
  156. [_chooseButton addTarget:self
  157. action:@selector(chooseButtonDidTap:)
  158. forControlEvents:UIControlEventTouchUpInside];
  159. _chooseButton.imageEdgeInsets = UIEdgeInsetsMake(4, 14, 15, 5);
  160. /// normal
  161. [_chooseButton setImage:/*RITLPhotosCollectionCellDeselectImageName.ritl_image*/NSBundle.ritl_deselect forState:UIControlStateNormal];
  162. /// selected
  163. [_chooseButton setTitle:@"1" forState:UIControlStateSelected];
  164. [_chooseButton setTitleColor:UIColor.whiteColor forState:UIControlStateSelected];
  165. _chooseButton.imageView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.15];
  166. _chooseButton.imageView.layer.cornerRadius = 21 / 2.0;
  167. _chooseButton.imageView.layer.masksToBounds = true;
  168. }
  169. /** 选择按钮被点击 */
  170. - (IBAction)chooseButtonDidTap:(id)sender
  171. {
  172. if (self.actionTarget && [self.actionTarget respondsToSelector:@selector(photosCellDidTouchUpInSlide:asset:indexPath:complete:)]) {
  173. [self.actionTarget photosCellDidTouchUpInSlide:self asset:self.asset indexPath:self.indexPath complete:^(RITLPhotosCellAnimatedStatus status, BOOL selected,NSUInteger count) {
  174. if (status == RITLPhotosCellAnimatedStatusPermit) {//允许使用动画
  175. [self selectedStatusDidChanged:selected count:count];
  176. }
  177. }];
  178. }
  179. }
  180. - (void)selectedStatusDidChanged:(BOOL)selected count:(NSUInteger)count
  181. {
  182. self.indexLabel.hidden = !selected;
  183. if (!selected) {
  184. self.indexLabel.text = @"";
  185. return;
  186. }
  187. self.indexLabel.text = @(count).stringValue;
  188. [UIView animateWithDuration:0.15 animations:^{//anmiation
  189. self.indexLabel.transform = CGAffineTransformMakeScale(1.3f, 1.3f);//放大
  190. } completion:^(BOOL finished) {//变回
  191. [UIView animateWithDuration:0.1 animations:^{
  192. self.indexLabel.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
  193. }];
  194. }];
  195. }
  196. @end