HKClipperVeiw.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // HKClipperVeiw.m
  3. // HKBaseDemo
  4. //
  5. // Created by hukaiyin on 16/8/9.
  6. // Copyright © 2016年 hukaiyin. All rights reserved.
  7. //
  8. #import "HKClipperVeiw.h"
  9. #import "UIImage+ClipperExtends.h"
  10. static const CGFloat minWidth = 60;
  11. @interface HKClipperVeiw()
  12. @property (nonatomic, strong) UIImageView *clipperView;
  13. @property (nonatomic, strong) UIImageView *baseImgView;
  14. @property (nonatomic, strong) CAShapeLayer *fillLayer;
  15. @end
  16. @implementation HKClipperVeiw {
  17. CGPoint panTouch;
  18. CGFloat scaleDistance; //缩放距离
  19. }
  20. #pragma mark - Life Cycle
  21. - (instancetype)initWithFrame:(CGRect)frame {
  22. self = [super initWithFrame:frame];
  23. if (self) {
  24. [self loadSubViews];
  25. }
  26. return self;
  27. }
  28. - (void)awakeFromNib {
  29. [super awakeFromNib];
  30. [self loadSubViews];
  31. }
  32. - (void)loadSubViews {
  33. // self.layer.contentsScale = [UIScreen mainScreen].scale;
  34. self.layer.contentsGravity = kCAGravityResizeAspect;
  35. }
  36. #pragma mark - Public
  37. - (UIImage *)clipImg {
  38. CGFloat scale = [UIScreen mainScreen].scale * self.baseImgView.image.size.width/self.baseImgView.frame.size.width;
  39. CGRect rect = [self convertRect:self.clipperView.frame toView:self.baseImgView];
  40. CGRect rect2 = CGRectMake(rect.origin.x * scale, rect.origin.y * scale, scale *rect.size.width, scale * rect.size.height);
  41. CGImageRef cgImg = CGImageCreateWithImageInRect(self.baseImgView.image.CGImage, rect2);
  42. UIImage *clippedImg = [UIImage imageWithCGImage:cgImg];
  43. CGImageRelease(cgImg);
  44. return clippedImg;
  45. }
  46. #pragma mark - Touches
  47. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  48. NSSet *allTouches = [event allTouches];
  49. switch ([allTouches count]) {
  50. case 1:{
  51. panTouch = [[allTouches anyObject] locationInView:self];
  52. break;
  53. }
  54. case 2:{
  55. break;
  56. }
  57. default:
  58. break;
  59. }
  60. }
  61. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  62. [self willChangeValueForKey:@"crop"];
  63. NSSet *allTouches = [event allTouches];
  64. switch ([allTouches count])
  65. {
  66. case 1: {
  67. CGPoint touchCurrent = [[allTouches anyObject] locationInView:self];
  68. CGFloat x = touchCurrent.x - panTouch.x;
  69. CGFloat y = touchCurrent.y - panTouch.y;
  70. switch (self.type) {
  71. case ClipperTypeImgMove: {
  72. self.baseImgView.center = CGPointMake(self.baseImgView.center.x + x, self.baseImgView.center.y + y);
  73. break;
  74. }
  75. case ClipperTypeImgStay: {
  76. self.clipperView.center = CGPointMake(self.clipperView.center.x + x, self.clipperView.center.y + y);
  77. break;
  78. }
  79. }
  80. panTouch = touchCurrent;
  81. } break;
  82. case 2: {
  83. switch (self.type) {
  84. case ClipperTypeImgMove: {
  85. [self scaleView:self.baseImgView touches:[allTouches allObjects]];
  86. break;
  87. }
  88. case ClipperTypeImgStay: {
  89. [self scaleView:self.clipperView touches:[allTouches allObjects]];
  90. break;
  91. }
  92. }
  93. } break;
  94. }
  95. [self correctFillLayer];
  96. }
  97. - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  98. switch (self.type) {
  99. case ClipperTypeImgMove: {
  100. [self correctBackImgView];
  101. break;
  102. }
  103. case ClipperTypeImgStay: {
  104. [self correctClipperView];
  105. break;
  106. }
  107. }
  108. }
  109. #pragma mark - Correct
  110. - (void)correctBackImgView {
  111. CGFloat x = self.baseImgView.frame.origin.x;
  112. CGFloat y = self.baseImgView.frame.origin.y;
  113. CGFloat height = self.baseImgView.frame.size.height;
  114. CGFloat width = self.baseImgView.frame.size.width;
  115. if (width < self.clipperView.frame.size.width) {
  116. width = self.clipperView.frame.size.width;
  117. height = width / self.baseImgView.frame.size.width * height;
  118. }
  119. if (height < self.clipperView.frame.size.height) {
  120. height = self.clipperView.frame.size.height;
  121. width = height / self.baseImgView.frame.size.height * width;
  122. }
  123. if(x > self.clipperView.frame.origin.x) {
  124. x = self.clipperView.frame.origin.x;
  125. } else if (x <(self.clipperView.frame.origin.x + self.clipperView.frame.size.width - width)) {
  126. x = self.clipperView.frame.origin.x + self.clipperView.frame.size.width - width;
  127. }
  128. if (y > self.clipperView.frame.origin.y) {
  129. y = self.clipperView.frame.origin.y;
  130. } else if (y < (self.clipperView.frame.origin.y + self.clipperView.frame.size.height - height)) {
  131. y = self.clipperView.frame.origin.y + self.clipperView.frame.size.height - height;
  132. }
  133. self.baseImgView.frame = CGRectMake(x, y, width, height);
  134. }
  135. - (void)correctClipperView {
  136. CGFloat width = self.clipperView.frame.size.width;
  137. CGFloat height;
  138. if (width < minWidth) {
  139. width = minWidth;
  140. }
  141. if (width > [UIScreen mainScreen].bounds.size.width) {
  142. width = [UIScreen mainScreen].bounds.size.width;
  143. }
  144. height = width/self.resultImgSize.width * self.resultImgSize.height;
  145. CGFloat x = self.clipperView.frame.origin.x;
  146. CGFloat y = self.clipperView.frame.origin.y;
  147. if (x < self.baseImgView.frame.origin.x) {
  148. x = self.baseImgView.frame.origin.x;
  149. }
  150. if(x > [UIScreen mainScreen].bounds.size.width - width){
  151. x = [UIScreen mainScreen].bounds.size.width - width;
  152. }
  153. if (y < self.baseImgView.frame.origin.y) {
  154. y = self.baseImgView.frame.origin.y;
  155. }
  156. if(y > (self.baseImgView.frame.origin.y + self.baseImgView.frame.size.height - self.clipperView.frame.size.height)) {
  157. y = self.baseImgView.frame.origin.y + self.baseImgView.frame.size.height - self.clipperView.frame.size.height;
  158. }
  159. self.clipperView.frame = CGRectMake(x, y, width, height);
  160. [self correctFillLayer];
  161. }
  162. - (void)correctFillLayer {
  163. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:0];
  164. UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:_clipperView.frame cornerRadius:0];
  165. [path appendPath:circlePath];
  166. [path setUsesEvenOddFillRule:YES];
  167. self.fillLayer.path = path.CGPath;
  168. }
  169. #pragma mark - Utilities
  170. //根据两点缩放View
  171. - (void)scaleView:(UIView *)view touches:(NSArray *)touches {
  172. CGPoint touch1 = [[touches objectAtIndex:0] locationInView:self];
  173. CGPoint touch2 = [[touches objectAtIndex:1] locationInView:self];
  174. CGFloat distance = [self distanceBetweenTwoPoints:touch1 toPoint:touch2];
  175. if (scaleDistance>0) {
  176. CGRect imgFrame=view.frame;
  177. if (distance>scaleDistance+2) {
  178. imgFrame.size.width+=10;
  179. scaleDistance=distance;
  180. }
  181. if (distance<scaleDistance-2) {
  182. imgFrame.size.width -= 10;
  183. scaleDistance=distance;
  184. }
  185. imgFrame.size.height=CGRectGetHeight(view.frame)*imgFrame.size.width/CGRectGetWidth(view.frame);
  186. float addwidth=imgFrame.size.width-view.frame.size.width;
  187. float addheight=imgFrame.size.height-view.frame.size.height;
  188. if (imgFrame.size.width != 0 && imgFrame.size.height != 0) {
  189. view.frame=CGRectMake(imgFrame.origin.x-addwidth/2.0f, imgFrame.origin.y-addheight/2.0f, imgFrame.size.width, imgFrame.size.height);
  190. }
  191. }else {
  192. scaleDistance = distance;
  193. }
  194. }
  195. - (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint {
  196. CGFloat x = toPoint.x - fromPoint.x;
  197. CGFloat y = toPoint.y - fromPoint.y;
  198. return sqrtf(x * x + y * y);
  199. }
  200. #pragma mark - Setters & Getters
  201. - (void)setBaseImg:(UIImage *)baseImg {
  202. _baseImg = baseImg;
  203. //调整背景图片大小
  204. CGFloat width = _baseImg.size.width;
  205. CGFloat height = _baseImg.size.height;
  206. if (width != self.frame.size.width) {
  207. width = self.frame.size.width;
  208. }
  209. height = _baseImg.size.height / _baseImg.size.width * width;
  210. if (height < self.clipperView.frame.size.height) {
  211. height = self.clipperView.frame.size.height;
  212. }
  213. width = _baseImg.size.width / _baseImg.size.height * height;
  214. UIImage *img = [_baseImg scaledToSize:CGSizeMake(width, height) withScale:NO];
  215. self.baseImgView.image = img;
  216. self.baseImgView.frame = CGRectMake(0, 0, img.size.width, img.size.height);
  217. [self correctBackImgView];
  218. }
  219. - (UIImageView *)baseImgView {
  220. if (!_baseImgView) {
  221. _baseImgView = [[UIImageView alloc]init];
  222. [self addSubview:_baseImgView];
  223. [self sendSubviewToBack:_baseImgView];
  224. }
  225. return _baseImgView;
  226. }
  227. - (void)setResultImgSize:(CGSize)resultImgSize {
  228. _resultImgSize = resultImgSize;
  229. [self clipperView];
  230. }
  231. - (UIImageView *)clipperView {
  232. if (!_clipperView) {
  233. CGFloat kscWidth = [UIScreen mainScreen].bounds.size.width;
  234. CGFloat kscHeight = [UIScreen mainScreen].bounds.size.height - 64;
  235. CGFloat width = kscWidth;
  236. CGFloat height = kscHeight;
  237. if (self.resultImgSize.width > (self.resultImgSize.height)/height *width) {
  238. height = [UIScreen mainScreen].bounds.size.width / self.resultImgSize.width * self.resultImgSize.height;
  239. } else {
  240. width = [UIScreen mainScreen].bounds.size.height / self.resultImgSize.height * self.resultImgSize.width;
  241. }
  242. CGFloat y = (kscHeight - height) / 2;
  243. CGFloat x = (kscWidth - width) / 2;
  244. _clipperView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
  245. // _clipperView.image = [UIImage imageNamed:@"img_clipper_border"];
  246. _clipperView.layer.borderColor = [UIColor whiteColor].CGColor;
  247. _clipperView.layer.borderWidth = 2;
  248. [self addSubview:_clipperView];
  249. [self correctFillLayer];
  250. }
  251. return _clipperView ;
  252. }
  253. - (CAShapeLayer *)fillLayer {
  254. if (!_fillLayer) {
  255. _fillLayer = [CAShapeLayer layer];
  256. _fillLayer.fillRule = kCAFillRuleEvenOdd;
  257. _fillLayer.fillColor = [UIColor blackColor].CGColor;
  258. _fillLayer.opacity =0.5;
  259. [self.layer addSublayer:_fillLayer];
  260. }
  261. return _fillLayer;
  262. }
  263. @end