IOS 仿AitiveTouch(小白点)虚拟按钮

代码比较简单,废话不多说,直接贴代码!!有不正确的地方还望指出。

因为个人比较懒,仅贴部分代码。具体demo请在文章结尾处下载。

//初始化按钮,初始位置
- (id)initInKeyWindowWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    self.percentX = frame.origin.x / (SCREEN_WIDTH - frame.size.width);
    self.percentY = frame.origin.y / (SCREEN_HEIGHT - frame.size.height);
    if (self) {
        [self performSelector:@selector(addButtonToKeyWindow) withObject:nil afterDelay:0.f];
        [self defaultSetting];
    }
    return self;
}

//设置圆角等、注册通知
- (void)defaultSetting {
    [self.layer setCornerRadius:self.frame.size.height / 2];
    [self.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.layer setBorderWidth:0.5];
    [self.layer setMasksToBounds:YES];
    
    [self registerKeyboardNotification];
}

//移除通知
- (void)dealloc
{
    [self removeKeyboardNotification];
}
//获取KeyWindow或者最上层Window
- (void)addButtonToKeyWindow {
    UIWindow *mainWindow = nil;
    mainWindow = [UIApplication sharedApplication].keyWindow;
    if (mainWindow == nil) {
        NSString *textWin = @" 0);
            BOOL windowIsClass = (![[window description] hasPrefix:textWin]);
            BOOL windowOnMainScreen = (window.screen == UIScreen.mainScreen);
            if (windowOnMainScreen && windowIsVisible && windowIsClass) {
                mainWindow = window;
            }
            
            BOOL windowIsKeyType = [window isKeyWindow];
            if (windowOnMainScreen && windowIsVisible && windowIsKeyType) {
                mainWindow = window;
                break;
            }
        }
    }
    [mainWindow addSubview:self];
}
//触摸屏幕时相关处理
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    self.isDragging = NO;
    self.hadDragDone = NO;
    [super touchesBegan:touches withEvent:event];
    self.beginLocation = [[touches anyObject] locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    self.isDragging = YES;
    
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];
    
    float offsetX = currentLocation.x - self.beginLocation.x;
    float offsetY = currentLocation.y - self.beginLocation.y;
    self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
    
    CGRect superviewFrame = self.superview.frame;
    CGRect frame = self.frame;
    CGFloat keyboardHeight = self.visibleKeyboardHeight;
    CGFloat leftLimitX = frame.size.width / 2;
    CGFloat rightLimitX = superviewFrame.size.width - leftLimitX;
    CGFloat topLimitY = frame.size.height / 2;
    CGFloat bottomLimitY = superviewFrame.size.height - topLimitY - keyboardHeight;
    
    if (self.center.x > rightLimitX) {
        self.center = CGPointMake(rightLimitX, self.center.y);
    }else if (self.center.x <= leftLimitX) {
        self.center = CGPointMake(leftLimitX, self.center.y);
    }
    
    if (self.center.y > bottomLimitY) {
        self.center = CGPointMake(self.center.x, bottomLimitY);
    }else if (self.center.y <= topLimitY){
        self.center = CGPointMake(self.center.x, topLimitY);
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded: touches withEvent: event];
    
    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self];
    float offsetX = currentLocation.x - self.beginLocation.x;
    float offsetY = currentLocation.y - self.beginLocation.y;
    if (offsetX != 0 || offsetY != 0) self.hadDragDone = YES;
    
    if (self.isDragging) {
        NSDictionary *dic = [self getBtnPosition:YES];
        CGFloat centerx = [dic[@"centerx"] floatValue];
        CGFloat centery = [dic[@"centery"] floatValue];
        
        [UIView animateWithDuration:AUTODOCKING_ANIMATE_DURATION animations:^{
            self.center = CGPointMake(centerx, centery);
        } completion:nil];
        
        CGRect superviewFrame = self.superview.frame;
        self.percentX = self.frame.origin.x / (superviewFrame.size.width - self.frame.size.width);
        self.percentY = self.frame.origin.y / (superviewFrame.size.height - self.frame.size.height);
    }
    
    self.isDragging = NO;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.isDragging = NO;
    [super touchesCancelled:touches withEvent:event];
}
//横竖屏切换及键盘抬起等处理
- (void)deviceOrientationWillChange:(NSNotification *)notification
{
    if ([self.superview isKindOfClass:[UIWindow class]])
    {
        [UIView animateWithDuration:0.1 animations:^{
            self.alpha = 0.0f;
        } completion:nil];
    }
}

- (void)deviceOrientationDidChange:(NSNotification *)notification
{
    if ([self.superview isKindOfClass:[UIWindow class]])
    {
        [self setTransformForCurrentOrientation:notification];
    }
}


- (void)keyBoardWillShow:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *dic = [self getKeyBoardHeight:notification];
    CGFloat keyboardHeight = [dic[@"keyHeight"] floatValue];
    double animationDuration = [dic[@"animationDuration"] doubleValue];
    
    CGRect superviewFrame = self.superview.frame;
    
    CGFloat bottom = CGRectGetMaxY(self.frame);
    if (bottom < (superviewFrame.size.height-keyboardHeight)) {
        return;
    }
    
    [UIView animateWithDuration:animationDuration animations:^{
        CGRect rect = self.frame;
        rect.origin.y = superviewFrame.size.height - keyboardHeight - rect.size.height;
        self.frame = rect;
    }];
}

- (void)keyBoardWillHide:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *dic = [self getKeyBoardHeight:notification];
    double animationDuration = [dic[@"animationDuration"] doubleValue];
    
    CGRect superviewFrame = self.superview.frame;
    CGFloat x = self.percentX * (superviewFrame.size.width - self.frame.size.width);
    CGFloat y = self.percentY * (superviewFrame.size.height - self.frame.size.height);
    self.frame = CGRectMake(x, y, self.bounds.size.width, self.bounds.size.height);
    
    [UIView animateWithDuration:animationDuration animations:^{
        self.frame = CGRectMake(x, y, self.bounds.size.width, self.bounds.size.height);
    }];
}

有不正确的地方,还望指出,觉得不错,还请打赏!!
demo地址:https://github.com/DaXiaGuiHai/MyRepository

你可能感兴趣的:(IOS 仿AitiveTouch(小白点)虚拟按钮)