1.悬浮球与设备刘海的安全距离
无论是横屏还是竖屏,悬浮球距离有刘海的一边会留出安全距离
设备方向的上下两边,也有安全距离
2.贴边吸附方向和距离
悬浮球只能贴设备方向的左右两边,需要贴上下两边自行调整
距离边缘的数值自行调整
3.切换横竖屏,悬浮球自适应
悬浮球位置切换横竖屏后,等比例转换的。
4.隐藏和显示
拖到屏幕中间ImageView范围内可以隐藏悬浮窗,在范围内会由蓝色变红色,可自定义图片或者大小
使用说明:
悬浮球点击事件代理方法- (void)suspendViewButtonClick:(UIButton*)sender;
悬浮球在ImageView范围内提示是否隐藏悬浮窗- (void)showHideAlertView;
显示悬浮窗- (void)showSuspendView;
隐藏悬浮窗- (void)dismissSuspendView;
demo下载地址:
https://github.com/longypjiangxi/XLUIDragButton
简书地址:
https://www.jianshu.com/p/30aeb1d506d3
#import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @protocol SuspendViewDelegate <NSObject> - (void)suspendViewButtonClick:(UIButton*)sender; - (void)showHideAlertView; @end @interface SuspendView : UIView { CGPoint lastPoint;/**存储悬浮球最后的移动完位置*/ BOOL isChangePosition;/**悬浮球是否改变了位置*/ CGFloat changeHig;//按钮高度位置比例 CGFloat changeWid;//按钮宽度位置比例 } @property (nonatomic, retain) UIButton *btn;/**<#name#>*/ @property (nonatomic, strong) NSTimer *_Nullable timer; @property (nonatomic, retain) UIImageView *imageView; @property (nonatomic, assign) UIInterfaceOrientation orientation; @property (nonatomic, weak) id<SuspendViewDelegate> delegate; - (void)showSuspendView; - (void)dismissSuspendView; @end NS_ASSUME_NONNULL_END #import "SuspendView.h" #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width #define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height #define ViewSize 50 #define KHeightFit(w) (((w) / 667.0) * SCREEN_HEIGHT) #define LRString [NSString stringWithFormat:@"%s", __FILE__].lastPathComponent #define DLog(...) {\ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];\ [dateFormatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];\ NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];\ printf("%s %s 第%d行:%s\n\n",[dateString UTF8String],[LRString UTF8String] ,__LINE__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String]);} @implementation SuspendView - (instancetype)init{ self = [super init]; if (self) { self.backgroundColor = UIColor.redColor; self.layer.masksToBounds = YES; self.layer.cornerRadius = ViewSize/2; self.alpha = 0.5; //获取设备方向 self.orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (self.orientation == UIInterfaceOrientationLandscapeRight){//横向home键在右侧,设备左转,刘海在左边 self.frame = CGRectMake(SCREEN_WIDTH - [self vg_safeDistanceTop] - ViewSize - 20, KHeightFit(80) + ViewSize/2, ViewSize, ViewSize); }else{ self.frame = CGRectMake(SCREEN_WIDTH - ViewSize/2, KHeightFit(80) + ViewSize/2, ViewSize, ViewSize); } self.btn = [UIButton buttonWithType:UIButtonTypeCustom]; self.btn.frame = CGRectMake(5, 5, 40, 40); self.btn.backgroundColor = UIColor.greenColor; self.btn.layer.cornerRadius = 20; [self.btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.btn]; //获取按钮与屏幕初始宽高比例 [self changeCoordinateScale]; //是否改变了悬浮窗初始位置 isChangePosition = NO; //添加手势 UIPanGestureRecognizer *panRcognize=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; [panRcognize setMinimumNumberOfTouches:1];