zoukankan      html  css  js  c++  java
  • 全局手势按钮(随意拖动,点击事件)

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

    创建一个对象继承UIWindow;

    .m
    
    #import <UIKit/UIKit.h>
    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 MCAssistiveTouch
    
    static  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(@"点击了手势按钮哦");
        };
  • 相关阅读:
    关于使用modelsim的一点感想
    matlab与modelsim中的文件操作函数
    几种测距定位方法
    关于数字信号处理中的一些概念的理解
    转载 关于case语句的优先级
    转载 关键路径优化实例
    转载 寄存器平衡实例
    第八篇:并发回射服务器的最基本实现思路
    第七篇:几个经典的TCP通信函数
    第六章:表达格式和数值格式的转换
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/6635239.html
Copyright © 2011-2022 走看看