iOS常用

iOS 悬浮框

2021-04-07  本文已影响0人  邓布利多教授

悬浮按钮,先上图

Kapture 2021-04-07 at 14.01.48.gif

很长时间没写了,手法生疏,直接上代码吧

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SheetControlView : UIWindow

/// 展示
+ (void)show;

/// 隐藏
+ (void)hidden;

@end

NS_ASSUME_NONNULL_END
#import "SheetControlView.h"

static SheetControlView *mainWindow;
static CGFloat widthWindow  = 120.f;
static CGFloat bottomWindow = 160.f;
static CGFloat heightButton = 40.f;
static CGFloat durationTime = 0.35;

typedef NS_ENUM(NSUInteger, SheetButtonShowType) {
    SheetButtonShowHalfType,// 展示一半
    SheetButtonShowFullType,// 展示全部
};

@interface SheetControlView()

/// 展示类型
@property(nonatomic, assign) SheetButtonShowType showType;

/// 悬浮按钮
@property (nonatomic, strong) UIButton *sheetButton;

@end

@implementation SheetControlView

+ (void)show {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mainWindow = [[SheetControlView alloc] initWithFrame:CGRectZero];
        mainWindow.rootViewController = [UIViewController new];
        mainWindow.showType = SheetButtonShowHalfType;
        mainWindow.sheetButton.hidden = NO;
    });
    [mainWindow setupWindow];
}

- (void)setupWindow {
    
    if (mainWindow.hidden) {
        mainWindow.hidden = NO;
        [UIView animateWithDuration:durationTime animations:^{
            CGRect frame = mainWindow.frame;
            frame.origin.x = [UIScreen mainScreen].bounds.size.width - widthWindow;
            mainWindow.frame = frame;
        }];
    }else if (!mainWindow) {
        mainWindow = [[SheetControlView alloc] initWithFrame:CGRectZero];
        mainWindow.rootViewController = [UIViewController new];
    }
    mainWindow.backgroundColor = UIColor.clearColor;
    mainWindow.windowLevel = MAXFLOAT;
    [mainWindow makeKeyAndVisible];
    
}

+ (void)hidden {
    if (mainWindow) {
        [UIView animateWithDuration:durationTime animations:^{
            CGRect frame = mainWindow.frame;
            frame.origin.x = [UIScreen mainScreen].bounds.size.width;
            mainWindow.frame = frame;
        } completion:^(BOOL finished) {
            mainWindow.hidden = YES;
        }];
    }
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        CGFloat xWindow = [UIScreen mainScreen].bounds.size.width;
        CGFloat yWindow = [UIScreen mainScreen].bounds.size.height - bottomWindow;
        frame = CGRectMake(xWindow, yWindow, widthWindow, heightButton);
        self.frame = frame;
    }
    return self;
    
}

+ (void)fullAnimation {
    mainWindow.showType = SheetButtonShowFullType;
    [UIView animateWithDuration:durationTime animations:^{
        CGRect frame = mainWindow.frame;
        frame.origin.x = [UIScreen mainScreen].bounds.size.width - widthWindow;
        mainWindow.frame = frame;
    }];
}

+ (void)halfAnimation {
    mainWindow.showType = SheetButtonShowHalfType;
    [UIView animateWithDuration:durationTime animations:^{
        CGRect frame = mainWindow.frame;
        frame.origin.x = [UIScreen mainScreen].bounds.size.width - (widthWindow / 2);
        mainWindow.frame = frame;
    }];
}

#pragma mark - 按钮懒加载
- (UIButton *)sheetButton {
    if (!_sheetButton) {
        _sheetButton = [[UIButton alloc] initWithFrame:mainWindow.rootViewController.view.bounds];
        [_sheetButton setTitle:@"查看新消息" forState:0];
        [_sheetButton setTitleColor:UIColor.whiteColor forState:0];
        _sheetButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _sheetButton.backgroundColor = UIColor.orangeColor;
        [_sheetButton addTarget:mainWindow action:@selector(buttonClickAction) forControlEvents:UIControlEventTouchUpInside];
        _sheetButton.center = mainWindow.rootViewController.view.center;
        [mainWindow.rootViewController.view addSubview:_sheetButton];// 添加到window上
        
        /// 添加圆角
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:_sheetButton.bounds
                                                   byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft
                                                         cornerRadii:CGSizeMake(heightButton / 2, heightButton / 2)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = _sheetButton.bounds;
        maskLayer.path = path.CGPath;
        _sheetButton.layer.mask = maskLayer;
    }
    return _sheetButton;
}

- (void)buttonClickAction {
    switch (self.showType) {
        case SheetButtonShowHalfType:
            [SheetControlView fullAnimation];// 展示全部
            break;
        case SheetButtonShowFullType:
        {
            NSLog(@"点击按钮");
        }
            break;
        default:
            break;
    }
}

/// 点击其他位置收回浮窗
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    if (CGRectContainsPoint(self.bounds, point)) {
        return YES;
    }else {
        [SheetControlView halfAnimation];
        return NO;
    }
}

@end
#import "ViewController.h"
#import "SheetControlView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.groupTableViewBackgroundColor;
    
    UIButton *showButton = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 160) / 2, 120, 160, 40)];
    [showButton setTitle:@"展示" forState:0];
    showButton.backgroundColor = UIColor.redColor;
    showButton.tag = 0;
    [showButton addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:showButton];
    
    UIButton *hiddenButton = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 160) / 2, 180, 160, 40)];
    [hiddenButton setTitle:@"隐藏" forState:0];
    hiddenButton.backgroundColor = UIColor.orangeColor;
    hiddenButton.tag = 1;
    [hiddenButton addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:hiddenButton];
    
}

- (void)buttonSelect:(UIButton *)sender {
    
    if (sender.tag == 0) {
        [SheetControlView show];
    }else {
        [SheetControlView hidden];
    }
    
}

@end

全剧终

上一篇下一篇

猜你喜欢

热点阅读