PanoramaGuideView.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. //
  2. // PanoramaGuideView.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/25.
  6. //
  7. #import "PanoramaGuideView.h"
  8. #import <YYText/NSAttributedString+YYText.h>
  9. #import <AVFoundation/AVFoundation.h>
  10. #import <YYText/YYLabel.h>
  11. #import <MetalKit/MetalKit.h>
  12. #import "Renderer.h"
  13. #import "LenzHeader.h"
  14. CGFloat const PanoramaGuideViewWOrH = 120;
  15. @interface PanoramaGuideViewPreviewContainer : MTKView
  16. @end
  17. @implementation PanoramaGuideViewPreviewContainer
  18. @end
  19. @interface PanoramaGuideView() <MTKViewDelegate>
  20. @property (nonatomic, strong) PanoramaGuideViewPreviewContainer* container;
  21. @property (nonatomic, assign) CGFloat aspect;
  22. @property (nonatomic, assign) CGFloat offset;
  23. @property (nonatomic, strong) NSTimer* timer;
  24. @property (nonatomic, strong) UIImageView *whiteArrow;
  25. @property (nonatomic, strong) UIView* line;
  26. @property (nonatomic, weak) UIView* superV;
  27. @property (nonatomic, strong)YYLabel *label;
  28. @property (nonatomic) UIImageView *showImageView;
  29. @property (nonatomic) PanoramaArrowDir lastArrowDir;
  30. @property (nonatomic) NSNumber *offSetTop;
  31. @property (nonatomic) NSNumber *offSetLeft;
  32. @property (nonatomic) PanoramaOrientationViewDirectionEnum currentDirection;
  33. @end
  34. @implementation PanoramaGuideView
  35. - (instancetype)init{
  36. self = [super init];
  37. self.backgroundColor = [UIColor jk_colorWithHexString:@"#000000" andAlpha:0.5];
  38. [self addSubview:self.showImageView];
  39. self.offSetTop = @((120-38)/2);
  40. self.offSetLeft = @((120-38)/2);
  41. return self;
  42. }
  43. - (void)willMoveToSuperview:(nullable UIView *)newSuperview;{
  44. [super willMoveToSuperview:newSuperview];
  45. self.superV = newSuperview;
  46. }
  47. - (void)updatePreviewAspect:(CGFloat)aspect{
  48. if(aspect > 0.01){
  49. self.aspect = 1.0 / aspect;
  50. }
  51. else{
  52. self.aspect = 1;
  53. }
  54. [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
  55. make.left.top.equalTo(self).offset(self.offset);
  56. make.height.equalTo(self);
  57. // it means aspect > 0
  58. make.width.equalTo(self.container.mas_height).multipliedBy(self.aspect);
  59. }];
  60. }
  61. - (void)updateOffset:(CGFloat)offset{
  62. self.offset = offset;
  63. [self.container mas_remakeConstraints:^(MASConstraintMaker *make) {
  64. make.left.equalTo(self).offset(self.offset);
  65. make.top.equalTo(self);
  66. make.height.equalTo(self);
  67. make.width.equalTo(self.container.mas_height).multipliedBy(self.aspect);
  68. }];
  69. }
  70. - (void)direction:(PanoramaOrientationViewDirectionEnum)dir{
  71. self.currentDirection = dir;
  72. [self insertSubview:self.line atIndex:0];
  73. self.line.hidden = YES;
  74. // self.label.hidden = YES;
  75. [self.superV addSubview:self.label];
  76. [self addSubview:self.whiteArrow];
  77. [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
  78. switch (dir) {
  79. case PanoramaOrientationViewLeft:
  80. case PanoramaOrientationViewRight:
  81. {
  82. make.left.right.offset(0);
  83. make.height.offset(0.5);
  84. make.centerY.mas_equalTo(self);
  85. }
  86. break;
  87. case PanoramaOrientationViewUp:
  88. case PanoramaOrientationViewDown:
  89. {
  90. make.top.bottom.offset(0);
  91. make.width.offset(0.5);
  92. make.centerX.mas_equalTo(self);
  93. }
  94. break;
  95. default:
  96. break;
  97. }
  98. }];
  99. // [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  100. // switch (dir) {
  101. // case PanoramaOrientationViewLeft:
  102. // case PanoramaOrientationViewRight: // 横
  103. // {
  104. // make.top.mas_equalTo(self.mas_bottom).offset(5);
  105. // make.centerX.mas_equalTo(self.superV);
  106. // make.height.offset(20);
  107. // make.width.offset(160);
  108. // self.label.verticalForm = NO;
  109. // }
  110. // break;
  111. // case PanoramaOrientationViewUp:
  112. // case PanoramaOrientationViewDown: // 上下
  113. // {
  114. // make.right.mas_equalTo(self.mas_left).offset(-5);
  115. // make.centerY.mas_equalTo(self.superV);
  116. // make.width.offset(20);
  117. // make.height.offset(160);
  118. // self.label.verticalForm = YES;
  119. // }
  120. // break;
  121. //
  122. // default:
  123. // break;
  124. // }
  125. // }];
  126. [self updatePromptLabel];
  127. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  128. switch (dir) {
  129. case PanoramaOrientationViewUp: {
  130. make.left.right.top.mas_equalTo(0);
  131. make.height.mas_equalTo(0);
  132. make.height.mas_lessThanOrEqualTo(self);
  133. }
  134. break;
  135. case PanoramaOrientationViewDown: {
  136. make.left.right.bottom.mas_equalTo(0);
  137. make.height.mas_equalTo(0);
  138. }
  139. break;
  140. case PanoramaOrientationViewLeft: {
  141. make.left.top.bottom.mas_equalTo(0);
  142. make.width.mas_equalTo(0);
  143. make.width.mas_lessThanOrEqualTo(self);
  144. }
  145. break;
  146. case PanoramaOrientationViewRight: {
  147. make.top.right.bottom.mas_equalTo(0);
  148. make.width.mas_equalTo(0);
  149. make.width.mas_lessThanOrEqualTo(self);
  150. }
  151. break;
  152. default:
  153. break;
  154. }
  155. }];
  156. [UIView animateWithDuration:0.2 animations:^{
  157. __block UIImage *arrow = nil;
  158. /// 白色的箭头
  159. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  160. switch (dir) {
  161. case PanoramaOrientationViewLeft: //左
  162. {
  163. make.centerY.mas_equalTo(self);
  164. make.left.offset(0);
  165. arrow = [UIImage loadNamed:@"ai_right_arrow"];
  166. }
  167. break;
  168. case PanoramaOrientationViewRight: // 右
  169. {
  170. make.centerY.mas_equalTo(self);
  171. make.right.offset(0);
  172. arrow = [UIImage loadNamed:@"ai_left_arrow"];
  173. }
  174. break;
  175. case PanoramaOrientationViewUp: //上
  176. {
  177. make.centerX.mas_equalTo(self);
  178. make.top.offset(0);
  179. arrow = [UIImage loadNamed:@"ai_down_arrow"];
  180. }
  181. break;
  182. case PanoramaOrientationViewDown: // 下
  183. {
  184. make.centerX.mas_equalTo(self);
  185. make.bottom.offset(0);
  186. arrow = [UIImage loadNamed:@"ai_up_arrow"];
  187. }
  188. break;
  189. default:
  190. break;
  191. }
  192. }];
  193. self.whiteArrow.image = arrow;
  194. [self setNeedsLayout];
  195. [self layoutIfNeeded];
  196. } completion:^(BOOL finished) {
  197. self.line.hidden = NO;
  198. // self.label.hidden = NO;
  199. }];
  200. }
  201. - (void)updatePromptLabel {
  202. // self.backgroundColor = [UIColor redColor];
  203. // self.superV.backgroundColor = [UIColor yellowColor];
  204. self.label.transform = CGAffineTransformMakeRotation(0);
  205. switch (self.dir) {
  206. case TgDirectionPortrait: {
  207. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  208. switch (self.orginDir) {
  209. case PanoramaOrientationViewLeft:
  210. case PanoramaOrientationViewRight: // 横
  211. {
  212. make.top.mas_equalTo(self.mas_bottom).offset(5);
  213. make.centerX.mas_equalTo(self.superV);
  214. make.height.offset(20);
  215. make.width.offset(160);
  216. self.label.verticalForm = NO;
  217. }
  218. break;
  219. case PanoramaOrientationViewUp:
  220. case PanoramaOrientationViewDown: // 上下
  221. {
  222. make.right.mas_equalTo(self.mas_left).offset(-5);
  223. make.centerY.mas_equalTo(self.superV);
  224. make.width.offset(20);
  225. make.height.offset(160);
  226. self.label.verticalForm = YES;
  227. }
  228. break;
  229. default:
  230. break;
  231. }
  232. }];
  233. }
  234. break;
  235. case TgDirectionDown: {
  236. switch (self.orginDir) {
  237. case PanoramaOrientationViewLeft: {
  238. self.label.transform = CGAffineTransformMakeRotation(-M_PI);
  239. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  240. make.bottom.mas_equalTo(self.mas_top).mas_offset(-20);
  241. make.centerX.mas_equalTo(self);
  242. make.height.offset(20);
  243. make.width.offset(160);
  244. self.label.verticalForm = NO;
  245. }];
  246. }
  247. break;
  248. case PanoramaOrientationViewRight: // 横
  249. {
  250. // self.label.transform = CGAffineTransformMakeRotation(0);
  251. self.label.transform = CGAffineTransformMakeRotation(-M_PI/2);
  252. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  253. make.left.mas_equalTo(self.mas_right).offset(-60);
  254. make.centerY.mas_equalTo(self);
  255. make.height.offset(20);
  256. make.width.offset(160);
  257. self.label.verticalForm = NO;
  258. }];
  259. }
  260. break;
  261. case PanoramaOrientationViewUp: {
  262. self.label.transform = CGAffineTransformMakeRotation(M_PI);
  263. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  264. make.right.mas_equalTo(self.mas_left).offset(-5);
  265. make.centerY.mas_equalTo(self.superV);
  266. make.width.offset(20);
  267. make.height.offset(160);
  268. self.label.verticalForm = YES;
  269. }];
  270. }
  271. break;
  272. case PanoramaOrientationViewDown: // 上下
  273. {
  274. self.label.transform = CGAffineTransformMakeRotation(M_PI);
  275. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  276. make.right.mas_equalTo(self.mas_left).offset(-5);
  277. make.centerY.mas_equalTo(self.superV);
  278. make.width.offset(20);
  279. make.height.offset(160);
  280. self.label.verticalForm = YES;
  281. }];
  282. }
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. break;
  289. case TgDirectionLeft: {
  290. switch (self.orginDir) {
  291. case PanoramaOrientationViewLeft: {
  292. // self.label.transform = CGAffineTransformMakeRotation(0);
  293. self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
  294. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  295. make.right.mas_equalTo(self.mas_left).mas_offset(60);
  296. make.centerY.mas_equalTo(self);
  297. make.height.offset(20);
  298. make.width.offset(160);
  299. self.label.verticalForm = NO;
  300. }];
  301. }
  302. break;
  303. case PanoramaOrientationViewRight: // 横
  304. {
  305. // self.label.transform = CGAffineTransformMakeRotation(0);
  306. self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
  307. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  308. make.right.mas_equalTo(self.mas_left).mas_offset(60);
  309. make.centerY.mas_equalTo(self);
  310. make.height.offset(20);
  311. make.width.offset(160);
  312. self.label.verticalForm = NO;
  313. }];
  314. }
  315. break;
  316. case PanoramaOrientationViewUp:{
  317. self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
  318. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  319. make.top.mas_equalTo(self.mas_bottom).offset(-60);
  320. make.centerX.mas_equalTo(self.superV);
  321. make.width.offset(20);
  322. make.height.offset(160);
  323. self.label.verticalForm = YES;
  324. }];
  325. }
  326. break;
  327. case PanoramaOrientationViewDown: // 上下
  328. {
  329. self.label.transform = CGAffineTransformMakeRotation(M_PI/2);
  330. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  331. make.top.mas_equalTo(self.mas_bottom).offset(-60);
  332. make.centerX.mas_equalTo(self.superV);
  333. make.width.offset(20);
  334. make.height.offset(160);
  335. self.label.verticalForm = YES;
  336. }];
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. }
  343. break;
  344. case TgDirectionRight: {
  345. switch (self.orginDir) {
  346. case PanoramaOrientationViewLeft: {
  347. self.label.transform = CGAffineTransformMakeRotation(-M_PI/2);
  348. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  349. make.left.mas_equalTo(self.mas_right).offset(-60);
  350. make.centerY.mas_equalTo(self);
  351. make.height.offset(20);
  352. make.width.offset(160);
  353. self.label.verticalForm = NO;
  354. }];
  355. }
  356. break;
  357. case PanoramaOrientationViewRight: // 横
  358. {
  359. self.label.transform = CGAffineTransformMakeRotation(-M_PI/2);
  360. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  361. make.left.mas_equalTo(self.mas_right).offset(-60);
  362. make.centerY.mas_equalTo(self);
  363. make.height.offset(20);
  364. make.width.offset(160);
  365. self.label.verticalForm = NO;
  366. }];
  367. }
  368. break;
  369. case PanoramaOrientationViewUp:{
  370. self.label.transform = CGAffineTransformMakeRotation(-M_PI/2);
  371. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  372. make.top.mas_equalTo(self.mas_bottom).offset(-60);
  373. make.centerX.mas_equalTo(self.superV);
  374. make.width.offset(20);
  375. make.height.offset(160);
  376. self.label.verticalForm = YES;
  377. }];
  378. }
  379. break;
  380. case PanoramaOrientationViewDown: // 上下
  381. {
  382. self.label.transform = CGAffineTransformMakeRotation(-M_PI/2);
  383. [self.label mas_remakeConstraints:^(MASConstraintMaker *make) {
  384. make.top.mas_equalTo(self.mas_bottom).offset(-60);
  385. make.centerX.mas_equalTo(self.superV);
  386. make.width.offset(20);
  387. make.height.offset(160);
  388. self.label.verticalForm = YES;
  389. }];
  390. }
  391. break;
  392. default:
  393. break;
  394. }
  395. }
  396. break;
  397. default:
  398. break;
  399. }
  400. }
  401. - (void)showImageWith:(UIImage * _Nullable)image {
  402. dispatch_async(dispatch_get_main_queue(), ^{
  403. CGFloat imageWH = 0;
  404. if (image) {
  405. if (self.currentDirection == PanoramaOrientationViewDown || self.currentDirection == PanoramaOrientationViewUp) {
  406. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  407. } else {
  408. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  409. }
  410. } else {
  411. self.offSetTop = @((120-38)/2);
  412. self.offSetLeft = @((120-38)/2);
  413. }
  414. // UIImage *resultImage = [self showDir:dir image:image];
  415. self.showImageView.image = image;
  416. [UIView animateWithDuration:.15 animations:^{
  417. switch (self.currentDirection) {
  418. case PanoramaOrientationViewDown: {
  419. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  420. make.height.mas_equalTo(imageWH);
  421. make.height.mas_lessThanOrEqualTo(self);
  422. make.left.right.bottom.mas_equalTo(0);
  423. }];
  424. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  425. make.bottom.offset(-imageWH);
  426. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  427. // make.centerX.mas_equalTo(self);
  428. }];
  429. }
  430. break;
  431. case PanoramaOrientationViewUp:{
  432. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  433. make.left.right.top.mas_equalTo(0);
  434. make.height.mas_equalTo(imageWH);
  435. make.height.mas_lessThanOrEqualTo(self);
  436. }];
  437. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  438. make.top.offset(imageWH);
  439. // make.centerX.mas_equalTo(self);
  440. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  441. }];
  442. }
  443. break;
  444. case PanoramaOrientationViewRight: {
  445. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  446. make.width.mas_equalTo(imageWH);
  447. make.width.mas_lessThanOrEqualTo(self);
  448. make.top.right.bottom.mas_equalTo(0);
  449. }];
  450. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  451. make.right.offset(-imageWH);
  452. // make.centerY.mas_equalTo(self);
  453. make.top.mas_equalTo(self.offSetTop.doubleValue);
  454. }];
  455. }
  456. break;
  457. case PanoramaOrientationViewLeft:{
  458. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  459. make.width.mas_equalTo(imageWH);
  460. make.width.mas_lessThanOrEqualTo(self);
  461. make.left.top.bottom.mas_equalTo(0);
  462. }];
  463. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  464. make.left.offset(imageWH);
  465. // make.centerY.mas_equalTo(self);
  466. make.top.mas_equalTo(self.offSetTop.doubleValue);
  467. }];
  468. }
  469. break;
  470. default:
  471. break;
  472. }
  473. [self setNeedsLayout];
  474. [self layoutIfNeeded];
  475. }];
  476. });
  477. }
  478. - (void)updateArrowWith:(PanoramaArrowDir)arrowDir dir:(PanoramaOrientationViewDirectionEnum)dir {
  479. // self.lastArrowDir = arrowDir;
  480. dispatch_async(dispatch_get_main_queue(), ^{
  481. if (!self.showImageView.image) {
  482. return;
  483. }
  484. CGFloat imageWidth = self.showImageView.image.size.width;
  485. CGFloat imageHight = self.showImageView.image.size.height;
  486. if (dir == PanoramaOrientationViewDown || dir == PanoramaOrientationViewUp) {
  487. if (arrowDir == PanoramaArrowDirCenter) {
  488. self.offSetLeft = @((120-38)/2);
  489. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  490. // make.bottom.offset(-(PanoramaGuideViewWOrH * imageWidth)/imageHight);
  491. //// make.centerX.mas_equalTo(self);
  492. // make.left.mas_offset(self.offSetLeft.doubleValue);
  493. // }];
  494. } else if (arrowDir == PanoramaArrowDirLeft) {
  495. self.offSetLeft = @(0);
  496. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  497. // make.bottom.offset(-(PanoramaGuideViewWOrH * imageWidth)/imageHight);
  498. //// make.left.mas_equalTo(self).mas_offset(10);
  499. // make.left.mas_offset(self.offSetLeft.doubleValue);
  500. //
  501. // }];
  502. } else if (arrowDir == PanoramaArrowDirRight) {
  503. self.offSetLeft = @(120-38);
  504. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  505. // make.bottom.offset(-(PanoramaGuideViewWOrH * imageWidth)/imageHight);
  506. //// make.right.mas_equalTo(self).mas_offset(-10);
  507. // make.left.mas_offset(self.offSetLeft.doubleValue);
  508. //
  509. // }];
  510. }
  511. }
  512. // else if (dir == PanoramaOrientationViewUp) {
  513. // if (arrowDir == PanoramaArrowDirCenter) {
  514. // self.offSetTop = @((120-38)/2);
  515. //
  516. //// [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  517. //// make.top.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  518. ////// make.centerX.mas_equalTo(self);
  519. //// make.top.mas_offset(self.offSetTop.doubleValue);
  520. //// }];
  521. // } else if (arrowDir == PanoramaArrowDirLeft) {
  522. // self.offSetTop = @((120-38)/2);
  523. //
  524. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  525. // make.top.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  526. // make.left.mas_equalTo(self).mas_offset(10);
  527. // }];
  528. // } else if (arrowDir == PanoramaArrowDirRight) {
  529. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  530. // make.top.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  531. // make.right.mas_equalTo(self).mas_offset(-10);
  532. // }];
  533. // }
  534. //
  535. // }
  536. else if (dir == PanoramaOrientationViewLeft || dir == PanoramaOrientationViewRight) {
  537. if (arrowDir == PanoramaArrowDirCenter) {
  538. self.offSetTop = @((120-38)/2);
  539. //
  540. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  541. // make.left.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  542. //// make.centerY.mas_equalTo(self);
  543. // make.top.mas_offset(self.offSetTop.doubleValue);
  544. //
  545. // }];
  546. } else if (arrowDir == PanoramaArrowDirUp) {
  547. self.offSetTop = @(0);
  548. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  549. // make.left.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  550. //// make.top.mas_equalTo(self).mas_offset(10);
  551. // make.top.mas_offset(self.offSetTop.doubleValue);
  552. //
  553. // }];
  554. } else if (arrowDir == PanoramaArrowDirDown) {
  555. self.offSetTop = @(120-38);
  556. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  557. // make.left.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  558. //// make.bottom.mas_equalTo(self).mas_offset(-10);
  559. // make.top.mas_offset(self.offSetTop.doubleValue);
  560. //
  561. // }];
  562. }
  563. }
  564. // else if (dir == PanoramaOrientationViewRight) {
  565. // if (arrowDir == PanoramaArrowDirCenter) {
  566. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  567. // make.left.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  568. // make.centerY.mas_equalTo(self);
  569. // }];
  570. // } else if (arrowDir == PanoramaArrowDirUp) {
  571. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  572. // make.left.offset((PanoramaGuideViewWOrH * imageWidth)/imageHight);
  573. // make.top.mas_equalTo(self).mas_offset(10);
  574. // }];
  575. // } else if (arrowDir == PanoramaArrowDirDown) {
  576. // [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  577. // make.right.offset(-(PanoramaGuideViewWOrH * imageWidth)/imageHight);
  578. // make.bottom.mas_equalTo(self).mas_offset(-10);
  579. // }];
  580. // }
  581. // }
  582. });
  583. }
  584. - (void)clearImage {
  585. dispatch_async(dispatch_get_main_queue(), ^{
  586. self.showImageView.image = nil;
  587. });
  588. }
  589. - (UIImage *)showDir:(PanoramaOrientationViewDirectionEnum)dir image:(UIImage *)image {
  590. switch (dir) {
  591. case PanoramaOrientationViewUp: {
  592. UIImage *flipImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationRight];
  593. return flipImage;
  594. }
  595. break;
  596. case PanoramaOrientationViewDown: {
  597. UIImage *flipImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationLeft];
  598. return flipImage;
  599. }
  600. break;
  601. case PanoramaOrientationViewLeft: {
  602. return image;
  603. }
  604. break;
  605. case PanoramaOrientationViewRight: {
  606. UIImage *flipImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
  607. return flipImage;
  608. }
  609. break;
  610. default:
  611. break;
  612. }
  613. return nil;
  614. }
  615. - (void)setHidden:(BOOL)hidden{
  616. [super setHidden:hidden];
  617. self.label.hidden = hidden;
  618. }
  619. - (void)awakeFromNib{
  620. [super awakeFromNib];
  621. self.container = [[PanoramaGuideViewPreviewContainer alloc] initWithFrame:CGRectMake(0, 0, 50, 50) device:[Renderer shared].device];
  622. self.container.clearColor = MTLClearColorMake(0, 0, 0, 1);
  623. self.container.backgroundColor = [UIColor redColor];
  624. [self addSubview:self.container];
  625. self.container.delegate = self;
  626. self.container.sampleCount = 1;
  627. self.container.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
  628. [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
  629. make.left.top.equalTo(self);
  630. make.height.equalTo(self);
  631. make.width.equalTo(@(50));
  632. }];
  633. [[Renderer shared] setup];
  634. }
  635. - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size{
  636. [[Renderer shared] mtkView:view drawableSizeWillChange:size];
  637. }
  638. - (void)drawInMTKView:(MTKView *)view{
  639. [[Renderer shared] drawInMTKView:view];
  640. }
  641. - (YYLabel *)label{
  642. if (_label == nil) {
  643. NSAttributedString *att = [[NSAttributedString alloc]initWithString:@"请沿箭头方向匀速拍摄"];
  644. _label = [[YYLabel alloc]init];
  645. // _label.numberOfLines = 0;
  646. _label.backgroundColor = [UIColor jk_colorWithHexString:@"000000" andAlpha:0.35];
  647. _label.attributedText = att;
  648. _label.textAlignment = NSTextAlignmentCenter;
  649. _label.layer.cornerRadius = 3;
  650. _label.layer.masksToBounds = YES;
  651. // _label.textVerticalAlignment = YYTextVerticalAlignmentCenter; // 文字置顶显示
  652. _label.textColor = UIColor.whiteColor;
  653. }
  654. return _label;
  655. }
  656. - (UIImageView *)showImageView {
  657. if (!_showImageView) {
  658. _showImageView = [[UIImageView alloc]init];
  659. _showImageView.contentMode = UIViewContentModeScaleAspectFill;
  660. _showImageView.clipsToBounds = YES;
  661. }
  662. return _showImageView;
  663. }
  664. - (UIView *)line{
  665. if (_line == nil) {
  666. _line = [[UIView alloc]init];
  667. _line.backgroundColor = UIColor.yellowColor;
  668. }
  669. return _line;
  670. }
  671. - (UIImageView *)whiteArrow{
  672. if (_whiteArrow == nil) {
  673. _whiteArrow = [[UIImageView alloc]initWithImage:[UIImage loadNamed:@"ai_right_arrow"]];
  674. }
  675. return _whiteArrow;
  676. }
  677. /*
  678. // Only override drawRect: if you perform custom drawing.
  679. // An empty implementation adversely affects performance during animation.
  680. - (void)drawRect:(CGRect)rect {
  681. // Drawing code
  682. }
  683. */
  684. @end