iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
前提:当时看到别人写过这个类似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相关推荐
- ios添加全局悬浮按钮_iOS开发悬浮按钮
释放双眼,带上耳机,听听看~! #import "ViewController.h"@interface ViewController ()@property (weak, non ...
- android 中的悬浮按钮,Android 中FloatingActionButton(悬浮按钮)实例详解
android 中floatingactionbutton(悬浮按钮)实例详解 一.介绍 这个类是继承自imageview的,所以对于这个控件我们可以使用imageview的所有属性 二.使用准备, ...
- Flutter全局悬浮按钮
方法一 Offset _offset = Offset.zero;Scaffold(body: Stack(children: [_pageList[_currentIndex],Positioned ...
- Vivado Turtorial 01 —— 使用vivado中debug功能(类似ISE中ChipScope)
这是转载大佬的,也可以移植到其他开发板上. Vivado Turtorial 01 -- 使用vivado中debug功能(类似ISE中ChipScope) 1.基于BASYS3板子,有如下代码: m ...
- iOS全局悬浮按钮+浮框
效果图 库代码 Github地址 应用 悬浮按钮可以用于展示特定的信息 按钮有点击拖动等事件回调,可以自行扩展功能 示例中,浮框利用响应链框选View 可以拖动回调坐标,获取手势处View,进一步展示 ...
- android悬浮按钮功能实现,Android中实现悬浮按钮
在有些APP中我们需要实现一个悬浮按钮,比如图片浏览应用左右翻页功能,比如左侧悬浮功能按钮.我们要实现此功能时,最开始想到的就是用FrameLayout来实现,但是如果把按钮简单的放到FrameLay ...
- (iOS开发) 自定义悬浮按钮
前几天接到个需求:在项目中添加一个悬浮按钮,每个页面都显示,而且受到命令跳转界面的时候(受到socket命令然后跳转页面),这个悬浮按钮跳转出来的View要不消失.下面就是我的处理方法以及Demo: ...
- 【转】Mac系统中安装homebrew(类似redhat|Centos中的yum;类似Ubuntu中的apt-get)
Homebrew,Homebrew简称brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,可以说Homebrew就是mac下的apt-get.yum神器 Homebr ...
- Android 全局悬浮按钮,悬浮按钮点击事件
实现效果: 实现方法: 在自定义baseActivity里面添加viwe即可.在子activity里刷新悬浮View即可 public abstract class BaseActivity exte ...
最新文章
- mysql 上亿记录_一入职!就遇到上亿(MySQL)大表的优化....
- 一款强大而实用的图片去水印神器
- 构建一个业务连续的网络
- linux的命令行操作和shell的区别
- LeetCode-链表-24. 两两交换链表中的节点
- android ndk开发之Log日志(一)
- 有限元课堂笔记03:钢架(Frame)
- 13.函数式编程:匿名函数、高阶函数、装饰器
- 一文彻底搞懂字符串、字符串常量池原理
- WPF:使用WPF应用程序中的默认网络凭据和凭据存储来管理自动登录
- SUSE12SP3-Mycat(2)Schema.xml配置详解
- otsu阈值分割算法原理_OTSU阈值分割的实现.doc
- 【精讲版】上位机C#/.NET与西门子PLC通信
- select ajax加载数据,ajax 执行成功后返回j页面加载 joson 数据(加载select数据)
- DM DBLINK使用笔记
- 芝诺数解|「十三」一菜一格,百菜百味——重庆川菜数据分析报告
- 微信小程序实战--仿知识星球(一)
- 【红队】ATTCK - Active Scanning(主动扫描)
- arcgis怎么压缩tif文件_微信传文件有大小限制怎么办?教你3秒把100MPPT压缩成10M...
- idea默认编码设置