TOCropToolbar.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // TOCropToolbar.h
  3. //
  4. // Copyright 2015 Timothy Oliver. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to
  8. // deal in the Software without restriction, including without limitation the
  9. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  21. // IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. #import "TOCropToolbar.h"
  23. @interface TOCropToolbar()
  24. @property (nonatomic, strong) UIButton *doneTextButton;
  25. @property (nonatomic, strong) UIButton *doneIconButton;
  26. @property (nonatomic, strong) UIButton *cancelTextButton;
  27. @property (nonatomic, strong) UIButton *cancelIconButton;
  28. @property (nonatomic, strong) UIButton *rotateButton;
  29. @property (nonatomic, strong) UIButton *resetButton;
  30. @property (nonatomic, strong) UIButton *clampButton;
  31. - (void)setup;
  32. - (void)buttonTapped:(id)button;
  33. + (UIImage *)doneImage;
  34. + (UIImage *)cancelImage;
  35. + (UIImage *)resetImage;
  36. + (UIImage *)rotateImage;
  37. + (UIImage *)clampImage;
  38. @end
  39. @implementation TOCropToolbar
  40. - (instancetype)initWithFrame:(CGRect)frame
  41. {
  42. if (self = [super initWithFrame:frame]) {
  43. [self setup];
  44. }
  45. return self;
  46. }
  47. - (void)setup {
  48. self.backgroundColor = [UIColor colorWithWhite:0.12f alpha:1.0f];
  49. _doneTextButton = [UIButton buttonWithType:UIButtonTypeSystem];
  50. _doneTextButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  51. [_doneTextButton setTitle:NSLocalizedStringFromTableInBundle(@"完成",
  52. @"TOCropViewControllerLocalizable",[NSBundle bundleForClass:[self class]],
  53. nil)forState:UIControlStateNormal];
  54. [_doneTextButton setTitleColor:[UIColor colorWithRed:1.0f green:0.8f blue:0.0f alpha:1.0f] forState:UIControlStateNormal];
  55. [_doneTextButton.titleLabel setFont:[UIFont systemFontOfSize:17.0f]];
  56. [_doneTextButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  57. [self addSubview:_doneTextButton];
  58. _doneIconButton = [UIButton buttonWithType:UIButtonTypeSystem];
  59. [_doneIconButton setImage:[TOCropToolbar doneImage] forState:UIControlStateNormal];
  60. [_doneIconButton setTintColor:[UIColor colorWithRed:1.0f green:0.8f blue:0.0f alpha:1.0f]];
  61. [_doneIconButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  62. [self addSubview:_doneIconButton];
  63. _cancelTextButton = [UIButton buttonWithType:UIButtonTypeSystem];
  64. _cancelTextButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  65. [_cancelTextButton setTitle:NSLocalizedStringFromTableInBundle(@"取消",
  66. @"TOCropViewControllerLocalizable",
  67. [NSBundle bundleForClass:[self class]],
  68. nil)
  69. forState:UIControlStateNormal];
  70. [_cancelTextButton.titleLabel setFont:[UIFont systemFontOfSize:17.0f]];
  71. [_cancelTextButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  72. [self addSubview:_cancelTextButton];
  73. _cancelIconButton = [UIButton buttonWithType:UIButtonTypeSystem];
  74. [_cancelIconButton setImage:[TOCropToolbar cancelImage] forState:UIControlStateNormal];
  75. [_cancelIconButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  76. [self addSubview:_cancelIconButton];
  77. // _clampButton = [UIButton buttonWithType:UIButtonTypeSystem];
  78. // _clampButton.contentMode = UIViewContentModeCenter;
  79. // _clampButton.tintColor = [UIColor whiteColor];
  80. // [_clampButton setImage:[TOCropToolbar clampImage] forState:UIControlStateNormal];
  81. // [_clampButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  82. // [self addSubview:_clampButton];
  83. _rotateButton = [UIButton buttonWithType:UIButtonTypeSystem];
  84. _rotateButton.contentMode = UIViewContentModeCenter;
  85. _rotateButton.tintColor = [UIColor whiteColor];
  86. [_rotateButton setImage:[TOCropToolbar rotateImage] forState:UIControlStateNormal];
  87. [_rotateButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  88. [self addSubview:_rotateButton];
  89. _resetButton = [UIButton buttonWithType:UIButtonTypeSystem];
  90. _resetButton.contentMode = UIViewContentModeCenter;
  91. _resetButton.tintColor = [UIColor whiteColor];
  92. _resetButton.enabled = NO;
  93. [_resetButton setImage:[TOCropToolbar resetImage] forState:UIControlStateNormal];
  94. [_resetButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
  95. [self addSubview:_resetButton];
  96. }
  97. - (void)layoutSubviews
  98. {
  99. [super layoutSubviews];
  100. BOOL verticalLayout = (CGRectGetWidth(self.bounds) < CGRectGetHeight(self.bounds));
  101. CGSize boundsSize = self.bounds.size;
  102. self.cancelIconButton.hidden = (!verticalLayout);
  103. self.cancelTextButton.hidden = (verticalLayout);
  104. self.doneIconButton.hidden = (!verticalLayout);
  105. self.doneTextButton.hidden = (verticalLayout);
  106. self.rotateButton.hidden = self.rotateButtonHidden;
  107. if (verticalLayout == NO) {
  108. CGRect frame = CGRectZero;
  109. frame.size.height = 44.0f;
  110. frame.size.width = [self.cancelTextButton.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.cancelTextButton.titleLabel.font}].width + 10;
  111. self.cancelTextButton.frame = frame;
  112. frame.size.width = [self.doneTextButton.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.doneTextButton.titleLabel.font}].width + 10;
  113. frame.origin.x = boundsSize.width - CGRectGetWidth(frame);
  114. self.doneTextButton.frame = frame;
  115. CGRect containerRect = (CGRect){0,0,165.0f,44.0f};
  116. containerRect.origin.x = (CGRectGetWidth(self.bounds) - (CGRectGetWidth(containerRect))) * 0.5f;
  117. CGRect buttonFrame = (CGRect){0,0,44.0f,44.0f};
  118. if (self.rotateButtonHidden) {
  119. buttonFrame.origin.x = CGRectGetMinX(containerRect);
  120. self.resetButton.frame = buttonFrame;
  121. }
  122. else {
  123. buttonFrame.origin.x = CGRectGetMinX(containerRect);
  124. self.rotateButton.frame = buttonFrame;
  125. buttonFrame.origin.x = CGRectGetMidX(containerRect) - 22.0f;
  126. self.resetButton.frame = buttonFrame;
  127. }
  128. // buttonFrame.origin.x = CGRectGetMaxX(containerRect) - 44.0f;
  129. // self.clampButton.frame = buttonFrame;
  130. }
  131. else {
  132. CGRect frame = CGRectZero;
  133. frame.size.height = 44.0f;
  134. frame.size.width = 44.0f;
  135. frame.origin.y = CGRectGetHeight(self.bounds) - 44.0f;
  136. self.cancelIconButton.frame = frame;
  137. frame.origin.y = 0.0f;
  138. frame.size.width = 44.0f;
  139. frame.size.height = 44.0f;
  140. self.doneIconButton.frame = frame;
  141. CGRect containerRect = (CGRect){0,0,44.0f,165.0f};
  142. containerRect.origin.y = (CGRectGetHeight(self.bounds) - (CGRectGetHeight(containerRect))) * 0.5f;
  143. CGRect buttonFrame = (CGRect){0,0,44.0f,44.0f};
  144. if (self.rotateButtonHidden) {
  145. buttonFrame.origin.y = CGRectGetMinY(containerRect);
  146. self.resetButton.frame = buttonFrame;
  147. }
  148. else {
  149. buttonFrame.origin.y = CGRectGetMinY(containerRect);
  150. self.rotateButton.frame = buttonFrame;
  151. buttonFrame.origin.y = CGRectGetMidY(containerRect) - 22.0f;
  152. self.resetButton.frame = buttonFrame;
  153. }
  154. // buttonFrame.origin.y = CGRectGetMaxY(containerRect) - 44.0f;
  155. // self.clampButton.frame = buttonFrame;
  156. }
  157. }
  158. - (void)buttonTapped:(id)button
  159. {
  160. if (button == self.cancelTextButton || button == self.cancelIconButton) {
  161. if (self.cancelButtonTapped)
  162. self.cancelButtonTapped();
  163. }
  164. else if (button == self.doneTextButton || button == self.doneIconButton) {
  165. if (self.doneButtonTapped)
  166. self.doneButtonTapped();
  167. }
  168. else if (button == self.resetButton && self.resetButtonTapped) {
  169. self.resetButtonTapped();
  170. }
  171. else if (button == self.rotateButton && self.rotateButtonTapped) {
  172. self.rotateButtonTapped();
  173. }
  174. else if (button == self.clampButton && self.clampButtonTapped) {
  175. self.clampButtonTapped();
  176. return;
  177. }
  178. }
  179. - (CGRect)clampButtonFrame
  180. {
  181. return self.clampButton.frame;
  182. }
  183. - (void)setClampButtonGlowing:(BOOL)clampButtonGlowing
  184. {
  185. if (_clampButtonGlowing == clampButtonGlowing)
  186. return;
  187. _clampButtonGlowing = clampButtonGlowing;
  188. if (_clampButtonGlowing)
  189. self.clampButton.tintColor = nil;
  190. else
  191. self.clampButton.tintColor = [UIColor whiteColor];
  192. }
  193. - (void)setRotateButtonHidden:(BOOL)rotateButtonHidden
  194. {
  195. if (_rotateButtonHidden == rotateButtonHidden)
  196. return;
  197. _rotateButtonHidden = rotateButtonHidden;
  198. [self setNeedsLayout];
  199. }
  200. - (BOOL)resetButtonEnabled
  201. {
  202. return self.resetButton.enabled;
  203. }
  204. - (void)setResetButtonEnabled:(BOOL)resetButtonEnabled
  205. {
  206. self.resetButton.enabled = resetButtonEnabled;
  207. }
  208. - (CGRect)doneButtonFrame
  209. {
  210. if (self.doneIconButton.hidden == NO)
  211. return self.doneIconButton.frame;
  212. return self.doneTextButton.frame;
  213. }
  214. #pragma mark - Image Generation -
  215. + (UIImage *)doneImage
  216. {
  217. UIImage *doneImage = nil;
  218. UIGraphicsBeginImageContextWithOptions((CGSize){17,14}, NO, 0.0f);
  219. {
  220. //// Rectangle Drawing
  221. UIBezierPath* rectanglePath = UIBezierPath.bezierPath;
  222. [rectanglePath moveToPoint: CGPointMake(1, 7)];
  223. [rectanglePath addLineToPoint: CGPointMake(6, 12)];
  224. [rectanglePath addLineToPoint: CGPointMake(16, 1)];
  225. [UIColor.whiteColor setStroke];
  226. rectanglePath.lineWidth = 2;
  227. [rectanglePath stroke];
  228. doneImage = UIGraphicsGetImageFromCurrentImageContext();
  229. }
  230. UIGraphicsEndImageContext();
  231. return doneImage;
  232. }
  233. + (UIImage *)cancelImage
  234. {
  235. UIImage *cancelImage = nil;
  236. UIGraphicsBeginImageContextWithOptions((CGSize){16,16}, NO, 0.0f);
  237. {
  238. UIBezierPath* bezierPath = UIBezierPath.bezierPath;
  239. [bezierPath moveToPoint: CGPointMake(15, 15)];
  240. [bezierPath addLineToPoint: CGPointMake(1, 1)];
  241. [UIColor.whiteColor setStroke];
  242. bezierPath.lineWidth = 2;
  243. [bezierPath stroke];
  244. //// Bezier 2 Drawing
  245. UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
  246. [bezier2Path moveToPoint: CGPointMake(1, 15)];
  247. [bezier2Path addLineToPoint: CGPointMake(15, 1)];
  248. [UIColor.whiteColor setStroke];
  249. bezier2Path.lineWidth = 2;
  250. [bezier2Path stroke];
  251. cancelImage = UIGraphicsGetImageFromCurrentImageContext();
  252. }
  253. UIGraphicsEndImageContext();
  254. return cancelImage;
  255. }
  256. + (UIImage *)rotateImage
  257. {
  258. UIImage *rotateImage = nil;
  259. UIGraphicsBeginImageContextWithOptions((CGSize){18,21}, NO, 0.0f);
  260. {
  261. //// Rectangle 2 Drawing
  262. UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake(0, 9, 12, 12)];
  263. [UIColor.whiteColor setFill];
  264. [rectangle2Path fill];
  265. //// Rectangle 3 Drawing
  266. UIBezierPath* rectangle3Path = UIBezierPath.bezierPath;
  267. [rectangle3Path moveToPoint: CGPointMake(5, 3)];
  268. [rectangle3Path addLineToPoint: CGPointMake(10, 6)];
  269. [rectangle3Path addLineToPoint: CGPointMake(10, 0)];
  270. [rectangle3Path addLineToPoint: CGPointMake(5, 3)];
  271. [rectangle3Path closePath];
  272. [UIColor.whiteColor setFill];
  273. [rectangle3Path fill];
  274. //// Bezier Drawing
  275. UIBezierPath* bezierPath = UIBezierPath.bezierPath;
  276. [bezierPath moveToPoint: CGPointMake(10, 3)];
  277. [bezierPath addCurveToPoint: CGPointMake(17.5, 11) controlPoint1: CGPointMake(15, 3) controlPoint2: CGPointMake(17.5, 5.91)];
  278. [UIColor.whiteColor setStroke];
  279. bezierPath.lineWidth = 1;
  280. [bezierPath stroke];
  281. rotateImage = UIGraphicsGetImageFromCurrentImageContext();
  282. }
  283. UIGraphicsEndImageContext();
  284. return rotateImage;
  285. }
  286. + (UIImage *)resetImage
  287. {
  288. UIImage *resetImage = nil;
  289. UIGraphicsBeginImageContextWithOptions((CGSize){22,18}, NO, 0.0f);
  290. {
  291. //// Bezier 2 Drawing
  292. UIBezierPath* bezier2Path = UIBezierPath.bezierPath;
  293. [bezier2Path moveToPoint: CGPointMake(22, 9)];
  294. [bezier2Path addCurveToPoint: CGPointMake(13, 18) controlPoint1: CGPointMake(22, 13.97) controlPoint2: CGPointMake(17.97, 18)];
  295. [bezier2Path addCurveToPoint: CGPointMake(13, 16) controlPoint1: CGPointMake(13, 17.35) controlPoint2: CGPointMake(13, 16.68)];
  296. [bezier2Path addCurveToPoint: CGPointMake(20, 9) controlPoint1: CGPointMake(16.87, 16) controlPoint2: CGPointMake(20, 12.87)];
  297. [bezier2Path addCurveToPoint: CGPointMake(13, 2) controlPoint1: CGPointMake(20, 5.13) controlPoint2: CGPointMake(16.87, 2)];
  298. [bezier2Path addCurveToPoint: CGPointMake(6.55, 6.27) controlPoint1: CGPointMake(10.1, 2) controlPoint2: CGPointMake(7.62, 3.76)];
  299. [bezier2Path addCurveToPoint: CGPointMake(6, 9) controlPoint1: CGPointMake(6.2, 7.11) controlPoint2: CGPointMake(6, 8.03)];
  300. [bezier2Path addLineToPoint: CGPointMake(4, 9)];
  301. [bezier2Path addCurveToPoint: CGPointMake(4.65, 5.63) controlPoint1: CGPointMake(4, 7.81) controlPoint2: CGPointMake(4.23, 6.67)];
  302. [bezier2Path addCurveToPoint: CGPointMake(7.65, 1.76) controlPoint1: CGPointMake(5.28, 4.08) controlPoint2: CGPointMake(6.32, 2.74)];
  303. [bezier2Path addCurveToPoint: CGPointMake(13, 0) controlPoint1: CGPointMake(9.15, 0.65) controlPoint2: CGPointMake(11, 0)];
  304. [bezier2Path addCurveToPoint: CGPointMake(22, 9) controlPoint1: CGPointMake(17.97, 0) controlPoint2: CGPointMake(22, 4.03)];
  305. [bezier2Path closePath];
  306. [UIColor.whiteColor setFill];
  307. [bezier2Path fill];
  308. //// Polygon Drawing
  309. UIBezierPath* polygonPath = UIBezierPath.bezierPath;
  310. [polygonPath moveToPoint: CGPointMake(5, 15)];
  311. [polygonPath addLineToPoint: CGPointMake(10, 9)];
  312. [polygonPath addLineToPoint: CGPointMake(0, 9)];
  313. [polygonPath addLineToPoint: CGPointMake(5, 15)];
  314. [polygonPath closePath];
  315. [UIColor.whiteColor setFill];
  316. [polygonPath fill];
  317. resetImage = UIGraphicsGetImageFromCurrentImageContext();
  318. }
  319. return resetImage;
  320. }
  321. + (UIImage *)clampImage
  322. {
  323. UIImage *clampImage = nil;
  324. UIGraphicsBeginImageContextWithOptions((CGSize){22,16}, NO, 0.0f);
  325. {
  326. //// Color Declarations
  327. UIColor* outerBox = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.553];
  328. UIColor* innerBox = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.773];
  329. //// Rectangle Drawing
  330. UIBezierPath* rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 3, 13, 13)];
  331. [UIColor.whiteColor setFill];
  332. [rectanglePath fill];
  333. //// Outer
  334. {
  335. //// Top Drawing
  336. UIBezierPath* topPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, 22, 2)];
  337. [outerBox setFill];
  338. [topPath fill];
  339. //// Side Drawing
  340. UIBezierPath* sidePath = [UIBezierPath bezierPathWithRect: CGRectMake(19, 2, 3, 14)];
  341. [outerBox setFill];
  342. [sidePath fill];
  343. }
  344. //// Rectangle 2 Drawing
  345. UIBezierPath* rectangle2Path = [UIBezierPath bezierPathWithRect: CGRectMake(14, 3, 4, 13)];
  346. [innerBox setFill];
  347. [rectangle2Path fill];
  348. clampImage = UIGraphicsGetImageFromCurrentImageContext();
  349. }
  350. UIGraphicsEndImageContext();
  351. return clampImage;
  352. }
  353. @end