{"id":18974685,"url":"https://github.com/pujiaxin33/jxwechatfloatview","last_synced_at":"2025-04-19T16:40:52.085Z","repository":{"id":98696980,"uuid":"136276828","full_name":"pujiaxin33/JXWeChatFloatView","owner":"pujiaxin33","description":"高仿微信文章悬浮球","archived":false,"fork":false,"pushed_at":"2018-06-06T05:48:18.000Z","size":20,"stargazers_count":111,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-16T11:05:24.057Z","etag":null,"topics":["float","wechat","weixin"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pujiaxin33.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-06-06T05:33:47.000Z","updated_at":"2025-04-14T09:14:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"f7cdcd82-2d83-4b17-ba6f-0c778469d910","html_url":"https://github.com/pujiaxin33/JXWeChatFloatView","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pujiaxin33%2FJXWeChatFloatView","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pujiaxin33%2FJXWeChatFloatView/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pujiaxin33%2FJXWeChatFloatView/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pujiaxin33%2FJXWeChatFloatView/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pujiaxin33","download_url":"https://codeload.github.com/pujiaxin33/JXWeChatFloatView/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249740040,"owners_count":21318674,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["float","wechat","weixin"],"created_at":"2024-11-08T15:15:49.627Z","updated_at":"2025-04-19T16:40:52.079Z","avatar_url":"https://github.com/pujiaxin33.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JXWeChatFloatView\n高仿微信文章悬浮球\n\n![头图.PNG](https://upload-images.jianshu.io/upload_images/1085173-bb3e00a5e84473d1.PNG?imageMogr2/auto-orient/strip%7CimageView2/2/w/375)\n\n\n## 前言\n微信在最新版本6.6.7，新加了一个文章悬浮球功能。当你正在阅读文章的时候，突然有好友发来了紧急消息，你需要立即回复。又或者你刚好路过小吃店，需要临时打开微信支付，等等临时中断阅读的情况。以前只有退出文章详情页面，处理完事情之后，再挨着挨着找到原来的文章。对于我们这种重度微信使用者来说，每次遭遇这种情况，真的很蛋疼。所以，当这个功能推出的事情，立马更新了最新版本，这个功能感觉就像遇到了知心人一样，用起来十分顺手。可以通过下面的动图感受一下\n![JXWeChatFloatView.gif](https://upload-images.jianshu.io/upload_images/1085173-57403dc5d867f949.gif?imageMogr2/auto-orient/strip)\n\n其实悬浮球的概念早就有了。比如360助手的流量监控球，iPhone自带的AssitiveTouch（就是那个可爱的小白球）等等。\n\n## 仓库地址\n[Github地址](https://github.com/pujiaxin33/JXWeChatFloatView)喜欢就点颗❤️\n\n## 核心技术点\n 体验过后，让人手痒痒，情不自禁得想要模仿一把。如果你的APP可以集成该功能，我觉得可以让你的APP逼格瞬间提升一个level。好了，下面让我们来一一解剖，微信文章悬浮球的核心技术点：\n### 1.悬浮球的出现\n当我们通过屏幕边缘手势pop视图的时候，右下角会有一个圆角提示图，跟着手势进度移动。\n**如何获取到`UIScreenEdgePanGestureRecognizer`的进度呢？**\n  因为系统自带的`interactivePopGestureRecognizer`是被封装起来的，它的action我们无法挂钩拿到里面的手势进度。所以，需要另辟蹊径了。\n- 首先，让UINavigationController的delegate等于自己，然后让多个手势可以同时响应。\n```swift\nself.interactivePopGestureRecognizer?.delegate = self\n\nfunc gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -\u003e Bool {\n        return true\n    }\n````\n- 然后自己添加一个`UIScreenEdgePanGestureRecognizer`到UINavigationController上面，用于获取pop手势的进度。\n```swift\nlet gesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(handleNavigationTransition(gesture:)))\ngesture.edges = .left\nself.view.addGestureRecognizer(gesture)\n```\n这样子，有两个`UIScreenEdgePanGestureRecognizer`可以同时响应，系统自带的依然保持原有逻辑不动，我们新增的用于获取pop手势进度，两者井水不犯河水，其乐融融。该技巧我的这篇文章也有使用[iOS：一分钟集成主流APP个人资料页（如简书、微博等）](https://www.jianshu.com/p/77fddf7f6b0e)\n\n### 2.悬浮球全局置顶\n既然悬浮球可以在悬浮在任何一个页面，必然是放在一个新的UIWindow上面。比如系统的键盘弹出的时候，就是一个`UIRemoteKeyboardWindow`在承载。\n然后这个window的生命周期不依赖某一个页面，所以用单例实现比较好。这块代码比较分散，直接看源码就可以了解\n\n### 3.事件响应\n\n- 悬浮UIWindow的事件传递\n只要事件位置没有在圆球和右下角上，就不响应\n```swift\noverride func point(inside point: CGPoint, with event: UIEvent?) -\u003e Bool {\n        let roundEntryViewPoint = self.convert(point, to: roundEntryView)\n        if roundEntryView.point(inside: roundEntryViewPoint, with: event) == true {\n            return true\n        }\n        let collectViewPoint = self.convert(point, to: collectView)\n        if collectView.point(inside: collectViewPoint, with: event) == true {\n            return true\n        }\n        return false\n    }\n```\n- 右下角四分之一圆，事件响应\n可以看到微信，只有当手指移动进右下角圆内，才能进行悬浮。而不是按着视图的frame来响应。\n首先，通过`UIBezierPath`画一个四分之一圆，然后用`CGPath`的`contains(point)`方法判断。\n```swift\nfunc updateBGLayerPath(isSmall: Bool) {\n        var ratio:CGFloat = 1\n        if !isSmall {\n            ratio = 1.3\n        }\n        let path = UIBezierPath()\n        path.move(to: CGPoint(x: viewSize.width, y: (1 - ratio)*viewSize.height))\n        path.addLine(to: CGPoint(x: viewSize.width, y: viewSize.height))\n        path.addLine(to: CGPoint(x: (1 - ratio)*viewSize.width, y: viewSize.height))\n        path.addArc(withCenter: CGPoint(x: viewSize.width, y: viewSize.height), radius: viewSize.width*ratio, startAngle: CGFloat(Double.pi), endAngle: CGFloat(Double.pi*3/2), clockwise: true)\n        path.close()\n        bgLayer.path = path.cgPath\n    }\n    override func point(inside point: CGPoint, with event: UIEvent?) -\u003e Bool {\n        return bgLayer.path!.contains(point)\n    }\n```\n对这块不太了解的同学，可以参考这篇文章。[一篇搞定事件传递、响应者链条、hitTest和pointInside的使用](https://www.jianshu.com/p/2f664e71c527)\n\n### 4.自定义转场动画\n可以看到点击悬浮球打开的文章，是通过一个自定义转场动画实现的，从悬浮球的位置开始展开。\n有许多文章都有讲解如何自定义转场动画，但是我推荐你看这篇文章[几句代码快速集成自定义转场效果+ 全手势驱动](https://www.jianshu.com/p/e498b956491c)\n\n## 实现效果\n![JXWeChatFloatView-2.gif](https://upload-images.jianshu.io/upload_images/1085173-7c404f4421258147.gif?imageMogr2/auto-orient/strip)\n\n## 总结\n微信的悬浮球，用到的技术点相对比较多，代码也比较分散，如果你的APP要集成该功能，需要认真封装代码。\n相对于如何实现，我认为如何设计好一个需求更重要。我在模仿的过程中，发现其中有许多细节的逻辑，彼此环环相扣，最终就呈现出了你正在使用的悬浮球功能。\n都说程序员和产品经理是相爱相杀，在这里我要为该功能的产品经理点个赞👍\n\n## 仓库地址\n如果代码中有任何问题，否则你有任何疑问，都可以反馈给我，我将第一时间处理。\n[Github地址](https://github.com/pujiaxin33/JXWeChatFloatView)喜欢就点颗❤️\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpujiaxin33%2Fjxwechatfloatview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpujiaxin33%2Fjxwechatfloatview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpujiaxin33%2Fjxwechatfloatview/lists"}