MHStickerCell.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // MHStickerCell.m
  3. #import "MHStickerCell.h"
  4. //#import "StickerDataListModel.h"
  5. #import "UIImageView+WebCache.h"
  6. #import "MHBeautyParams.h"
  7. #import <MHBeautySDK/MHBeautySDK.h>
  8. @interface MHStickerIndicatorView ()
  9. {
  10. CALayer *_animationLayer;
  11. }
  12. @end
  13. @implementation MHStickerIndicatorView
  14. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  15. self = [super initWithCoder:aDecoder];
  16. if (self) {
  17. _tintColor = [UIColor whiteColor];
  18. //_size = 25.0f;
  19. [self commonInit];
  20. }
  21. return self;
  22. }
  23. - (id)initWithTintColor:(UIColor *)tintColor size:(CGFloat)size {
  24. self = [super init];
  25. if (self) {
  26. _size = size;
  27. _tintColor = tintColor;
  28. [self commonInit];
  29. }
  30. return self;
  31. }
  32. #pragma mark -
  33. #pragma mark Methods
  34. - (void)commonInit {
  35. self.userInteractionEnabled = NO;
  36. self.hidden = YES;
  37. _animationLayer = [[CALayer alloc] init];
  38. [self.layer addSublayer:_animationLayer];
  39. [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
  40. [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
  41. }
  42. - (void)setupAnimation {
  43. _animationLayer.sublayers = nil;
  44. [self setupAnimationInLayer:_animationLayer withSize:CGSizeMake(_size, _size) tintColor:_tintColor];
  45. _animationLayer.speed = 0.0f;
  46. }
  47. - (void)setupAnimationInLayer:(CALayer *)layer withSize:(CGSize)size tintColor:(UIColor *)tintColor {
  48. CGFloat duration = 0.6f;
  49. // Rotate animation
  50. CAKeyframeAnimation *rotateAnimation = [self createKeyframeAnimationWithKeyPath:@"transform.rotation.z"];
  51. rotateAnimation.values = @[@0, @M_PI, @(2 * M_PI)];
  52. rotateAnimation.duration = duration;
  53. rotateAnimation.repeatCount = HUGE_VALF;
  54. // Draw ball clip
  55. CAShapeLayer *circle = [CAShapeLayer layer];
  56. UIBezierPath *circlePath =
  57. [UIBezierPath bezierPathWithArcCenter:CGPointMake(size.width / 2, size.height / 2) radius:size.width / 2 startAngle:1.5 * M_PI endAngle:M_PI clockwise:true];
  58. circle.path = circlePath.CGPath;
  59. circle.lineWidth = 2;
  60. circle.fillColor = nil;
  61. circle.strokeColor = tintColor.CGColor;
  62. circle.frame =
  63. CGRectMake(0, 0, size.width, size.height);
  64. [circle addAnimation:rotateAnimation forKey:@"animation"];
  65. [layer addSublayer:circle];
  66. }
  67. - (CAKeyframeAnimation *)createKeyframeAnimationWithKeyPath:(NSString *)keyPath {
  68. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:keyPath];
  69. animation.removedOnCompletion = NO;
  70. return animation;
  71. }
  72. - (void)startAnimating {
  73. if (!_animationLayer.sublayers) {
  74. [self setupAnimation];
  75. }
  76. self.hidden = NO;
  77. _animationLayer.speed = 1.0f;
  78. _animating = YES;
  79. }
  80. - (void)stopAnimating {
  81. _animationLayer.speed = 0.0f;
  82. _animating = NO;
  83. self.hidden = YES;
  84. }
  85. #pragma mark Setters
  86. - (void)setSize:(CGFloat)size {
  87. if (_size != size) {
  88. _size = size;
  89. [self setupAnimation];
  90. [self invalidateIntrinsicContentSize];
  91. }
  92. }
  93. - (void)setTintColor:(UIColor *)tintColor {
  94. if (![_tintColor isEqual:tintColor]) {
  95. _tintColor = tintColor;
  96. CGColorRef tintColorRef = tintColor.CGColor;
  97. for (CALayer *sublayer in _animationLayer.sublayers) {
  98. sublayer.backgroundColor = tintColorRef;
  99. if ([sublayer isKindOfClass:[CAShapeLayer class]]) {
  100. CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
  101. shapeLayer.strokeColor = tintColorRef;
  102. shapeLayer.fillColor = tintColorRef;
  103. }
  104. }
  105. }
  106. }
  107. #pragma mark Layout
  108. - (void)layoutSubviews {
  109. [super layoutSubviews];
  110. _animationLayer.frame = self.bounds;
  111. BOOL animating = _animating;
  112. if (animating)
  113. [self stopAnimating];
  114. [self setupAnimation];
  115. if (animating)
  116. [self startAnimating];
  117. }
  118. - (CGSize)intrinsicContentSize {
  119. return CGSizeMake(_size, _size);
  120. }
  121. @end
  122. @interface MHStickerCell ()
  123. @property (nonatomic, strong) UIImageView *stickerView;
  124. @property(nonatomic, strong) MHStickerIndicatorView *indicatorView;
  125. @end
  126. @implementation MHStickerCell
  127. - (instancetype)initWithFrame:(CGRect)frame {
  128. if (self = [super initWithFrame:frame]) {
  129. self.backgroundColor = [UIColor clearColor];
  130. [self addSubview:self.selectedImgView];
  131. [self addSubview:self.stickerView];
  132. [self addSubview:self.downloadImg];
  133. [self addSubview:self.indicatorView];
  134. }
  135. return self;
  136. }
  137. - (void)setSticker:(StickerDataListModel *)sticker index:(NSInteger)index {
  138. _listModel = sticker;
  139. if (index == 0) {
  140. if (self.indicatorView.animating == YES) {
  141. [self.indicatorView stopAnimating];
  142. }
  143. }
  144. self.stickerView.image = [UIImage imageNamed:sticker.thumb];
  145. self.downloadImg.hidden = YES;
  146. }
  147. - (void)setListModel:(StickerDataListModel *)listModel {
  148. if (!listModel) {
  149. return;
  150. }
  151. _listModel = listModel;
  152. NSString * thumb = listModel.thumb;
  153. if (!IsString(thumb)) {
  154. return;
  155. }
  156. // if (listModel.isCancelImage) {
  157. // [self.stickerView setImage:listModel.cancelImage];
  158. // }else{
  159. [self.stickerView sd_setImageWithURL:[NSURL URLWithString:thumb]];
  160. // }
  161. if (isSaveSticker) {
  162. NSDictionary *info = [[NSUserDefaults standardUserDefaults] objectForKey:kMHSticker];
  163. if (IsDictionaryWithAnyKeyValue(info)) {
  164. NSString *key = [NSString stringWithFormat:@"%@:%@",listModel.name,listModel.uptime];
  165. NSString *savedKey = [info objectForKey:@"content"];
  166. if ([savedKey isEqualToString:key]) {
  167. listModel.isSelected = YES;
  168. listModel.is_downloaded = @"1";
  169. }else{
  170. listModel.isSelected = NO;
  171. }
  172. }else{
  173. listModel.isSelected = NO;
  174. // listModel.is_downloaded = @"0";
  175. }
  176. }
  177. self.selectedImgView.hidden = !listModel.isSelected;
  178. if (listModel.is_downloaded.boolValue == YES) {
  179. if (self.indicatorView.animating == YES) {
  180. [self.indicatorView stopAnimating];
  181. }
  182. self.downloadImg.hidden = YES;
  183. listModel.downloadState = MHStickerDownloadStateDownoadDone;
  184. } else {
  185. if (listModel.downloadState == MHStickerDownloadStateDownoading) {
  186. self.downloadImg.hidden = YES;
  187. if (self.indicatorView.animating != YES) {
  188. [self.indicatorView startAnimating];
  189. }
  190. } else {
  191. if (self.indicatorView.animating == YES) {
  192. [self.indicatorView stopAnimating];
  193. }
  194. listModel.downloadState = MHStickerDownloadStateDownoadNot;
  195. self.downloadImg.hidden = NO;
  196. }
  197. }
  198. }
  199. /**
  200. 动画
  201. */
  202. - (void)startDownload {
  203. self.listModel.downloadState = MHStickerDownloadStateDownoading;
  204. self.downloadImg.hidden = YES;
  205. [self.indicatorView startAnimating];
  206. }
  207. - (void)stopDownLoad {
  208. self.listModel.downloadState = MHStickerDownloadStateDownoadDone;
  209. self.downloadImg.hidden = YES;
  210. [self.indicatorView stopAnimating];
  211. }
  212. #pragma mark - lazy
  213. - (UIImageView *)selectedImgView {
  214. if (!_selectedImgView) {
  215. UIImage *img = BundleImg(@"ic_border_selected");
  216. if ([[[NSUserDefaults standardUserDefaults] valueForKey:kLanguage] isEqualToString:kLanguage_EN]) {
  217. img = FoxBundleImg(@"selectedBorder");
  218. }
  219. // if (!isChinese) {
  220. // img = FoxBundleImg(@"selectedBorder");
  221. // }
  222. _selectedImgView = [[UIImageView alloc] initWithImage:img];
  223. _selectedImgView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  224. _selectedImgView.hidden = YES;
  225. }
  226. return _selectedImgView;
  227. }
  228. - (UIImageView *)stickerView {
  229. if (!_stickerView) {
  230. _stickerView = [[UIImageView alloc] initWithFrame:CGRectMake((self.frame.size.width - 45)/2, (self.frame.size.height - 45)/2, 45, 45)];
  231. }
  232. return _stickerView;
  233. }
  234. - (UIImageView *)downloadImg {
  235. if (!_downloadImg) {
  236. UIImage *img = BundleImg(@"stickerDownload");
  237. _downloadImg = [[UIImageView alloc] initWithImage:img];
  238. _downloadImg.frame = CGRectMake(_stickerView.frame.size.width - 13, _stickerView.frame.size.height - 13, 13, 13);
  239. }
  240. return _downloadImg;
  241. }
  242. - (MHStickerIndicatorView *)indicatorView {
  243. if (!_indicatorView) {
  244. _indicatorView = [[MHStickerIndicatorView alloc] initWithTintColor:[UIColor whiteColor] size:25];
  245. _indicatorView.frame = CGRectMake((self.frame.size.width - 25)/2, (self.frame.size.height - 25)/2, 25, 25);
  246. }
  247. return _indicatorView;
  248. }
  249. @end