前提:当时看到别人写过这个类似AssistiveTouch的demo,但是有问题,第一改变不了位置、第二切换页面后无法使用、第三运行时偶尔会崩溃。然后自己就去度娘、论坛中都查了一些资料,然后结合起来写了这么一个demo。
思路:实现全局 需要在 AppDelegate.m 文件中 didFinishLaunchingWithOptions 方法里面实现
1、新建一个 继承于 UIWindow 的类 AssistiveTouch

//在 AssistiveTouch.h 文件中代码

#import <UIKit/UIKit.h>@interface AssistiveTouch : UIWindow
{UIButton *_button;
}-(id) initWithFrame:(CGRect)frame;@end

//在 AssistiveTouch.m 文件中代码

#import "AssistiveTouch.h"@interface AssistiveTouch ()@end@implementation AssistiveTouch/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {// Drawing code
}
*/-(id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {self.backgroundColor = [UIColor clearColor];self.windowLevel = UIWindowLevelAlert + 1;
//这句话很重要[self makeKeyAndVisible];_button = [UIButton buttonWithType:UIButtonTypeCustom];_button.backgroundColor = [UIColor grayColor];_button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);_button.layer.cornerRadius = frame.size.width/2;[_button addTarget:self action:@selector(choose) forControlEvents:UIControlEventTouchUpInside];[self addSubview:_button];//放一个拖动手势,用来改变控件的位置UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changePostion:)];[_button addGestureRecognizer:pan];}return self;
}//按钮事件
-(void)choose
{NSLog(@"悬浮窗");
}
//手势事件 -- 改变位置
-(void)changePostion:(UIPanGestureRecognizer *)pan
{CGPoint point = [pan translationInView:self];CGFloat width = [UIScreen mainScreen].bounds.size.width;CGFloat height = [UIScreen mainScreen].bounds.size.height;CGRect originalFrame = self.frame;if (originalFrame.origin.x >= 0 && originalFrame.origin.x+originalFrame.size.width <= width) {originalFrame.origin.x += point.x;}if (originalFrame.origin.y >= 0 && originalFrame.origin.y+originalFrame.size.height <= height) {originalFrame.origin.y += point.y;}self.frame = originalFrame;[pan setTranslation:CGPointZero inView:self];if (pan.state == UIGestureRecognizerStateBegan) {_button.enabled = NO;}else if (pan.state == UIGestureRecognizerStateChanged){} else {CGRect frame = self.frame;//记录是否越界BOOL isOver = NO;if (frame.origin.x < 0) {frame.origin.x = 0;isOver = YES;} else if (frame.origin.x+frame.size.width > width) {frame.origin.x = width - frame.size.width;isOver = YES;}if (frame.origin.y < 0) {frame.origin.y = 0;isOver = YES;} else if (frame.origin.y+frame.size.height > height) {frame.origin.y = height - frame.size.height;isOver = YES;}if (isOver) {[UIView animateWithDuration:0.3 animations:^{self.frame = frame;}];}_button.enabled = YES;}
}@end
#import "AppDelegate.h"
#import "AssistiveTouch.h"@interface AppDelegate ()
{//悬浮框AssistiveTouch * _Win;
}
@end@implementation AppDelegate// 设置自定义悬浮框坐标
-(void)setNew
{_Win = [[AssistiveTouch alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// 这句话很重要,要先将rootview加载完成之后在显示悬浮框,如没有这句话,将可能造成程序崩溃[self performSelector:@selector(setNew) withObject:nil afterDelay:3];[self.window makeKeyAndVisible];return YES;
}

iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch相关推荐

  1. ios添加全局悬浮按钮_iOS开发悬浮按钮

    释放双眼,带上耳机,听听看~! #import "ViewController.h"@interface ViewController ()@property (weak, non ...

  2. android 中的悬浮按钮,Android 中FloatingActionButton(悬浮按钮)实例详解

    android 中floatingactionbutton(悬浮按钮)实例详解 一.介绍 这个类是继承自imageview的,所以对于这个控件我们可以使用imageview的所有属性 二.使用准备, ...

  3. Flutter全局悬浮按钮

    方法一 Offset _offset = Offset.zero;Scaffold(body: Stack(children: [_pageList[_currentIndex],Positioned ...

  4. Vivado Turtorial 01 —— 使用vivado中debug功能(类似ISE中ChipScope)

    这是转载大佬的,也可以移植到其他开发板上. Vivado Turtorial 01 -- 使用vivado中debug功能(类似ISE中ChipScope) 1.基于BASYS3板子,有如下代码: m ...

  5. iOS全局悬浮按钮+浮框

    效果图 库代码 Github地址 应用 悬浮按钮可以用于展示特定的信息 按钮有点击拖动等事件回调,可以自行扩展功能 示例中,浮框利用响应链框选View 可以拖动回调坐标,获取手势处View,进一步展示 ...

  6. android悬浮按钮功能实现,Android中实现悬浮按钮

    在有些APP中我们需要实现一个悬浮按钮,比如图片浏览应用左右翻页功能,比如左侧悬浮功能按钮.我们要实现此功能时,最开始想到的就是用FrameLayout来实现,但是如果把按钮简单的放到FrameLay ...

  7. (iOS开发) 自定义悬浮按钮

    前几天接到个需求:在项目中添加一个悬浮按钮,每个页面都显示,而且受到命令跳转界面的时候(受到socket命令然后跳转页面),这个悬浮按钮跳转出来的View要不消失.下面就是我的处理方法以及Demo: ...

  8. 【转】Mac系统中安装homebrew(类似redhat|Centos中的yum;类似Ubuntu中的apt-get)

    Homebrew,Homebrew简称brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,可以说Homebrew就是mac下的apt-get.yum神器 Homebr ...

  9. Android 全局悬浮按钮,悬浮按钮点击事件

    实现效果: 实现方法: 在自定义baseActivity里面添加viwe即可.在子activity里刷新悬浮View即可 public abstract class BaseActivity exte ...

最新文章

  1. mysql 上亿记录_一入职!就遇到上亿(MySQL)大表的优化....
  2. 一款强大而实用的图片去水印神器
  3. 构建一个业务连续的网络
  4. linux的命令行操作和shell的区别
  5. LeetCode-链表-24. 两两交换链表中的节点
  6. android ndk开发之Log日志(一)
  7. 有限元课堂笔记03:钢架(Frame)
  8. 13.函数式编程:匿名函数、高阶函数、装饰器
  9. 一文彻底搞懂字符串、字符串常量池原理
  10. WPF:使用WPF应用程序中的默认网络凭据和凭据存储来管理自动登录
  11. SUSE12SP3-Mycat(2)Schema.xml配置详解
  12. otsu阈值分割算法原理_OTSU阈值分割的实现.doc
  13. 【精讲版】上位机C#/.NET与西门子PLC通信
  14. select ajax加载数据,ajax 执行成功后返回j页面加载 joson 数据(加载select数据)
  15. DM DBLINK使用笔记
  16. 芝诺数解|「十三」一菜一格,百菜百味——重庆川菜数据分析报告
  17. 微信小程序实战--仿知识星球(一)
  18. 【红队】ATTCK - Active Scanning(主动扫描)
  19. arcgis怎么压缩tif文件_微信传文件有大小限制怎么办?教你3秒把100MPPT压缩成10M...
  20. idea默认编码设置

热门文章

  1. 视频文件大小计算方法(终极篇附实例)
  2. kali安装beef
  3. el-date-picker使用中遇到无法点击叉号清除,且不清除状态下无法重新选择时间的问题
  4. php 公众号内h5支付宝支付宝支付宝支付宝支付,微信浏览器中支付宝wap支付和微信JSAPI公众号支付...
  5. StackGAN论文解读
  6. webug靶场xss篇
  7. 免费下载《Nginx教程从入门到精通》.pdf
  8. 人工智能音乐算法的应用领域:从音频分析到音乐风格的探索
  9. 【pynq-z2】初始配置
  10. 小问题little little question