原文地址:http://blog.5ibc.net/p/86562.html

前提:当时看到别人写过这个类似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;
}

最新文章

  1. Unity3d热更新全书-加载(二)如何在不用AssetBundle的前提下动态加载预设
  2. 【初探Underscore】再说模版引擎
  3. Spring Data JPA 的配置文件 已经数据库的状态
  4. mongodb 释放磁盘空间
  5. word文档快速取消图片的链接
  6. poj2503 哈希
  7. lintcode :同构字符串
  8. JQuery Mobile 实战一
  9. POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
  10. USB Type-C,接口上的大统一?
  11. log4j.properties文件配置--官方文档
  12. sublime text snippet代码片断
  13. eclipse注释模板修改
  14. WordPress插件制作教程(三): 添加菜单的方法
  15. POJ 1798 Truck History
  16. 猫学习IOS(五岁以下儿童)UI之360其他下载管理器广场UI
  17. 棋盘覆盖(一) ACM
  18. html onclick时间传字符串参数
  19. Javascript 进阶 面向对象编程 继承的一个例子
  20. canvas-6font.html

热门文章

  1. KISS
  2. redis-3.28 一主二从模式介绍、主从从模式介绍、sentinel模式一主两从高可用
  3. Android 开发之 ---- bootloader (LK)
  4. Jpeglib读取jpg文件 【转】
  5. ubuntu 不是 识别 android 设备 解决方法
  6. Windows环境下Git配置及使用
  7. 读书笔记--MapReduce 适用场景 及 常见应用
  8. [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions
  9. Sublime Text 如何一个代码双屏显示代码上下部分?
  10. Python多线程问题的资料查找与汇总