可吸附 悬浮按钮

可吸附悬浮

- (void)initXuanFuView{
    
    UIPanGestureRecognizer * panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(doMoveAction:)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageGestureClick:)];

    CGFloat bgViewWid = kSuspendBtnWidth + 20;

    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(susScreenW - bgViewWid, 75, bgViewWid,bgViewWid)];
    bgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:bgView];
    [bgView addGestureRecognizer:panGestureRecognizer];
    [bgView addGestureRecognizer:tap];

    UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_pufa_headrImg.png"]];
    imageV.userInteractionEnabled = YES;
    imageV.frame = CGRectMake((bgViewWid - kSuspendBtnWidth)/2.0, 0, kSuspendBtnWidth, kSuspendBtnWidth);
    //    imageV.backgroundColor = [UIColor cyanColor];
    [bgView addSubview:imageV];
    UILabel *tibLab= [[UILabel alloc]initWithFrame:CGRectMake(0, kSuspendBtnWidth + 5, bgViewWid, 15)];
    tibLab.text = @"巴拉巴拉";
    [tibLab setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]];

    //    tibLab.font = [UIFont systemFontOfSize:13.0];
    tibLab.textAlignment = NSTextAlignmentCenter;
    tibLab.textColor = [UIColor blueColor];
    [bgView addSubview:tibLab];
    self.imageVie  = bgView;

}
-(void)imageGestureClick:(UITapGestureRecognizer *)recognizer{
    NSLog(@"image_Tap");
}
-(void)doMoveAction:(UIPanGestureRecognizer *)recognizer{
    
    //移动状态
    UIGestureRecognizerState recState =  recognizer.state;
    
    switch (recState) {
        case UIGestureRecognizerStateBegan:
            
            break;
        case UIGestureRecognizerStateChanged:
        {
            CGPoint translation = [recognizer translationInView:self.navigationController.view];
            recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            CGPoint stopPoint = CGPointMake(0, susScreenH / 2.0);
            
            if (recognizer.view.center.x < susScreenW / 2.0) {
                if (recognizer.view.center.y <= susScreenH/2.0) {
                    //左上
                    if (recognizer.view.center.x  >= recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(kSuspendBtnWidth/2.0, recognizer.view.center.y);
                    }
                }else{
                    //左下
                    if (recognizer.view.center.x  >= susScreenH - recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, susScreenH - kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(kSuspendBtnWidth/2.0, recognizer.view.center.y);
                    }
                }
            }else{
                if (recognizer.view.center.y <= susScreenH/2.0) {
                    //右上
                    if (susScreenW - recognizer.view.center.x  >= recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(susScreenW - kSuspendBtnWidth/2.0, recognizer.view.center.y);
                    }
                }else{
                    //右下
                    if (susScreenW - recognizer.view.center.x  >= susScreenH - recognizer.view.center.y) {
                        stopPoint = CGPointMake(recognizer.view.center.x, susScreenH - kSuspendBtnWidth/2.0);
                    }else{
                        stopPoint = CGPointMake(susScreenW - kSuspendBtnWidth/2.0,recognizer.view.center.y);
                    }
                }
            }
            
            if (stopPoint.x - kSuspendBtnWidth/2.0 <= 0) {
                stopPoint = CGPointMake(kSuspendBtnWidth/2.0, stopPoint.y);
            }
            
            if (stopPoint.x + kSuspendBtnWidth/2.0 >= susScreenW) {
                stopPoint = CGPointMake(susScreenW - kSuspendBtnWidth/2.0, stopPoint.y);
            }
            
            if (stopPoint.y - kSuspendBtnWidth/2.0 <= 0) {
                stopPoint = CGPointMake(stopPoint.x, kSuspendBtnWidth/2.0);
            }
            
            if (stopPoint.y + kSuspendBtnWidth/2.0 >= susScreenH) {
                stopPoint = CGPointMake(stopPoint.x, susScreenH - kSuspendBtnWidth/2.0);
            }
            
            [UIView animateWithDuration:0.5 animations:^{
                recognizer.view.center = stopPoint;
            }];
        }
            break;
            
        default:
            break;
    }
    
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    
    
    return;
    CGPoint translation = [recognizer translationInView:self.view];
    CGPoint newCenter = CGPointMake(recognizer.view.center.x+ translation.x,
                                    recognizer.view.center.y + translation.y);
    //    限制屏幕范围:
    // 全屏
    newCenter.y = MAX(recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.y = MIN(self.view.frame.size.height - recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.view.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self.view];
    
    return;
    // 上半部分
    newCenter.y = MAX(recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.y = MIN(self.view.frame.size.height/2.0, newCenter.y);
    newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.view.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self.view];
    
}

你可能感兴趣的:(可吸附 悬浮按钮)