RKShowPaintedView.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // RKShowPaintedView.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/8/27.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "RKShowPaintedView.h"
  9. #import "RKPaintedHeader.h"
  10. @interface RKShowPaintedView() {
  11. int imgCount;
  12. }
  13. @property(nonatomic,strong)NSMutableArray<UIImageView *> *paintedGiftsArray;
  14. @property(nonatomic,assign)CGFloat giftWidth;
  15. @property(nonatomic,assign)CGFloat giftHeight;
  16. @end
  17. @implementation RKShowPaintedView
  18. - (instancetype)init {
  19. self = [super init];
  20. if (self) {
  21. self.hidden = YES;
  22. imgCount = 0;
  23. _giftWidth = _giftHeight = _window_width/10;
  24. self.paintedGiftsArray = [NSMutableArray array];
  25. }
  26. return self;
  27. }
  28. - (void)setPaintedWidth:(CGFloat)paintedWidth{
  29. _paintedWidth = paintedWidth;
  30. [self resetSelfUI];
  31. }
  32. - (void)setPaintedHeight:(CGFloat)paintedHeight {
  33. _paintedHeight = paintedHeight;
  34. [self resetSelfUI];
  35. }
  36. -(void)resetSelfUI {
  37. if (_paintedWidth<=0) {
  38. _paintedWidth = 1;
  39. }
  40. if (_paintedHeight<=0) {
  41. _paintedHeight = 1;
  42. }
  43. CGFloat hhhhh = self.superview.width*_paintedHeight/_paintedWidth;
  44. [self mas_remakeConstraints:^(MASConstraintMaker *make) {
  45. make.centerX.equalTo(self.superview);
  46. make.width.equalTo(self.superview);
  47. make.height.mas_equalTo(hhhhh);
  48. make.top.equalTo(self.superview.mas_top).offset(statusbarHeight);
  49. }];
  50. }
  51. - (void)setPaintedPointArray:(NSArray *)paintedPointArray {
  52. _paintedPointArray = paintedPointArray;
  53. [_paintedGiftsArray removeAllObjects];
  54. [self layoutIfNeeded];
  55. self.hidden = NO;
  56. [self removeAllSubViews];
  57. if ([PublicObj checkNull:_giftPathStr] || _paintedPointArray.count<=0) {
  58. return;
  59. }
  60. for (NSDictionary* dic in _paintedPointArray) {
  61. //NSDictionary* dic = [self dictionaryWithJsonString:dicStr];
  62. //坐标转换
  63. CGFloat originX = [[dic objectForKey:@"x"] floatValue];
  64. CGFloat originY = [[dic objectForKey:@"y"] floatValue];
  65. if (_paintedWidth<=0) {
  66. _paintedWidth = 1;
  67. }
  68. if (_paintedHeight<=0) {
  69. _paintedHeight = 1;
  70. }
  71. CGFloat nowX = originX * self.width/_paintedWidth;
  72. CGFloat nowY = originY * self.height/_paintedHeight;
  73. CGPoint point = CGPointMake(nowX, nowY);
  74. UIImageView* imageView = [[UIImageView alloc] init];
  75. [imageView sd_setImageWithURL:[NSURL URLWithString:_giftPathStr]];
  76. [self addSubview:imageView];
  77. [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
  78. make.top.equalTo(self).offset(point.y - _giftWidth/2);
  79. make.left.equalTo(self).offset(point.x - _giftHeight/2);
  80. make.width.mas_equalTo(_giftWidth);
  81. make.height.mas_equalTo(_giftHeight);
  82. }];
  83. imageView.hidden = YES;
  84. [_paintedGiftsArray addObject:imageView];
  85. }
  86. [self showAnimation];
  87. //NSLog(@"00000000-----");
  88. }
  89. -(void)showAnimation {
  90. //NSLog(@"000000000");
  91. if (imgCount >= _paintedGiftsArray.count) {
  92. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  93. imgCount = 0;
  94. [_paintedGiftsArray removeAllObjects];
  95. for (UIImageView *imgIV in _paintedGiftsArray) {
  96. [imgIV.layer removeAnimationForKey:@"animateLayer"];
  97. }
  98. //整体移除动画
  99. [UIView animateWithDuration:0.36 animations:^{
  100. self.layer.transform = CATransform3DMakeScale(2, 2, 1);
  101. } completion:^(BOOL finished) {
  102. self.layer.transform = CATransform3DIdentity;
  103. self.hidden = YES;
  104. [self removeAllSubViews];
  105. }];
  106. });
  107. }else {
  108. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(PaintedPerTime*0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  109. [self startAnimation:_paintedGiftsArray[imgCount]];
  110. imgCount ++;
  111. [self showAnimation];
  112. });
  113. }
  114. }
  115. -(void)startAnimation:(UIImageView *)imgIV {
  116. //NSLog(@"11111111111");
  117. imgIV.hidden = NO;
  118. //每一帧动画
  119. CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  120. animation.duration = PaintedPerTime*0.6;
  121. NSMutableArray *values = [NSMutableArray array];
  122. CGFloat fromValue = 1.5;
  123. CGFloat toValue = 1.0;
  124. int num = 10;
  125. for (int i = 0; i<num; i++) {
  126. //NSLog(@"rk==Cur:%f",fromValue);
  127. [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(fromValue, fromValue, fromValue)]];
  128. fromValue -= (fromValue-toValue)/num;
  129. if (fromValue == toValue) {
  130. break;
  131. }
  132. }
  133. animation.values = values;
  134. [imgIV.layer addAnimation:animation forKey:@"animateLayer"];
  135. }
  136. -(void)destroyPaitend; {
  137. self.hidden = YES;
  138. for (UIImageView *imgIV in _paintedGiftsArray) {
  139. [imgIV.layer removeAnimationForKey:@"animateLayer"];
  140. }
  141. _paintedPointArray = @[];
  142. }
  143. //- (NSDictionary* )dictionaryWithJsonString:(NSString* )jsonString {
  144. // if (jsonString == nil) {
  145. // return nil;
  146. // }
  147. // NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  148. // NSError* err;
  149. // NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
  150. // if (err) {
  151. // NSLog(@"json解析失败:%@",err);
  152. // return nil;
  153. // }
  154. // return dic;
  155. //}
  156. @end