设为首页 加入收藏

TOP

iOS 悬浮球效果实现,悬浮按钮,拖拽,贴边,隐藏,显示,旋转屏幕适配(一)
2023-07-23 13:26:37 】 浏览:401
Tags:iOS 拖拽 贴边 隐藏 显示

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];
首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS 组件化实施过程 下一篇上架app store流程详解

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目