diff --git a/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.h b/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.h index 729d35df78c3efe0951873e91d2c3b036244307d..21536950c1b5242901c19f246a0d6330b7d99a54 100644 --- a/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.h +++ b/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.h @@ -8,7 +8,12 @@ #import +/// 入口滑动浮窗(默认记录上次坐标) @interface DoraemonEntryView : UIWindow + +/// 是否自动停靠,默认为YES +@property (nonatomic, assign) BOOL autoDock; + // 定制位置 - (instancetype)initWithStartPoint:(CGPoint)startingPosition; - (void)show; diff --git a/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.m b/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.m index d1212a9ebdb62602057c72eddc48175426698730..77ec8b8ba319125cda94986d21e54d2f143f9835 100644 --- a/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.m +++ b/iOS/DoraemonKit/Src/Core/Entry/View/DoraemonEntryView.m @@ -136,6 +136,10 @@ } - (void)pan:(UIPanGestureRecognizer *)sender{ + if (self.autoDock) { + [self autoDocking:sender]; + return; + } //1、获得拖动位移 CGPoint offsetPoint = [sender translationInView:sender.view]; //2、清空拖动位移 @@ -157,6 +161,61 @@ newY = DoraemonScreenHeight - _kEntryViewSize/2; } panView.center = CGPointMake(newX, newY); + [[NSUserDefaults standardUserDefaults] setObject:@{ + @"x":[NSNumber numberWithFloat:newX], + @"y":[NSNumber numberWithFloat:newY] + } forKey:@"FloatViewCenterLocation"]; +} + +- (void)autoDocking:(UIPanGestureRecognizer *)panGestureRecognizer { + UIView *panView = panGestureRecognizer.view; + switch (panGestureRecognizer.state) { + case UIGestureRecognizerStateBegan: + case UIGestureRecognizerStateChanged: + { + CGPoint translation = [panGestureRecognizer translationInView:panView]; + [panGestureRecognizer setTranslation:CGPointZero inView:panView]; + panView.center = CGPointMake(panView.center.x + translation.x, panView.center.y + translation.y); + } + break; + case UIGestureRecognizerStateEnded: + case UIGestureRecognizerStateCancelled: + { + CGPoint location = panView.center; + CGFloat centerX; + CGFloat safeBottom = 0; + if (@available(iOS 11.0, *)) { + safeBottom = self.safeAreaInsets.bottom; + } + CGFloat centerY = MAX(MIN(location.y, CGRectGetMaxY([UIScreen mainScreen].bounds)-safeBottom), [UIApplication sharedApplication].statusBarFrame.size.height); + if(location.x > CGRectGetWidth([UIScreen mainScreen].bounds)/2.f) + { + centerX = CGRectGetWidth([UIScreen mainScreen].bounds)-6; + } + else + { + centerX = 6.f; + } + [[NSUserDefaults standardUserDefaults] setObject:@{ + @"x":[NSNumber numberWithFloat:centerX], + @"y":[NSNumber numberWithFloat:centerY] + } forKey:@"FloatViewCenterLocation"]; + [UIView animateWithDuration:0.3 animations:^{ + panView.center = CGPointMake(centerX, centerY); + }]; + } + + default: + break; + } +} + +- (void)setAutoDock:(BOOL)autoDock { + _autoDock = autoDock; + NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"FloatViewCenterLocation"]; + if (dict && dict[@"x"] && dict[@"y"]) { + self.center = CGPointMake([dict[@"x"] integerValue], [dict[@"y"] integerValue]); + } } @end diff --git a/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.h b/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.h index 79a1de745fa30ca286e330f2b45a5848b5da3b28..5da8859b7363c2a0e86546638b0416ffc4cd3925 100644 --- a/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.h +++ b/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.h @@ -120,6 +120,10 @@ typedef NS_ENUM(NSUInteger, DoraemonManagerPluginType) { - (void)installWithCustomBlock:(void(^)(void))customBlock; +/// 设置是否自动停靠屏幕边缘 +/// @param autoDock bool +- (void)setAutoDock:(BOOL)autoDock; + @property (nonatomic,strong) NSMutableArray *dataArray; @property (nonatomic, copy) DoraemonH5DoorBlock h5DoorBlock; diff --git a/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.m b/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.m index f8bb53ac2c99a4eef0b67a1cf6d282a151aefa83..33e991d05fa16c08535aa1c9c07f015180bf53a9 100644 --- a/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.m +++ b/iOS/DoraemonKit/Src/Core/Manager/DoraemonManager.m @@ -102,6 +102,7 @@ typedef void (^DoraemonPerformanceBlock)(NSDictionary *); [self installWithCustomBlock:^{ //什么也没发生 }]; + [self setAutoDock:YES]; } - (void)installWithCustomBlock:(void(^)(void))customBlock{ @@ -191,6 +192,9 @@ typedef void (^DoraemonPerformanceBlock)(NSDictionary *); } +- (void)setAutoDock:(BOOL)autoDock { + self.entryView.autoDock = autoDock; +} /** 初始化内置工具数据