iOS开发之实现自定义浮动操作框效果
今天有个需求是如上图实现类似微信的自定义浮动操作框效果
我自己就写了个demo,大家感兴趣的可以试试,下面是代码
VC代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #import "TestCustomMenuItemVC.h" #import "CustomMenuItemView.h" @interface TestCustomMenuItemVC () /** 移动视图,给这个视图来添加浮动窗*/ @property (strong, nonatomic ) UIView *moveView; @end @implementation TestCustomMenuItemVC - (UIView *)moveView { if (_moveView == nil ) { _moveView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]; _moveView.backgroundColor = [UIColor greenColor]; [ self .view addSubview:_moveView]; } return _moveView; } - ( void )viewDidLoad { [ super viewDidLoad]; } - ( void )touchesBegan:( NSSet <UITouch *> *)touches withEvent:(UIEvent *)event { //解析出随机点击页面的坐标 UITouch *touch = touches.anyObject; CGPoint point = [touch locationInView: self .view]; NSLog (@ "point x-%@ y-%@" , @(point.x), @(point.y)); //随机改变移动视图宽高 NSInteger width = 50 + arc4random() % 200; NSInteger height = 20 + arc4random() %80; NSInteger ox = point.x - width/2; NSInteger oy = point.y - height/2; //对越界判断处理 if (ox < 10) { ox = 10; } else if (ox > self .view.bounds.size.width - 10 - width) { ox = self .view.bounds.size.width - 10 - width; } if (oy < 30) { oy = 30; } self .moveView.frame = CGRectMake(ox, oy, width, height); //添加浮动窗 CustomMenuItemView *view = [[CustomMenuItemView alloc] init]; [view showInView: self .view frame: self .moveView.frame]; } @end |
自定义浮动框视图类如下 CustomMenuItemView.h
1 2 3 4 5 6 7 | #import <UIKit/UIKit.h> @interface CustomMenuItemView : UIView - ( void )showInView:(UIView *)view frame:(CGRect)rect; @end |
CustomMenuItemView.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #import "CustomMenuItemView.h" @implementation CustomMenuItemView - ( void )showInView:(UIView *)view frame:(CGRect)rect { self .frame = view.bounds; [view addSubview: self ]; NSInteger jiantouSize = 10; NSInteger width = 250; NSInteger height = 120; UIView *bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; bgview.backgroundColor = [UIColor darkGrayColor]; bgview.layer.masksToBounds = YES ; bgview.layer.cornerRadius = 6; [ self addSubview:bgview]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, jiantouSize, jiantouSize)]; imageView.image = [UIImage imageNamed:@ "down" ]; [ self addSubview:imageView]; NSInteger ox = rect.origin.x + rect.size.width/2 - width/2; NSInteger oy = rect.origin.y - height/2 - jiantouSize - height/2; NSInteger jiantouCenterY = rect.origin.y - jiantouSize/2; if (oy <= 50) { imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI); oy = rect.origin.y + rect.size.height + jiantouSize; jiantouCenterY = rect.origin.y + rect.size.height + jiantouSize/2; } if (oy > view.bounds.size.height - jiantouSize - height - 10) { oy = view.bounds.size.height - height - 10; jiantouCenterY = view.bounds.size.height - jiantouSize/2 - height - 10; } if (ox <= 10) { ox = 10; } else if (ox >= view.bounds.size.width - 10 - width) { ox = view.bounds.size.width - 10 - width; } bgview.frame = CGRectMake(ox, oy, width, height); imageView.center = CGPointMake(rect.origin.x + rect.size.width/2, jiantouCenterY); } - ( void )touchesBegan:( NSSet <UITouch *> *)touches withEvent:(UIEvent *)event { self .hidden = YES ; self .removeFromSuperview; } @end |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!