• 全局手势按钮(随意拖动,点击事件)


    连接: 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(@"点击了手势按钮哦");
        };
  • 相关阅读:
    函数后面加delete和default
    passing as 'this' argument discards qualifier
    QString和std::string互相转换
    ERROR in config: Command requires a target workspace.错误解决方案
    论文阅读:Precise and realistic grasping and manipulation in Virtual Reality without force feedback
    unreal 安卓打包出现 LogHttp: Warning: 000001D09B0C8F00: request failed, libcurl error 哇塞 真的是爆炸 不知道为啥巨慢
    UE4-打包安卓报错 /Android/APKgradle ungradle.bat" :app:assembleDebug
    oculus quest2与ue开发教程
    Understand Oculus Integration Package Components
    HandsInteractionTrainScene Sample Scene
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/6635239.html
Copyright © 2020-2023  润新知