iOS 自由拖动的滑块
完全自由拖动的滑块
代码实现:
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | @implementation FloatView #pragma mark - lazyload - (UIImageView *)imageView { if (_imageView == nil ) { _imageView = [[UIImageView alloc]init]; _imageView.image = [UIImage imageNamed:@ "telephone" ]; _imageView.frame = self .bounds; } return _imageView; } - ( void )touchesBegan:( NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; //CGPoint pt = [touch locationInView:self.superview]; CGRect frame = self .frame; //在自身视图中的位置 CGPoint pt = [touch locationInView: self ]; CGPoint anchorPoint = CGPointMake(pt.x/ self .bounds.size.width, pt.y/ self .bounds.size.height); //这是设置的是position也就是center的位置百分比 //也就是鼠标所在的位置 self .layer.anchorPoint = anchorPoint; self .frame = frame; } - ( void )touchesMoved:( NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint pt = [touch locationInView: self .superview]; self .layer.position = pt; } - ( void )touchesEnded:( NSSet *)touches withEvent:(UIEvent *)event { [ self limitRangeOfView]; } - ( void )touchesCancelled:( NSSet <UITouch *> *)touches withEvent:(UIEvent *)event { [ self limitRangeOfView]; } - ( void )limitRangeOfView { [UIView animateWithDuration:0.3 animations:^{ CGFloat padding = 1; CGRect frame = self .frame; if (frame.origin.x < padding) { frame.origin.x = padding; } if (frame.origin.y < padding) { frame.origin.y = padding; } if (frame.origin.x > (kScreenWidth-frame.size.height-padding)) { frame.origin.x = kScreenWidth-frame.size.width-padding; } if (frame.origin.y > (kScreenHeight-frame.size.height-padding)) { frame.origin.y = kScreenHeight-frame.size.height-padding; } self .frame = frame; }]; } @end |
github 地址:floatViewExample
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程