PanoramaGuideView.m 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. // self.lastImage = image;
  403. dispatch_async(dispatch_get_main_queue(), ^{
  404. __block CGFloat imageWH = 0;
  405. if (!image) {
  406. self.offSetTop = @((120-38)/2);
  407. self.offSetLeft = @((120-38)/2);
  408. }
  409. self.showImageView.image = image;
  410. [UIView animateWithDuration:.15 animations:^{
  411. if (self.dir == TgDirectionPortrait) {
  412. if (self.orginDir == PanoramaOrientationViewDown) {
  413. if (image) {
  414. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  415. }
  416. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  417. make.height.mas_equalTo(imageWH);
  418. make.height.mas_lessThanOrEqualTo(self);
  419. make.left.right.bottom.mas_equalTo(0);
  420. }];
  421. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  422. make.bottom.mas_offset(-imageWH);
  423. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  424. }];
  425. } else if (self.orginDir == PanoramaOrientationViewUp) {
  426. if (image) {
  427. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  428. }
  429. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  430. make.left.right.top.mas_equalTo(0);
  431. make.height.mas_equalTo(imageWH);
  432. make.height.mas_lessThanOrEqualTo(self);
  433. }];
  434. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  435. make.top.mas_offset(imageWH);
  436. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  437. }];
  438. } else if (self.orginDir == PanoramaOrientationViewLeft) {
  439. if (image) {
  440. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  441. }
  442. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  443. make.width.mas_equalTo(imageWH);
  444. make.width.mas_lessThanOrEqualTo(self);
  445. make.left.top.bottom.mas_equalTo(0);
  446. }];
  447. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  448. make.left.mas_offset(imageWH);
  449. make.top.mas_equalTo(self.offSetTop.doubleValue);
  450. }];
  451. } else if (self.orginDir == PanoramaOrientationViewRight) {
  452. if (image) {
  453. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  454. }
  455. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  456. make.width.mas_equalTo(imageWH);
  457. make.width.mas_lessThanOrEqualTo(self);
  458. make.top.right.bottom.mas_equalTo(0);
  459. }];
  460. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  461. make.right.mas_offset(-imageWH);
  462. make.top.mas_equalTo(self.offSetTop.doubleValue);
  463. }];
  464. }
  465. } else if (self.dir == TgDirectionLeft) {
  466. if (self.orginDir == PanoramaOrientationViewLeft) {
  467. if (image) {
  468. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  469. }
  470. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  471. make.left.right.top.mas_equalTo(0);
  472. make.height.mas_equalTo(imageWH);
  473. make.height.mas_lessThanOrEqualTo(self);
  474. }];
  475. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  476. make.top.mas_offset(imageWH);
  477. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  478. }];
  479. } else if (self.orginDir == PanoramaOrientationViewRight) {
  480. if (image) {
  481. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  482. }
  483. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  484. make.height.mas_equalTo(imageWH);
  485. make.height.mas_lessThanOrEqualTo(self);
  486. make.left.right.bottom.mas_equalTo(0);
  487. }];
  488. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  489. make.bottom.mas_offset(-imageWH);
  490. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  491. }];
  492. } else if (self.orginDir == PanoramaOrientationViewUp) {
  493. if (image) {
  494. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  495. }
  496. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  497. make.width.mas_equalTo(imageWH);
  498. make.width.mas_lessThanOrEqualTo(self);
  499. make.top.right.bottom.mas_equalTo(0);
  500. }];
  501. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  502. make.right.mas_offset(-imageWH);
  503. make.top.mas_equalTo(self.offSetTop.doubleValue);
  504. }];
  505. } else if (self.orginDir == PanoramaOrientationViewDown) {
  506. if (image) {
  507. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  508. }
  509. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  510. make.width.mas_equalTo(imageWH);
  511. make.width.mas_lessThanOrEqualTo(self);
  512. make.left.top.bottom.mas_equalTo(0);
  513. }];
  514. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  515. make.left.mas_offset(imageWH);
  516. make.top.mas_equalTo(self.offSetTop.doubleValue);
  517. }];
  518. }
  519. } else if (self.dir == TgDirectionRight) {
  520. if (self.orginDir == PanoramaOrientationViewLeft) {
  521. if (image) {
  522. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  523. }
  524. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  525. make.height.mas_equalTo(imageWH);
  526. make.height.mas_lessThanOrEqualTo(self);
  527. make.left.right.bottom.mas_equalTo(0);
  528. }];
  529. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  530. make.bottom.mas_offset(-imageWH);
  531. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  532. }];
  533. } else if (self.orginDir == PanoramaOrientationViewRight) {
  534. if (image) {
  535. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  536. }
  537. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  538. make.left.right.top.mas_equalTo(0);
  539. make.height.mas_equalTo(imageWH);
  540. make.height.mas_lessThanOrEqualTo(self);
  541. }];
  542. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  543. make.top.mas_offset(imageWH);
  544. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  545. }];
  546. } else if (self.orginDir == PanoramaOrientationViewUp) {
  547. if (image) {
  548. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  549. }
  550. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  551. make.width.mas_equalTo(imageWH);
  552. make.width.mas_lessThanOrEqualTo(self);
  553. make.left.top.bottom.mas_equalTo(0);
  554. }];
  555. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  556. make.left.mas_offset(imageWH);
  557. make.top.mas_equalTo(self.offSetTop.doubleValue);
  558. }];
  559. } else if (self.orginDir == PanoramaOrientationViewDown) {
  560. if (image) {
  561. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  562. }
  563. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  564. make.width.mas_equalTo(imageWH);
  565. make.width.mas_lessThanOrEqualTo(self);
  566. make.top.right.bottom.mas_equalTo(0);
  567. }];
  568. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  569. make.right.mas_offset(-imageWH);
  570. make.top.mas_equalTo(self.offSetTop.doubleValue);
  571. }];
  572. }
  573. } else if (self.dir == TgDirectionDown) {
  574. if (self.orginDir == PanoramaOrientationViewLeft) {
  575. if (image) {
  576. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  577. }
  578. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  579. make.width.mas_equalTo(imageWH);
  580. make.width.mas_lessThanOrEqualTo(self);
  581. make.top.right.bottom.mas_equalTo(0);
  582. }];
  583. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  584. make.right.mas_offset(-imageWH);
  585. make.top.mas_equalTo(self.offSetTop.doubleValue);
  586. }];
  587. } else if (self.orginDir == PanoramaOrientationViewRight) {
  588. if (image) {
  589. imageWH = (PanoramaGuideViewWOrH * image.size.width)/image.size.height;
  590. }
  591. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  592. make.width.mas_equalTo(imageWH);
  593. make.width.mas_lessThanOrEqualTo(self);
  594. make.left.top.bottom.mas_equalTo(0);
  595. }];
  596. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  597. make.left.mas_offset(imageWH);
  598. make.top.mas_equalTo(self.offSetTop.doubleValue);
  599. }];
  600. } else if (self.orginDir == PanoramaOrientationViewUp) {
  601. if (image) {
  602. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  603. }
  604. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  605. make.height.mas_equalTo(imageWH);
  606. make.height.mas_lessThanOrEqualTo(self);
  607. make.left.right.bottom.mas_equalTo(0);
  608. }];
  609. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  610. make.bottom.mas_offset(-imageWH);
  611. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  612. }];
  613. } else if (self.orginDir == PanoramaOrientationViewDown) {
  614. if (image) {
  615. imageWH = (PanoramaGuideViewWOrH * image.size.height)/image.size.width;
  616. }
  617. [self.showImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
  618. make.left.right.top.mas_equalTo(0);
  619. make.height.mas_equalTo(imageWH);
  620. make.height.mas_lessThanOrEqualTo(self);
  621. }];
  622. [self.whiteArrow mas_remakeConstraints:^(MASConstraintMaker *make) {
  623. make.top.mas_offset(imageWH);
  624. make.left.mas_equalTo(self.offSetLeft.doubleValue);
  625. }];
  626. }
  627. }
  628. [self setNeedsLayout];
  629. [self layoutIfNeeded];
  630. }];
  631. });
  632. }
  633. - (void)updateArrowWith:(PanoramaArrowDir)arrowDir dir:(PanoramaOrientationViewDirectionEnum)dir {
  634. // self.lastArrowDir = arrowDir;
  635. dispatch_async(dispatch_get_main_queue(), ^{
  636. if (!self.showImageView.image) {
  637. return;
  638. }
  639. if (self.dir == TgDirectionPortrait || self.dir == TgDirectionDown) {
  640. if (dir == PanoramaOrientationViewDown || dir == PanoramaOrientationViewUp) {
  641. if (arrowDir == PanoramaArrowDirCenter) {
  642. self.offSetLeft = @((120-38)/2);
  643. } else if (arrowDir == PanoramaArrowDirLeft) {
  644. self.offSetLeft = @(0);
  645. } else if (arrowDir == PanoramaArrowDirRight) {
  646. self.offSetLeft = @(120-38);
  647. }
  648. } else if (dir == PanoramaOrientationViewLeft || dir == PanoramaOrientationViewRight) {
  649. if (arrowDir == PanoramaArrowDirCenter) {
  650. self.offSetTop = @((120-38)/2);
  651. } else if (arrowDir == PanoramaArrowDirUp) {
  652. self.offSetTop = @(0);
  653. } else if (arrowDir == PanoramaArrowDirDown) {
  654. self.offSetTop = @(120-38);
  655. }
  656. }
  657. } else if (self.dir == TgDirectionLeft || self.dir == TgDirectionRight) {
  658. if (dir == PanoramaOrientationViewLeft || dir == PanoramaOrientationViewRight) {
  659. if (arrowDir == PanoramaArrowDirCenter) {
  660. self.offSetLeft = @((120-38)/2);
  661. } else if (arrowDir == PanoramaArrowDirLeft) {
  662. self.offSetLeft = @(0);
  663. } else if (arrowDir == PanoramaArrowDirRight) {
  664. self.offSetLeft = @(120-38);
  665. }
  666. } else {
  667. if (arrowDir == PanoramaArrowDirCenter) {
  668. self.offSetTop = @((120-38)/2);
  669. } else if (arrowDir == PanoramaArrowDirUp) {
  670. self.offSetTop = @(0);
  671. } else if (arrowDir == PanoramaArrowDirDown) {
  672. self.offSetTop = @(120-38);
  673. }
  674. }
  675. }
  676. [self showImageWith:self.showImageView.image];
  677. });
  678. }
  679. - (void)clearImage {
  680. dispatch_async(dispatch_get_main_queue(), ^{
  681. self.showImageView.image = nil;
  682. });
  683. }
  684. - (UIImage *)showDir:(PanoramaOrientationViewDirectionEnum)dir image:(UIImage *)image {
  685. switch (dir) {
  686. case PanoramaOrientationViewUp: {
  687. UIImage *flipImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationRight];
  688. return flipImage;
  689. }
  690. break;
  691. case PanoramaOrientationViewDown: {
  692. UIImage *flipImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationLeft];
  693. return flipImage;
  694. }
  695. break;
  696. case PanoramaOrientationViewLeft: {
  697. return image;
  698. }
  699. break;
  700. case PanoramaOrientationViewRight: {
  701. UIImage *flipImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
  702. return flipImage;
  703. }
  704. break;
  705. default:
  706. break;
  707. }
  708. return nil;
  709. }
  710. - (void)setHidden:(BOOL)hidden{
  711. [super setHidden:hidden];
  712. self.label.hidden = hidden;
  713. }
  714. - (void)awakeFromNib{
  715. [super awakeFromNib];
  716. self.container = [[PanoramaGuideViewPreviewContainer alloc] initWithFrame:CGRectMake(0, 0, 50, 50) device:[Renderer shared].device];
  717. self.container.clearColor = MTLClearColorMake(0, 0, 0, 1);
  718. self.container.backgroundColor = [UIColor redColor];
  719. [self addSubview:self.container];
  720. self.container.delegate = self;
  721. self.container.sampleCount = 1;
  722. self.container.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
  723. [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
  724. make.left.top.equalTo(self);
  725. make.height.equalTo(self);
  726. make.width.equalTo(@(50));
  727. }];
  728. [[Renderer shared] setup];
  729. }
  730. - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size{
  731. [[Renderer shared] mtkView:view drawableSizeWillChange:size];
  732. }
  733. - (void)drawInMTKView:(MTKView *)view{
  734. [[Renderer shared] drawInMTKView:view];
  735. }
  736. - (YYLabel *)label{
  737. if (_label == nil) {
  738. NSAttributedString *att = [[NSAttributedString alloc]initWithString:@"请沿箭头方向匀速拍摄"];
  739. _label = [[YYLabel alloc]init];
  740. // _label.numberOfLines = 0;
  741. _label.backgroundColor = [UIColor jk_colorWithHexString:@"000000" andAlpha:0.35];
  742. _label.attributedText = att;
  743. _label.textAlignment = NSTextAlignmentCenter;
  744. _label.layer.cornerRadius = 3;
  745. _label.layer.masksToBounds = YES;
  746. // _label.textVerticalAlignment = YYTextVerticalAlignmentCenter; // 文字置顶显示
  747. _label.textColor = UIColor.whiteColor;
  748. }
  749. return _label;
  750. }
  751. - (UIImageView *)showImageView {
  752. if (!_showImageView) {
  753. _showImageView = [[UIImageView alloc]init];
  754. _showImageView.contentMode = UIViewContentModeScaleAspectFill;
  755. _showImageView.clipsToBounds = YES;
  756. }
  757. return _showImageView;
  758. }
  759. - (UIView *)line{
  760. if (_line == nil) {
  761. _line = [[UIView alloc]init];
  762. _line.backgroundColor = UIColor.yellowColor;
  763. }
  764. return _line;
  765. }
  766. - (UIImageView *)whiteArrow{
  767. if (_whiteArrow == nil) {
  768. _whiteArrow = [[UIImageView alloc]initWithImage:[UIImage loadNamed:@"ai_right_arrow"]];
  769. }
  770. return _whiteArrow;
  771. }
  772. /*
  773. // Only override drawRect: if you perform custom drawing.
  774. // An empty implementation adversely affects performance during animation.
  775. - (void)drawRect:(CGRect)rect {
  776. // Drawing code
  777. }
  778. */
  779. @end