悬浮按钮 的实现

#import <UIKit/UIKit.h>

@interface QHFloatButton : UIButton

- (void)showInView:(UIView *)view;

@end
#import "QHFloatButton.h"

@interface QHFloatButton ()

@property (nonatomic, strong) UIView *baseView;

@end

@implementation QHFloatButton

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self setup];
    }
    
    return self;
}

- (void)setup{
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self addGestureRecognizer:panGesture];
}

- (void)showInView:(UIView *)view{
    self.baseView = view;
    //初始化悬浮按钮的frame和位置
    self.frame = CGRectMake(0, 0, 60.0, 60.0);
    self.center = CGPointMake(view.bounds.size.width, view.center.y);
    [view addSubview:self];
}

- (void)panAction:(UIPanGestureRecognizer *)pan{
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:{
            self.alpha = 1;
        }
            break;
        case UIGestureRecognizerStateChanged:{
            CGPoint point = [pan locationInView:self.baseView];
            
            self.center = point;
        }
            break;
        case UIGestureRecognizerStateEnded:{
            CGPoint point = [self getResultPoint];
            
            [UIView animateWithDuration:0.3 animations:^{
                self.alpha = 0.5;
                self.center = point;
            } completion:^(BOOL finished) {
                
            }];
        }
            break;
        default:{
            self.center = CGPointMake(self.baseView.bounds.size.width, self.baseView.center.y);
        }
            break;
    }
}

- (CGPoint)getResultPoint{
    CGRect baseFrame = self.baseView.frame;
    CGPoint resultPoint = CGPointZero;
    if(self.center.x > baseFrame.size.width / 2.0){
//        if(self.center.y > baseFrame.size.height / 2.0){
            //贴右边
            resultPoint = CGPointMake(baseFrame.size.width, self.center.y);
//        }else{
//            //贴顶部
//            resultPoint = CGPointMake(self.center.x, 0);
//        }
    }else{
//        if(self.center.y > baseFrame.size.height / 2.0){
            //贴左边
            resultPoint = CGPointMake(0, self.center.y);
//        }else{
//            //贴顶部
//            resultPoint = CGPointMake(self.center.x, 0);
//        }
    }
    
    return resultPoint;
}

@end

使用方法如下:

#pragma mark 悬浮按钮
- (void)showCaptionBtn{
    _floatBtn = [[QHFloatButton alloc]init];
    [_floatBtn setImage:IMAGENAMED(@"home_icon_captain") forState:UIControlStateNormal];
    [_floatBtn addTarget:self action:@selector(captainAction) forControlEvents:UIControlEventTouchUpInside];
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [_floatBtn showInView:app.window];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [_floatBtn setHidden:NO];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [_floatBtn setHidden:YES];
}

效果图

猜你喜欢

转载自my.oschina.net/u/2519763/blog/1787490
今日推荐