iOS中全局悬浮按钮,类似IPhone中的AssistiveTouch

//

//  AssistiveTouch.m

//  GraspCourse

//

//  Created by admin10 on 2019/7/16.

//  Copyright © 2019年 sun. All rights reserved.

//

 

#import "AssistiveTouch.h"

@interface AssistiveTouch ()

@end

@implementation AssistiveTouch

-(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];

        

        

        

        _buttonSe = [UIButton buttonWithType:UIButtonTypeCustom];

        _buttonSe.backgroundColor = [UIColor greenColor];

        _buttonSe.frame = CGRectMake(0, 0, 20, 20);

        

        [_buttonSe addTarget:self action:@selector(chooseSe) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:_buttonSe];

 

    }

    return self;

  

}

 

 

 

 

 

//按钮事件

-(void)choose

{

//    NSLog(@"悬浮窗");

    

    [[NSNotificationCenter defaultCenter]postNotificationName:@"SuspenWindow" object:nil];

   

}

 

 

-(void)chooseSe

{

    

    self.hidden = YES;

    

}

 

 

 

 

//手势事件 -- 改变位置

 

-(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;

}

如果工程有地方用到添加控件到主视图请用

    UIApplication * app = [UIApplication sharedApplication];

    [app.delegate.window addSubview:self];

不要用/UIWindow *rootWindow = [UIApplication sharedApplication]. keyWindow;

因为UIWindow *rootWindow = [UIApplication sharedApplication]. keyWindow 获得的视图是AssistiveTouch

 


--------------------- 
作者:iosWorker 
来源:CSDN 
原文:https://blog.csdn.net/iosWorker/article/details/50562297 
版权声明:本文为博主原创文章,转载请附上博文链接! /

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值