VideoPasterView.m 8.2 KB

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