博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
您的位置:首页 » IOS » iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch...
阅读量:4350 次
发布时间:2019-06-07

本文共 3761 字,大约阅读时间需要 12 分钟。

 

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

 

前提:当时看到别人写过这个类似AssistiveTouch的demo,但是有问题,第一改变不了位置、第二切换页面后无法使用、第三运行时偶尔会崩溃。然后自己就去度娘、论坛中都查了一些资料,然后结合起来写了这么一个demo。

思路:实现全局 需要在 AppDelegate.m 文件中 didFinishLaunchingWithOptions 方法里面实现
1、新建一个 继承于 UIWindow 的类 AssistiveTouch

//在 AssistiveTouch.h 文件中代码

#import 
@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;}

转载于:https://www.cnblogs.com/xiaoxiaoyublogs/p/5923602.html

你可能感兴趣的文章
使用Maven命令安装jar包到仓库中
查看>>
JavaScript 数据类型
查看>>
SQLite数据库
查看>>
[译]Vulkan教程(29)组合的Image采样器
查看>>
combox的DispalyMember和ValueMember属性的测试
查看>>
Start Developing Mac Apps -- Human Interface Design 用户界面设计
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>
hdu4348区间更新的主席树+标记永久化
查看>>
bzoj3261: 最大异或和 可持久化trie
查看>>
ZOJ 2532 Internship
查看>>
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>
图的遍历 | 1013 连通块块数
查看>>
Kinect 开发 —— 进阶指引(上)
查看>>
python学习笔记(六)time、datetime、hashlib模块
查看>>
uva489(需要考虑周全)
查看>>
C-关键字(二)
查看>>
排序笔记
查看>>
天气API整理,返回的数据格式为json对象
查看>>