UGCKitVideoPasterView.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitVideoPasterView.h"
  3. #import "UGCKit_UIViewAdditions.h"
  4. #import "UGCKitColorMacro.h"
  5. @interface UGCKitVideoPasterView () <UITextViewDelegate, UITextFieldDelegate>
  6. {
  7. UIView* _borderView; //用来显示边框或样式背景
  8. UIButton* _deleteBtn; //删除铵钮
  9. UIButton* _scaleRotateBtn; //单手操作放大,旋转按钮
  10. CGRect _initFrame;
  11. UGCKitTheme *_theme;
  12. }
  13. @end
  14. @implementation UGCKitVideoPasterView
  15. - (id)initWithFrame:(CGRect)frame theme:(UGCKitTheme *)theme;
  16. {
  17. if (self = [super initWithFrame:frame]) {
  18. _theme = theme;
  19. _initFrame = frame;
  20. _pasterImageView = [[UIImageView alloc] init];
  21. UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)];
  22. singleTap.numberOfTapsRequired = 1;
  23. _pasterImageView.userInteractionEnabled = YES;
  24. [_pasterImageView addGestureRecognizer:singleTap];
  25. _borderView = [UIView new];
  26. _borderView.layer.borderWidth = 1;
  27. _borderView.layer.borderColor = theme.editPasterBorderColor.CGColor;
  28. _borderView.userInteractionEnabled = YES;
  29. _borderView.backgroundColor = [UIColor clearColor];
  30. [self addSubview:_borderView];
  31. _deleteBtn = [UIButton new];
  32. [_deleteBtn setImage:_theme.closeIcon forState:UIControlStateNormal];
  33. [_deleteBtn addTarget:self action:@selector(onDeleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
  34. [self addSubview:_deleteBtn];
  35. _scaleRotateBtn = [UIButton new];
  36. [_scaleRotateBtn setImage:_theme.editTextPasterRotateIcon forState:UIControlStateNormal];
  37. UIPanGestureRecognizer* panGensture = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector (handlePanSlide:)];
  38. [self addSubview:_scaleRotateBtn];
  39. [_scaleRotateBtn addGestureRecognizer:panGensture];
  40. [_borderView addSubview:_pasterImageView];
  41. UIPanGestureRecognizer* selfPanGensture = [[UIPanGestureRecognizer alloc] initWithTarget:self action: @selector (handlePanSlide:)];
  42. [self addGestureRecognizer:selfPanGensture];
  43. UIPinchGestureRecognizer* pinchGensture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
  44. [self addGestureRecognizer:pinchGensture];
  45. UIRotationGestureRecognizer* rotateGensture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
  46. [self addGestureRecognizer:rotateGensture];
  47. _rotateAngle = 0.f;
  48. }
  49. return self;
  50. }
  51. - (void)layoutSubviews
  52. {
  53. [super layoutSubviews];
  54. if (self.ugckit_width == 0 || self.ugckit_height == 0) return;
  55. CGPoint center = [self convertPoint:self.center fromView:self.superview];
  56. _borderView.bounds = CGRectMake(0, 0, self.bounds.size.width - 25, self.bounds.size.height - 25);
  57. _borderView.center = center;
  58. _pasterImageView.frame = CGRectMake(0, 0, _borderView.bounds.size.width, _borderView.bounds.size.height);
  59. _deleteBtn.center = CGPointMake(_borderView.ugckit_x, _borderView.ugckit_y);
  60. _deleteBtn.bounds = CGRectMake(0, 0, 50, 50);
  61. _scaleRotateBtn.center = CGPointMake(_borderView.ugckit_right, _borderView.ugckit_bottom);
  62. _scaleRotateBtn.bounds = CGRectMake(0, 0, 50, 50);
  63. }
  64. - (void)setImageList:(NSArray *)imageList imageDuration:(float)duration;
  65. {
  66. if (imageList.count > 1) {
  67. _pasterImageView.animationImages = imageList;
  68. _pasterImageView.animationDuration = imageList.count / duration;
  69. [_pasterImageView startAnimating];
  70. }else if (imageList.count > 0){
  71. _pasterImageView.image = imageList[0];
  72. }
  73. }
  74. - (CGRect)pasterFrameOnView:(UIView *)view
  75. {
  76. CGRect frame = CGRectMake(_borderView.ugckit_x, _borderView.ugckit_y, _borderView.bounds.size.width, _borderView.bounds.size.height);
  77. if (![view.subviews containsObject:self]) {
  78. [view addSubview:self];
  79. CGRect rc = [self convertRect:frame toView:view];
  80. [self removeFromSuperview];
  81. return rc;
  82. }
  83. return [self convertRect:frame toView:view];
  84. }
  85. #pragma mark - GestureRecognizer handle
  86. - (void)onTap:(UITapGestureRecognizer*)recognizer
  87. {
  88. [self.delegate onPasterViewTap];
  89. }
  90. - (void)handlePanSlide:(UIPanGestureRecognizer*)recognizer
  91. {
  92. //拖动
  93. if (recognizer.view == self) {
  94. CGPoint translation = [recognizer translationInView:self.superview];
  95. CGPoint center = CGPointMake(recognizer.view.center.x + translation.x,
  96. recognizer.view.center.y + translation.y);
  97. if (center.x < 0) {
  98. center.x = 0;
  99. }
  100. else if (center.x > self.superview.ugckit_width) {
  101. center.x = self.superview.ugckit_width;
  102. }
  103. if (center.y < 0) {
  104. center.y = 0;
  105. }
  106. else if (center.y > self.superview.ugckit_height) {
  107. center.y = self.superview.ugckit_height;
  108. }
  109. recognizer.view.center = center;
  110. [recognizer setTranslation:CGPointZero inView:self.superview];
  111. }
  112. else if (recognizer.view == _scaleRotateBtn) {
  113. CGPoint translation = [recognizer translationInView:self];
  114. //放大
  115. if (recognizer.state == UIGestureRecognizerStateChanged) {
  116. CGFloat delta = translation.x;
  117. self.bounds = CGRectMake(0, 0, self.bounds.size.width + delta, self.bounds.size.height + delta);
  118. }
  119. [recognizer setTranslation:CGPointZero inView:self];
  120. //旋转
  121. CGPoint newCenter = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
  122. CGPoint anthorPoint = _pasterImageView.center;
  123. CGFloat height = newCenter.y - anthorPoint.y;
  124. CGFloat width = newCenter.x - anthorPoint.x;
  125. CGFloat angle1 = atan(height / width);
  126. height = recognizer.view.center.y - anthorPoint.y;
  127. width = recognizer.view.center.x - anthorPoint.x;
  128. CGFloat angle2 = atan(height / width);
  129. CGFloat angle = angle1 - angle2;
  130. self.transform = CGAffineTransformRotate(self.transform, angle);
  131. _rotateAngle += angle;
  132. }
  133. }
  134. //双手指放大
  135. - (void)handlePinch:(UIPinchGestureRecognizer*)recognizer
  136. {
  137. self.bounds = CGRectMake(0, 0, self.bounds.size.width * recognizer.scale, self.bounds.size.height * recognizer.scale);
  138. recognizer.scale = 1;
  139. }
  140. ////双手指旋转
  141. - (void)handleRotate:(UIRotationGestureRecognizer*)recognizer
  142. {
  143. recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
  144. _rotateAngle += recognizer.rotation;
  145. recognizer.rotation = 0;
  146. }
  147. //如果是静态贴纸,生成静态贴纸图片
  148. - (UIImage*)staticImage
  149. {
  150. _borderView.layer.borderWidth = 0;
  151. [_borderView setNeedsDisplay];
  152. CGRect rect = _borderView.bounds;
  153. UIView *rotatedViewBox = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rect.size.width , rect.size.height)];
  154. CGAffineTransform t = CGAffineTransformMakeRotation(_rotateAngle);
  155. rotatedViewBox.transform = t;
  156. CGSize rotatedSize = rotatedViewBox.frame.size;
  157. UIGraphicsBeginImageContextWithOptions(rotatedSize, NO, 0.f);
  158. CGContextRef context = UIGraphicsGetCurrentContext();
  159. CGContextTranslateCTM(context, rotatedSize.width/2, rotatedSize.height/2);
  160. CGContextRotateCTM(context, _rotateAngle);
  161. //[_textLabel drawTextInRect:CGRectMake(-rect.size.width / 2, -rect.size.height / 2, rect.size.width, rect.size.height)];
  162. [_borderView drawViewHierarchyInRect:CGRectMake(-rect.size.width / 2, -rect.size.height / 2, rect.size.width, rect.size.height) afterScreenUpdates:YES];
  163. UIImage *rotatedImg = UIGraphicsGetImageFromCurrentImageContext();
  164. UIGraphicsEndImageContext();
  165. _borderView.layer.borderWidth = 1;
  166. _borderView.layer.borderColor = UIColorFromRGB(0x0accac).CGColor;
  167. return rotatedImg;
  168. }
  169. - (void)onDeleteBtnClicked:(UIButton*)sender
  170. {
  171. [self.delegate onRemovePasterView:self];
  172. [self removeFromSuperview];
  173. }
  174. - (void)dealloc
  175. {
  176. [[NSNotificationCenter defaultCenter] removeObserver:self];
  177. }
  178. @end