iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch (可以替换为视频悬浮窗口)
来源:互联网 发布:js下拉框联动 编辑:程序博客网 时间:2024/04/28 13:46
前提:当时看到别人写过这个类似AssistiveTouch的demo,但是有问题,第一改变不了位置、第二切换页面后无法使用、第三运行时偶尔会崩溃。然后自己就去度娘、论坛中都查了一些资料,然后结合起来写了这么一个demo。
思路:实现全局 需要在 AppDelegate.m 文件中 didFinishLaunchingWithOptions 方法里面实现
1、新建一个 继承于 UIWindow 的类 AssistiveTouch
//在 AssistiveTouch.h 文件中代码
#import <UIKit/UIKit.h>
@interface AssistiveView :UIWindow
{
UIButton *_button;
}
-(id) initWithFrame:(CGRect)frame;
@end
//在 AssistiveTouch.m 文件中代码
#import "AssistiveView.h"
@implementation AssistiveView
/*
// 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 = [superinitWithFrame:frame];
if (self) {
self.backgroundColor = [UIColorclearColor];
self.windowLevel =UIWindowLevelAlert + 1;
//这句话很重要
[selfmakeKeyAndVisible];
_button = [UIButtonbuttonWithType:UIButtonTypeCustom];
_button.backgroundColor = [UIColorgrayColor];
_button.frame =CGRectMake(0,0, frame.size.width, frame.size.height);
_button.layer.cornerRadius = frame.size.width/2;
[_buttonaddTarget:selfaction:@selector(choose)forControlEvents:UIControlEventTouchUpInside];
[selfaddSubview:_button];
//放一个拖动手势,用来改变控件的位置
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(changePostion:)];
[_buttonaddGestureRecognizer:pan];
}
returnself;
}
//按钮事件
-(void)choose
{
NSLog(@"悬浮窗");
}
//手势事件--改变位置
-(void)changePostion:(UIPanGestureRecognizer *)pan
{
CGPoint point = [pantranslationInView:self];
CGFloat width = [UIScreenmainScreen].bounds.size.width;
CGFloat height = [UIScreenmainScreen].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:CGPointZeroinView:self];
if (pan.state ==UIGestureRecognizerStateBegan) {
_button.enabled =NO;
}elseif (pan.state ==UIGestureRecognizerStateChanged){
} else {
CGRect frame =self.frame;
//记录是否越界
BOOL isOver =NO;
if (frame.origin.x <0) {
frame.origin.x =0;
isOver = YES;
} elseif (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;
} elseif (frame.origin.y+frame.size.height > height) {
frame.origin.y = height - frame.size.height;
isOver = YES;
}
if (isOver) {
[UIViewanimateWithDuration:0.3animations:^{
self.frame = frame;
}];
}
_button.enabled =YES;
}
}
#import "AppDelegate.h"
#import "AssistiveView.h"
@interfaceAppDelegate ()
{
//悬浮框
AssistiveView * _Win;
}
@end
@implementation AppDelegate
//设置自定义悬浮框坐标
-(void)setNew
{
_Win = [[AssistiveViewalloc] initWithFrame:CGRectMake(0,100, 60,60)];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 这句话很重要,要先将rootview加载完成之后在显示悬浮框,如没有这句话,将可能造成程序崩溃
[selfperformSelector:@selector(setNew)withObject:nilafterDelay:3];
[self.windowmakeKeyAndVisible];
returnYES;
}
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch (可以替换为视频悬浮窗口)
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch
- iOS自定义悬浮按钮,Objective-C可拖动的浮动按钮,iOS浮动图标,类似AssistiveTouch的浮动漂浮按钮
- 【iOS_GitHub】悬浮球/悬浮按钮/辅助按钮(类似于iOS系统自带的AssistiveTouch/京东/聚划算/等的辅助按钮)
- Android仿IOS AssistiveTouch(悬浮框的运用)
- 全局悬浮按钮(FAB)
- 仿iPhone的AssistiveTouch 悬浮辅助球工具
- 仿iPhone的AssistiveTouch 悬浮辅助球工具
- 类似迅雷的悬浮窗口
- iOS-悬浮按钮
- iOS 悬浮按钮
- Android中悬浮窗口
- Android中悬浮窗口
- Android 中悬浮窗口
- Android中悬浮窗口
- Android中悬浮窗口
- webpack2入门例子001
- dbn中的wake-sleep算法是如何调优的?
- java 类加载ClassLoad
- Python数据库安装
- javaScript商城实现购物车思路
- iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch (可以替换为视频悬浮窗口)
- EasyUI入门程序
- iOS 10 以后 Uitableview 与 gestureRecognizer手势冲突
- ios-swift-导入Alamofire出坑
- linux下的密码计算
- Qt 获取当前窗口背景色
- Video Analysis 相关领域解读之Temporal Action Detection(时序行为检测)
- unity 事件函数
- Pipeline