IOS 浮层视图 实现原理研究

视图浮层移动需要知道刚开始移动浮层视图的point,移动完成的point,手指离开浮层的point。

    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:_rootView];

获取到浮层视图根视图所对应的Point 

- (CGPoint)ConvertDir:(CGPoint)p {
    xh_ScreenChangeOrientation change2orien = [self screenChange];
    // covert
    switch (change2orien) {
        case xh_Change2Left:
            return [self LandscapeLeft:p];
            break;
        case xh_Change2Right:
            return [self LandscapeRight:p];
            break;
        case xh_Change2Upside:
            return [self UpsideDown:p];
            break;
        default:
        {
            return CGPointMake(p.x, p.y + TABBAR_HEIGHT*2);

        }
            break;
    }
}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    UITouch *touch = [touches anyObject];
    self.touchStartPosition = [touch locationInView:_rootView];
    if(IS_IPHONE) self.touchStartPosition = [self ConvertDir:_touchStartPosition];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:_rootView];
    if(IS_IPHONE) curPoint = [self ConvertDir:curPoint];
    self.superview.center = curPoint;
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint curPoint = [touch locationInView:_rootView];
    if(IS_IPHONE) curPoint = [self ConvertDir:curPoint];
    
    // if the start touch point is too close to the end point, take it as the click event and notify the click delegate
    if (pow((_touchStartPosition.x - curPoint.x),2) + pow((_touchStartPosition.y - curPoint.y),2) < 1) {
        [self.buttonDelegate dragButtonClicked:self];
        return;
    }
    [self buttonAutoAdjust:curPoint];
}

逆时针旋转分别是1 2 3 4象限

获取到程序旋转的角度 获得四条边的边距

    CGFloat left = curPoint.x;
    CGFloat right = IS_IPHONE ? (W - curPoint.x) : (xh_ScreenW - curPoint.x);
    CGFloat top = curPoint.y;
    CGFloat bottom = IS_IPHONE ? (H - curPoint.y) : (xh_ScreenH - curPoint.y);

判断当前触摸点的相对 左边 右边 顶部 底部 

 xh_FloatWindowDirection minDir = xh_FloatWindowLEFT;
    CGFloat minDistance = left;
    if (right < minDistance) {
        minDistance = right;
        minDir = xh_FloatWindowRIGHT;
    }
    if (top < minDistance) {
        minDistance = top;
        minDir = xh_FloatWindowTOP;
    }
    if (bottom < minDistance) {
        minDir = xh_FloatWindowBOTTOM;
    }

根据方向将当前视图父视图中心点进行更改

switch (minDir) {
        case xh_FloatWindowLEFT: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(self.superview.frame.size.width/2, self.superview.center.y);
            }];
            break;
        }
        case xh_FloatWindowRIGHT: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(W - self.superview.frame.size.width/2, self.superview.center.y);
            }];
            break;
        }
        case xh_FloatWindowTOP: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(self.superview.center.x, self.superview.frame.size.height/2);
            }];
            break;
        }
        case xh_FloatWindowBOTTOM: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(self.superview.center.x, H - self.superview.frame.size.height/2);
            }];
            break;
        }
        default:
            break;
    }

完整的代码如下:

-(void)buttonAutoAdjust:(CGPoint)curPoint {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    CGFloat W = xh_ScreenW;
    CGFloat H = xh_ScreenH;
    // (1,2->3,4 | 3,4->1,2)
    NSInteger judge = orientation + _initOrientation;
    if (IS_IPHONE && orientation != _initOrientation && judge != 3 && judge != 7) {
        W = xh_ScreenH;
        H = xh_ScreenW;
    }
    // distances to the four screen edges
    CGFloat left = curPoint.x;
    CGFloat right = IS_IPHONE ? (W - curPoint.x) : (xh_ScreenW - curPoint.x);
    CGFloat top = curPoint.y;
    CGFloat bottom = IS_IPHONE ? (H - curPoint.y) : (xh_ScreenH - curPoint.y);
    // find the direction to go
    xh_FloatWindowDirection minDir = xh_FloatWindowLEFT;
    CGFloat minDistance = left;
    if (right < minDistance) {
        minDistance = right;
        minDir = xh_FloatWindowRIGHT;
    }
    if (top < minDistance) {
        minDistance = top;
        minDir = xh_FloatWindowTOP;
    }
    if (bottom < minDistance) {
        minDir = xh_FloatWindowBOTTOM;
    }
    
    switch (minDir) {
        case xh_FloatWindowLEFT: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(self.superview.frame.size.width/2, self.superview.center.y);
            }];
            break;
        }
        case xh_FloatWindowRIGHT: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(W - self.superview.frame.size.width/2, self.superview.center.y);
            }];
            break;
        }
        case xh_FloatWindowTOP: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(self.superview.center.x, self.superview.frame.size.height/2);
            }];
            break;
        }
        case xh_FloatWindowBOTTOM: {
            [UIView animateWithDuration:0.3 animations:^{
                self.superview.center = CGPointMake(self.superview.center.x, H - self.superview.frame.size.height/2);
            }];
            break;
        }
        default:
            break;
    }
}

增加点击触发代理:

@protocol UIDragButtonDelegate <NSObject>

- (void)dragButtonClicked:(UIButton *)sender;

@end

 

这个浮层是在一个视图控制器上填充,该视图控制器添加到一个主视图控制器的字控制器中,为了可以弹进其他视图控制器

 [target addChildViewController:_floatVC];
    [target.view addSubview:_floatVC.view];

 

效果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值