RKPaintedGiftView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. //
  2. // RKPaintedGiftView.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/8/27.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "RKPaintedGiftView.h"
  9. #import "RKPaintedHeader.h"
  10. @interface RKPaintedGiftView()
  11. {
  12. CGPoint _lastPoint;
  13. }
  14. @property(nonatomic,strong)UIView *funBgView;
  15. @property(nonatomic,strong)UIButton *closeBtn;
  16. @property(nonatomic,strong)UIButton *clearLastBtn;
  17. @property(nonatomic,strong)UIButton *clearAllBtn;
  18. @property(nonatomic,strong)UILabel *paintedNumL; //涂鸦个数
  19. @property(nonatomic,strong)UIView *paintedDesView; //涂鸦只是说明
  20. @property(nonatomic,strong)NSMutableArray<NSDictionary *> *pointsArray;
  21. @property(nonatomic,strong)NSMutableArray<UIImageView *> *paintedGiftsArray;
  22. @property(nonatomic,assign)CGFloat giftWidth;
  23. @property(nonatomic,assign)CGFloat giftHeight;
  24. @end
  25. @implementation RKPaintedGiftView
  26. - (instancetype)init {
  27. self = [super init];
  28. if (self) {
  29. [self createUI];
  30. }
  31. return self;
  32. }
  33. -(void)createUI {
  34. self.backgroundColor = RGB_COLOR(@"#000000", 0.2);
  35. self.pointsArray = [NSMutableArray array];
  36. self.paintedGiftsArray = [NSMutableArray array];
  37. _giftWidth = _giftHeight = _window_width/10;
  38. [self addSubview:self.paintedDesView];
  39. [_paintedDesView mas_makeConstraints:^(MASConstraintMaker *make) {
  40. make.width.equalTo(self.mas_width).multipliedBy(0.8);
  41. make.height.equalTo(self.mas_width);
  42. make.centerX.equalTo(self);
  43. make.centerY.equalTo(self.mas_centerY).multipliedBy(0.9);
  44. }];
  45. [self addSubview:self.funBgView];
  46. [_funBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.right.equalTo(self.mas_right).offset(0);
  48. make.height.mas_equalTo(43);
  49. make.bottom.equalTo(self.mas_bottom).offset(-1);
  50. make.left.equalTo(self.mas_left).offset(0);
  51. }];
  52. UIView *funShadow = [[UIView alloc]init];
  53. funShadow.backgroundColor = self.backgroundColor;
  54. [_funBgView addSubview:funShadow];
  55. [funShadow mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.width.height.centerX.centerY.equalTo(_funBgView);
  57. }];
  58. [_funBgView addSubview:self.paintedNumL];
  59. [_funBgView addSubview:self.closeBtn];
  60. _funBgView.backgroundColor = RGB_COLOR(@"#ffffff", 1);
  61. [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  62. make.right.equalTo(_funBgView.mas_right).offset(-10);
  63. make.centerY.equalTo(_funBgView);
  64. make.width.height.mas_equalTo(40);
  65. }];
  66. [_funBgView addSubview:self.clearAllBtn];
  67. [_clearAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.centerY.width.height.equalTo(_closeBtn);
  69. make.right.equalTo(_closeBtn.mas_left).offset(-8);
  70. }];
  71. [_funBgView addSubview:self.clearLastBtn];
  72. [_clearLastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.centerY.width.height.equalTo(_closeBtn);
  74. make.right.equalTo(_clearAllBtn.mas_left).offset(-8);
  75. }];
  76. //默认隐藏
  77. self.hidden = YES;
  78. [self judgeShowDesView];
  79. }
  80. - (void)layoutSubviews {
  81. [super layoutSubviews];
  82. [_funBgView layoutIfNeeded];
  83. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_funBgView.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];
  84. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  85. maskLayer.frame = _funBgView.bounds;
  86. maskLayer.path = maskPath.CGPath;
  87. _funBgView.layer.mask = maskLayer;
  88. }
  89. - (void)setShowPainted:(BOOL)showPainted {
  90. _showPainted = showPainted;
  91. [_pointsArray removeAllObjects];
  92. self.hidden = !_showPainted;
  93. for (UIImageView* subImgView in _paintedGiftsArray) {
  94. if ([subImgView isKindOfClass:[UIImageView class]]) {
  95. [subImgView removeFromSuperview];
  96. }
  97. }
  98. //展开时重新布局个数提示
  99. if (_showPainted) {
  100. [self judgeShowDesView];
  101. [_paintedNumL mas_remakeConstraints:^(MASConstraintMaker *make) {
  102. if (_painteLeftMas) {
  103. make.left.equalTo(_painteLeftMas).offset(8);
  104. }else {
  105. make.left.equalTo(_funBgView).offset(10);
  106. }
  107. make.centerY.equalTo(_funBgView);
  108. }];
  109. }
  110. }
  111. - (CGSize)getPaintedRegion {
  112. return CGSizeMake(self.width, self.height);
  113. }
  114. -(NSArray<NSString *> *)getPaintedPointArray; {
  115. return [NSArray arrayWithArray:_pointsArray];
  116. }
  117. - (BOOL)gestureRecognizer:(UIGestureRecognizer* )gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer* )otherGestureRecognizer {
  118. return YES;
  119. }
  120. - (void)touchesBegan:(NSSet<UITouch* >* )touches withEvent:(UIEvent* )event {
  121. UITouch* touch = [touches anyObject];
  122. CGPoint point = [touch locationInView:self];
  123. //NSLog(@"touchesBegan:x:=%f y:=%f",point.x,point.y);
  124. if ([PublicObj checkNull:_giftPathStr] || CGRectContainsPoint(_funBgView.frame,point)) {
  125. return;
  126. }
  127. if ( point.y > _giftHeight/2 && point.y < self.height-_giftHeight/2 ) {
  128. if (_pointsArray.count < PaintedMaxNum) {
  129. UIImageView* imageView = [[UIImageView alloc] init];
  130. [imageView sd_setImageWithURL:[NSURL URLWithString:_giftPathStr]];
  131. [self addSubview:imageView];
  132. [_paintedGiftsArray addObject:imageView];
  133. [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
  134. make.top.equalTo(self).offset(point.y - _giftHeight/2);
  135. make.left.equalTo(self).offset(point.x - _giftWidth/2);
  136. make.width.mas_equalTo(_giftWidth);
  137. make.height.mas_equalTo(_giftHeight);
  138. }];
  139. NSString* x = [NSString stringWithFormat:@"%d",(int)point.x];
  140. NSString* y = [NSString stringWithFormat:@"%d",(int)point.y];
  141. NSDictionary* pointDic = [NSDictionary dictionaryWithObjectsAndKeys:x,@"x",y,@"y", nil];
  142. //NSString* dicStr = [self dictionaryToJson:pointDic];
  143. [_pointsArray addObject:pointDic];
  144. _lastPoint = point;
  145. }
  146. }
  147. [self judgeShowDesView];
  148. }
  149. - (void)touchesMoved:(NSSet<UITouch* >* )touches withEvent:(UIEvent* )event {
  150. UITouch* touch = [touches anyObject];
  151. CGPoint point = [touch locationInView:self];
  152. //NSLog(@"touchesMoved:x:=%f y:=%f",point.x,point.y);
  153. if ([PublicObj checkNull:_giftPathStr] || CGRectContainsPoint(_funBgView.frame,point)) {
  154. return;
  155. }
  156. if (point.y > _giftHeight/2 && point.y < self.height-_giftHeight/2 ) {
  157. if ( fabs(point.x - _lastPoint.x) > _giftHeight/2 || fabs(point.y - _lastPoint.y) > _giftHeight/2) {
  158. if (_pointsArray.count < PaintedMaxNum) {
  159. UIImageView* imageView = [[UIImageView alloc] init];
  160. [imageView sd_setImageWithURL:[NSURL URLWithString:_giftPathStr]];
  161. [self addSubview:imageView];
  162. [_paintedGiftsArray addObject:imageView];
  163. [imageView mas_makeConstraints:^(MASConstraintMaker* make) {
  164. make.top.equalTo(self).offset(point.y - _giftHeight/2);
  165. make.left.equalTo(self).offset(point.x - _giftWidth/2);
  166. make.width.mas_equalTo(_giftWidth);
  167. make.height.mas_equalTo(_giftHeight);
  168. }];
  169. NSString* x = [NSString stringWithFormat:@"%d",(int)point.x];
  170. NSString* y = [NSString stringWithFormat:@"%d",(int)point.y];
  171. NSDictionary* pointDic = [NSDictionary dictionaryWithObjectsAndKeys:x,@"x",y,@"y", nil];
  172. //NSString* dicStr = [self dictionaryToJson:pointDic];
  173. [_pointsArray addObject:pointDic];
  174. _lastPoint = point;
  175. }
  176. }
  177. }
  178. [self judgeShowDesView];
  179. }
  180. -(void)judgeShowDesView {
  181. if (_pointsArray.count>0) {
  182. _paintedDesView.hidden = YES;
  183. }else {
  184. _paintedDesView.hidden = NO;
  185. }
  186. NSString *preStr = YZMsg(@"涂鸦");
  187. NSString *numStr = [NSString stringWithFormat:@" %lu ",(unsigned long)_pointsArray.count];
  188. NSMutableAttributedString *p_muta = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@%@",preStr,numStr,YZMsg(@"个")]];
  189. [p_muta addAttributes:@{NSForegroundColorAttributeName:Pink_Cor} range:NSMakeRange(preStr.length, numStr.length)];
  190. _paintedNumL.attributedText = p_muta;
  191. }
  192. - (void)touchesEnded:(NSSet<UITouch* >* )touches withEvent:(UIEvent* )event {
  193. UITouch* touch = [touches anyObject];
  194. CGPoint point = [touch locationInView:self];
  195. NSLog(@"touchesEnded:x:=%f y:=%f",point.x,point.y);
  196. }
  197. - (UIView *)funBgView {
  198. if (!_funBgView) {
  199. _funBgView = [[UIView alloc]init];
  200. _funBgView.backgroundColor = UIColor.clearColor;
  201. }
  202. return _funBgView;
  203. }
  204. - (UILabel *)paintedNumL {
  205. if (!_paintedNumL) {
  206. _paintedNumL = [[UILabel alloc]init];
  207. _paintedNumL.font = SYS_Font(12);
  208. _paintedNumL.textColor = RGB_COLOR(@"#323232", 1);
  209. }
  210. return _paintedNumL;
  211. }
  212. - (UIButton *)closeBtn {
  213. if (!_closeBtn) {
  214. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  215. _closeBtn.titleLabel.font = SYS_Font(13);
  216. [_closeBtn addTarget:self action:@selector(clikcCloseBtn) forControlEvents:UIControlEventTouchUpInside];
  217. //[_closeBtn setTitle:YZMsg(@"关闭") forState:0];
  218. //[_closeBtn setTitleColor:Pink_Cor forState:0];
  219. //_closeBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
  220. [_closeBtn setImage:[UIImage imageNamed:@"手绘-关闭"] forState:0];
  221. _closeBtn.backgroundColor = UIColor.clearColor;
  222. }
  223. return _closeBtn;
  224. }
  225. -(void)clikcCloseBtn {
  226. self.showPainted = NO;
  227. if (self.paintedEvent) {
  228. self.paintedEvent();
  229. }
  230. }
  231. - (UIButton *)clearAllBtn {
  232. if (!_clearAllBtn) {
  233. _clearAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  234. _clearAllBtn.titleLabel.font = SYS_Font(13);
  235. [_clearAllBtn addTarget:self action:@selector(clikcClearAllBtn) forControlEvents:UIControlEventTouchUpInside];
  236. //[_clearAllBtn setTitle:YZMsg(@"清除全部") forState:0];
  237. //[_clearAllBtn setTitleColor:Pink_Cor forState:0];
  238. //_clearAllBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
  239. [_clearAllBtn setImage:[UIImage imageNamed:@"手绘-删除"] forState:0];
  240. _clearAllBtn.backgroundColor = UIColor.clearColor;
  241. }
  242. return _clearAllBtn;
  243. }
  244. -(void)clikcClearAllBtn {
  245. [_pointsArray removeAllObjects];
  246. for (UIImageView* subImgView in _paintedGiftsArray) {
  247. if ([subImgView isKindOfClass:[UIImageView class]]) {
  248. [subImgView removeFromSuperview];
  249. }
  250. }
  251. [self judgeShowDesView];
  252. }
  253. - (UIButton *)clearLastBtn {
  254. if (!_clearLastBtn) {
  255. _clearLastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  256. _clearLastBtn.titleLabel.font = SYS_Font(13);
  257. [_clearLastBtn addTarget:self action:@selector(clikcClearLastBtn) forControlEvents:UIControlEventTouchUpInside];
  258. //[_clearLastBtn setTitle:YZMsg(@"回退") forState:0];
  259. //[_clearLastBtn setTitleColor:Pink_Cor forState:0];
  260. //_clearLastBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 8, 5, 8);
  261. [_clearLastBtn setImage:[UIImage imageNamed:@"手绘-回退"] forState:0];
  262. _clearLastBtn.backgroundColor = UIColor.clearColor;
  263. }
  264. return _clearLastBtn;
  265. }
  266. -(void)clikcClearLastBtn {
  267. if (_pointsArray.count>0) {
  268. [_pointsArray removeLastObject];
  269. UIImageView *imgIV = [_paintedGiftsArray lastObject];
  270. [imgIV removeFromSuperview];
  271. [_paintedGiftsArray removeLastObject];
  272. }
  273. [self judgeShowDesView];
  274. }
  275. - (UIView *)paintedDesView {
  276. if (!_paintedDesView) {
  277. _paintedDesView = [[UIView alloc]init];
  278. _paintedDesView.backgroundColor = UIColor.clearColor;
  279. UIImageView *desIV = [[UIImageView alloc]init];
  280. desIV.userInteractionEnabled = YES;
  281. desIV.image = [UIImage imageNamed:@"手绘-指示"];
  282. [_paintedDesView addSubview:desIV];
  283. [desIV mas_makeConstraints:^(MASConstraintMaker *make) {
  284. make.width.equalTo(_paintedDesView.mas_width).multipliedBy(0.3);
  285. make.height.equalTo(desIV.mas_width);
  286. make.centerY.equalTo(_paintedDesView);
  287. make.centerX.equalTo(_paintedDesView);
  288. }];
  289. UILabel *desL = [[UILabel alloc]init];
  290. desL.font = SYS_Font(12);
  291. desL.text = YZMsg(@"绘制礼物,进入涂鸦模式");
  292. desL.textColor = [UIColor whiteColor];
  293. [_paintedDesView addSubview:desL];
  294. [desL mas_makeConstraints:^(MASConstraintMaker *make) {
  295. make.centerX.equalTo(desIV);
  296. make.top.equalTo(desIV.mas_bottom).offset(3);
  297. }];
  298. }
  299. return _paintedDesView;
  300. }
  301. //- (NSString* )dictionaryToJson:(NSDictionary* )dic {
  302. // NSError* parseError = nil;
  303. // NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
  304. // return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  305. //}
  306. @end