博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
全局手势按钮(随意拖动,点击事件)
阅读量:4351 次
发布时间:2019-06-07

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

连接: http://code.cocoachina.com/view/134248

创建一个对象继承UIWindow;

.m#import 
typedef void(^MCWindowTapBlock)();@interface MCAssistiveTouch : UIWindow/// 单例+ (instancetype)sharedInstance;/// 展示-(void)show;/// 隐藏-(void)dismiss;/// 按钮点击的回调@property(nonatomic,copy)MCWindowTapBlock windowTapBlock;@end

 

.h#import "MCAssistiveTouch.h"@interface MCAssistiveTouch (){    UIButton *_button;}@end@implementation MCAssistiveTouchstatic  MCAssistiveTouch *sharedInstance = nil ;+ (instancetype)sharedInstance{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        sharedInstance = [[self alloc] initWithFrame:CGRectMake(0, 0, 45, 45)];            });    return sharedInstance;}-(id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];        if (self) {        self.backgroundColor = [UIColor clearColor];        self.windowLevel = UIWindowLevelAlert + 1;        //这句话很重要        [self makeKeyAndVisible];        self.hidden = YES;                _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)show{    self.hidden = NO;}-(void)dismiss{    self.hidden = YES;    }//按钮事件-(void)choose{    if (self.windowTapBlock) {        self.windowTapBlock();    }}//手势事件 -- 改变位置-(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

 

使用:显示:[[MCAssistiveTouch sharedInstance] show];隐藏:[[MCAssistiveTouch sharedInstance] dismiss];添加点击事件:[MCAssistiveTouch sharedInstance].windowTapBlock = ^(){        NSLog(@"点击了手势按钮哦");    };

 

转载于:https://www.cnblogs.com/LzwBlog/p/6635239.html

你可能感兴趣的文章
HDU 3452 Bonsai
查看>>
[Erlang12] Mnesia分布式应用
查看>>
图的遍历 | 1013 连通块块数
查看>>
Kinect 开发 —— 进阶指引(上)
查看>>
python学习笔记(六)time、datetime、hashlib模块
查看>>
uva489(需要考虑周全)
查看>>
C-关键字(二)
查看>>
排序笔记
查看>>
天气API整理,返回的数据格式为json对象
查看>>
andorid 字体 修改
查看>>
oracle日期函数、字符串函数、格式化方式
查看>>
咏南APP(手机)开发框架
查看>>
第三周学习进度总结
查看>>
05:年龄与疾病
查看>>
POJ 1873
查看>>
POJ_1679_The Unique MST(次小生成树模板)
查看>>
JSP
查看>>
每天一个JavaScript实例-展示设置和获取CSS样式设置
查看>>
一篇文章教你如何用R进行数据挖掘
查看>>
MSDN--ASP.NET概述
查看>>