PanoramaOrientationView.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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:12];
  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.height.mas_equalTo(30);
  98. }];
  99. PanoramaOrientationViewItem* previous = items.firstObject;
  100. for(int i = 1; i < 5; ++i){
  101. PanoramaOrientationViewItem* item = items[i];
  102. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  103. make.left.equalTo(previous.mas_right);
  104. make.width.equalTo(previous);
  105. make.top.bottom.equalTo(self);
  106. }];
  107. previous = item;
  108. }
  109. [items.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
  110. make.right.equalTo(self.mas_right);
  111. }];
  112. self.selectedItemIndex = PanoramaOrientationViewLeft;
  113. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
  114. self.layer.cornerRadius = 3.5;
  115. self.layer.masksToBounds = YES;
  116. [self addGesture];
  117. }
  118. /// 添加滑动的手势,选择全景的拍摄的方向
  119. -(void)addGesture{
  120. UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panDirection:)];
  121. [self addGestureRecognizer:panGesture];
  122. panGesture.delegate = self;
  123. }
  124. - (void)tapAction:(UITapGestureRecognizer *)tap {
  125. UIView *view = tap.view;
  126. self.selectedItemIndex = view.tag;
  127. if ([self.delegate respondsToSelector:@selector(panoramaOrientationViewslt:)]) {
  128. [self.delegate panoramaOrientationViewslt:view.tag];
  129. }
  130. }
  131. -(void)panDirection:(UIPanGestureRecognizer *)sender{
  132. [self handleSwipe:sender];
  133. }
  134. - (void)handleSwipe:( UIPanGestureRecognizer *)gesture{
  135. if (gesture.state == UIGestureRecognizerStateBegan) {
  136. self.left = self.inserView.frame.origin.x;
  137. }
  138. if (gesture.state == UIGestureRecognizerStateChanged) {
  139. [self commitTranslation:[gesture translationInView:self] left:self.left];
  140. }
  141. if (gesture.state == UIGestureRecognizerStateEnded) {
  142. CGPoint center = self.inserView.center;
  143. [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
  144. if (CGRectContainsPoint(obj.frame, center)) {
  145. self.selectedItemIndex = key.integerValue;
  146. if ([self.delegate respondsToSelector:@selector(panoramaOrientationViewslt:)]) {
  147. [self.delegate panoramaOrientationViewslt:key.integerValue];
  148. }
  149. }
  150. }];
  151. }
  152. }
  153. - (void)commitTranslation:(CGPoint)translation left:(CGFloat)left{
  154. CGFloat absX = fabs(translation.x);
  155. CGFloat absY = fabs(translation.y);
  156. // 设置滑动有效距离
  157. if (MAX(absX, absY) < 10) return;
  158. CGFloat margin = left + translation.x;
  159. if (margin < 0) {
  160. margin = 0;
  161. }
  162. if (margin+self.inserView.frame.size.width >= self.bounds.size.width) {
  163. margin = self.bounds.size.width - CGRectGetWidth(self.inserView.bounds);
  164. }
  165. [self.inserView mas_remakeConstraints:^(MASConstraintMaker *make) {
  166. make.top.bottom.offset(0);
  167. make.width.mas_equalTo(self.items[@0].mas_width);
  168. make.left.mas_equalTo(margin);
  169. }];
  170. CGPoint center = self.inserView.center;
  171. [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
  172. if (CGRectContainsPoint(obj.frame, center)) {
  173. obj.isSel = YES;
  174. }else{
  175. obj.isSel = NO;
  176. }
  177. }];
  178. }
  179. @end