PanoramaOrientationView.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // PanoramaOrientationView.m
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/25.
  6. //
  7. #import "PanoramaOrientationView.h"
  8. #import <Masonry/Masonry.h>
  9. @interface PanoramaOrientationViewItem : UIView
  10. @property (nonatomic, strong) UILabel* paddingLabel;
  11. @property (nonatomic, assign) BOOL isSel;
  12. - (instancetype)initText: (NSString*)text;
  13. @end
  14. @implementation PanoramaOrientationViewItem
  15. - (void)setIsSel:(BOOL)isSel{
  16. self.backgroundColor = UIColor.clearColor;
  17. self.paddingLabel.backgroundColor = UIColor.clearColor;
  18. if(isSel){
  19. self.paddingLabel.textColor = UIColor.blackColor;
  20. }else{
  21. self.paddingLabel.textColor = UIColor.whiteColor;
  22. }
  23. }
  24. - (instancetype)initText:(NSString*)text{
  25. self = [super init];
  26. if(self){
  27. _isSel = NO;
  28. self.backgroundColor = UIColor.clearColor;
  29. UILabel* paddingLabel = [[UILabel alloc] init];
  30. paddingLabel.textAlignment = NSTextAlignmentCenter;
  31. [self addSubview:paddingLabel];
  32. paddingLabel.font = [UIFont systemFontOfSize:13];
  33. paddingLabel.text = text;
  34. paddingLabel.textColor = UIColor.whiteColor;
  35. paddingLabel.backgroundColor = UIColor.clearColor;
  36. [paddingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  37. make.edges.equalTo(self);
  38. }];
  39. _paddingLabel = paddingLabel;
  40. }
  41. return self;
  42. }
  43. @end
  44. @interface PanoramaOrientationView ()<UIGestureRecognizerDelegate>
  45. @property (nonatomic, strong) NSDictionary<NSNumber*, __kindof PanoramaOrientationViewItem*>* items;
  46. @property (nonatomic, strong) UIView *inserView;
  47. @property (nonatomic, assign) CGFloat left;
  48. @end
  49. @implementation PanoramaOrientationView
  50. - (UIView *)inserView{
  51. if (_inserView == nil) {
  52. _inserView = [[UIView alloc]init];;
  53. _inserView.backgroundColor = UIColor.whiteColor;
  54. _inserView.layer.cornerRadius = 4;
  55. _inserView.layer.masksToBounds = YES;
  56. }
  57. return _inserView;
  58. }
  59. - (void)setSelectedItemIndex:(PanoramaOrientationViewDirectionEnum)selectedItemIndex{
  60. _selectedItemIndex = selectedItemIndex;
  61. if(selectedItemIndex >= self.items.count){
  62. return;
  63. }
  64. [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
  65. obj.isSel = NO;
  66. }];
  67. PanoramaOrientationViewItem *item =self.items[@(self.selectedItemIndex)];
  68. item.isSel = YES;
  69. [self insertSubview:self.inserView atIndex:0];
  70. [UIView animateWithDuration:0.25 animations:^{
  71. [self.inserView mas_remakeConstraints:^(MASConstraintMaker *make) {
  72. make.edges.mas_equalTo(item);
  73. }];
  74. [self layoutIfNeeded];
  75. }];
  76. }
  77. - (void)awakeFromNib{
  78. [super awakeFromNib];
  79. self.userInteractionEnabled = YES;
  80. NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
  81. NSString* titles[] = {@"上", @"下", @"左", @"右",@"异形"};
  82. NSMutableArray<__kindof PanoramaOrientationViewItem*>* items = [NSMutableArray array];
  83. PanoramaOrientationViewDirectionEnum directions[] = {PanoramaOrientationViewUp, PanoramaOrientationViewDown, PanoramaOrientationViewLeft, PanoramaOrientationViewRight,PanoramaOrientationViewAny};
  84. for(int i = 0; i < 5; ++i){
  85. PanoramaOrientationViewItem* item = [[PanoramaOrientationViewItem alloc] initText:titles[i]];
  86. item.tag = directions[i];
  87. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
  88. [item addGestureRecognizer:tap];
  89. [self addSubview:item];
  90. [dic setObject:item forKey:@(directions[i])];
  91. [items addObject:item];
  92. }
  93. self.items = [dic copy];
  94. [items.firstObject mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.left.equalTo(self.mas_left);
  96. make.top.bottom.equalTo(self);
  97. make.width.mas_offset(44);
  98. make.height.mas_equalTo(22);
  99. }];
  100. PanoramaOrientationViewItem* previous = items.firstObject;
  101. for(int i = 1; i < 5; ++i){
  102. PanoramaOrientationViewItem* item = items[i];
  103. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.left.equalTo(previous.mas_right);
  105. make.width.equalTo(previous);
  106. make.top.bottom.equalTo(self);
  107. }];
  108. previous = item;
  109. }
  110. [items.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.right.equalTo(self.mas_right);
  112. }];
  113. self.selectedItemIndex = PanoramaOrientationViewAny;
  114. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
  115. self.layer.cornerRadius = 3.5;
  116. self.layer.masksToBounds = YES;
  117. [self addGesture];
  118. }
  119. /// 添加滑动的手势,选择全景的拍摄的方向
  120. -(void)addGesture{
  121. UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panDirection:)];
  122. [self addGestureRecognizer:panGesture];
  123. panGesture.delegate = self;
  124. }
  125. - (void)tapAction:(UITapGestureRecognizer *)tap {
  126. UIView *view = tap.view;
  127. self.selectedItemIndex = view.tag;
  128. if ([self.delegate respondsToSelector:@selector(panoramDirDidChange)]) {
  129. [self.delegate panoramDirDidChange];
  130. }
  131. }
  132. -(void)panDirection:(UIPanGestureRecognizer *)sender{
  133. [self handleSwipe:sender];
  134. }
  135. - (void)handleSwipe:( UIPanGestureRecognizer *)gesture{
  136. if (gesture.state == UIGestureRecognizerStateBegan) {
  137. self.left = self.inserView.frame.origin.x;
  138. }
  139. if (gesture.state == UIGestureRecognizerStateChanged) {
  140. [self commitTranslation:[gesture translationInView:self] left:self.left];
  141. }
  142. if (gesture.state == UIGestureRecognizerStateEnded) {
  143. CGPoint center = self.inserView.center;
  144. [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
  145. if (CGRectContainsPoint(obj.frame, center)) {
  146. self.selectedItemIndex = key.integerValue;
  147. if ([self.delegate respondsToSelector:@selector(panoramDirDidChange)]) {
  148. [self.delegate panoramDirDidChange];
  149. }
  150. }
  151. }];
  152. }
  153. }
  154. - (void)commitTranslation:(CGPoint)translation left:(CGFloat)left{
  155. CGFloat absX = fabs(translation.x);
  156. CGFloat absY = fabs(translation.y);
  157. // 设置滑动有效距离
  158. if (MAX(absX, absY) < 10) return;
  159. CGFloat margin = left + translation.x;
  160. if (margin < 0) {
  161. margin = 0;
  162. }
  163. if (margin+self.inserView.frame.size.width >= self.bounds.size.width) {
  164. margin = self.bounds.size.width - CGRectGetWidth(self.inserView.bounds);
  165. }
  166. [self.inserView mas_remakeConstraints:^(MASConstraintMaker *make) {
  167. make.top.bottom.offset(0);
  168. make.width.mas_equalTo(self.items[@0].mas_width);
  169. make.left.mas_equalTo(margin);
  170. }];
  171. CGPoint center = self.inserView.center;
  172. [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
  173. if (CGRectContainsPoint(obj.frame, center)) {
  174. obj.isSel = YES;
  175. }else{
  176. obj.isSel = NO;
  177. }
  178. }];
  179. }
  180. @end