123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //
- // PanoramaOrientationView.m
- // LenzCameraNativeModuleForRN
- //
- // Created by 王昭威 on 2023/1/25.
- //
- #import "PanoramaOrientationView.h"
- #import <Masonry/Masonry.h>
- @interface PanoramaOrientationViewItem : UIView
- @property (nonatomic, strong) UILabel* paddingLabel;
- @property (nonatomic, assign) BOOL isSel;
- - (instancetype)initText: (NSString*)text;
- @end
- @implementation PanoramaOrientationViewItem
- - (void)setIsSel:(BOOL)isSel{
- self.backgroundColor = UIColor.clearColor;
- self.paddingLabel.backgroundColor = UIColor.clearColor;
- if(isSel){
- self.paddingLabel.textColor = UIColor.blackColor;
- }else{
- self.paddingLabel.textColor = UIColor.whiteColor;
- }
- }
- - (instancetype)initText:(NSString*)text{
- self = [super init];
- if(self){
- _isSel = NO;
- self.backgroundColor = UIColor.clearColor;
- UILabel* paddingLabel = [[UILabel alloc] init];
- paddingLabel.textAlignment = NSTextAlignmentCenter;
- [self addSubview:paddingLabel];
- paddingLabel.font = [UIFont systemFontOfSize:12];
- paddingLabel.text = text;
- paddingLabel.textColor = UIColor.whiteColor;
- paddingLabel.backgroundColor = UIColor.clearColor;
- [paddingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self);
- }];
- _paddingLabel = paddingLabel;
- }
- return self;
- }
- @end
- @interface PanoramaOrientationView ()<UIGestureRecognizerDelegate>
- @property (nonatomic, strong) NSDictionary<NSNumber*, __kindof PanoramaOrientationViewItem*>* items;
- @property (nonatomic, strong) UIView *inserView;
- @property (nonatomic, assign) CGFloat left;
- @end
- @implementation PanoramaOrientationView
- - (UIView *)inserView{
- if (_inserView == nil) {
- _inserView = [[UIView alloc]init];;
- _inserView.backgroundColor = UIColor.whiteColor;
- _inserView.layer.cornerRadius = 4;
- _inserView.layer.masksToBounds = YES;
- }
- return _inserView;
- }
- - (void)setSelectedItemIndex:(PanoramaOrientationViewDirectionEnum)selectedItemIndex{
- _selectedItemIndex = selectedItemIndex;
- if(selectedItemIndex >= self.items.count){
- return;
- }
-
- [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
- obj.isSel = NO;
- }];
-
- PanoramaOrientationViewItem *item =self.items[@(self.selectedItemIndex)];
- item.isSel = YES;
- [self insertSubview:self.inserView atIndex:0];
- [UIView animateWithDuration:0.25 animations:^{
- [self.inserView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.edges.mas_equalTo(item);
- }];
- [self layoutIfNeeded];
- }];
-
- }
- - (void)awakeFromNib{
- [super awakeFromNib];
- self.userInteractionEnabled = YES;
- NSMutableDictionary* dic = [[NSMutableDictionary alloc] init];
- NSString* titles[] = {@"上", @"下", @"左", @"右",@"异型"};
- NSMutableArray<__kindof PanoramaOrientationViewItem*>* items = [NSMutableArray array];
- PanoramaOrientationViewDirectionEnum directions[] = {PanoramaOrientationViewUp, PanoramaOrientationViewDown, PanoramaOrientationViewLeft, PanoramaOrientationViewRight,PanoramaOrientationViewAny};
- for(int i = 0; i < 5; ++i){
- PanoramaOrientationViewItem* item = [[PanoramaOrientationViewItem alloc] initText:titles[i]];
- item.tag = directions[i];
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
- [item addGestureRecognizer:tap];
- [self addSubview:item];
- [dic setObject:item forKey:@(directions[i])];
- [items addObject:item];
- }
- self.items = [dic copy];
- [items.firstObject mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.mas_left);
- make.top.bottom.equalTo(self);
- make.height.mas_equalTo(30);
- }];
- PanoramaOrientationViewItem* previous = items.firstObject;
- for(int i = 1; i < 5; ++i){
- PanoramaOrientationViewItem* item = items[i];
- [item mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(previous.mas_right);
- make.width.equalTo(previous);
- make.top.bottom.equalTo(self);
- }];
- previous = item;
- }
-
- [items.lastObject mas_makeConstraints:^(MASConstraintMaker *make) {
- make.right.equalTo(self.mas_right);
- }];
- self.selectedItemIndex = PanoramaOrientationViewLeft;
- self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
- self.layer.cornerRadius = 3.5;
- self.layer.masksToBounds = YES;
- [self addGesture];
- }
- /// 添加滑动的手势,选择全景的拍摄的方向
- -(void)addGesture{
- UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panDirection:)];
- [self addGestureRecognizer:panGesture];
- panGesture.delegate = self;
- }
- - (void)tapAction:(UITapGestureRecognizer *)tap {
- UIView *view = tap.view;
- self.selectedItemIndex = view.tag;
- if ([self.delegate respondsToSelector:@selector(panoramaOrientationViewslt:)]) {
- [self.delegate panoramaOrientationViewslt:view.tag];
- }
- }
- -(void)panDirection:(UIPanGestureRecognizer *)sender{
- [self handleSwipe:sender];
- }
- - (void)handleSwipe:( UIPanGestureRecognizer *)gesture{
- if (gesture.state == UIGestureRecognizerStateBegan) {
- self.left = self.inserView.frame.origin.x;
- }
-
- if (gesture.state == UIGestureRecognizerStateChanged) {
- [self commitTranslation:[gesture translationInView:self] left:self.left];
- }
-
- if (gesture.state == UIGestureRecognizerStateEnded) {
- CGPoint center = self.inserView.center;
- [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
- if (CGRectContainsPoint(obj.frame, center)) {
- self.selectedItemIndex = key.integerValue;
- if ([self.delegate respondsToSelector:@selector(panoramaOrientationViewslt:)]) {
- [self.delegate panoramaOrientationViewslt:key.integerValue];
- }
- }
- }];
- }
- }
- - (void)commitTranslation:(CGPoint)translation left:(CGFloat)left{
- CGFloat absX = fabs(translation.x);
- CGFloat absY = fabs(translation.y);
- // 设置滑动有效距离
- if (MAX(absX, absY) < 10) return;
- CGFloat margin = left + translation.x;
- if (margin < 0) {
- margin = 0;
- }
-
- if (margin+self.inserView.frame.size.width >= self.bounds.size.width) {
- margin = self.bounds.size.width - CGRectGetWidth(self.inserView.bounds);
- }
- [self.inserView mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.bottom.offset(0);
- make.width.mas_equalTo(self.items[@0].mas_width);
- make.left.mas_equalTo(margin);
- }];
-
- CGPoint center = self.inserView.center;
- [self.items enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull key, __kindof PanoramaOrientationViewItem * _Nonnull obj, BOOL * _Nonnull stop) {
- if (CGRectContainsPoint(obj.frame, center)) {
- obj.isSel = YES;
- }else{
- obj.isSel = NO;
- }
- }];
- }
- @end
|