新建视图文件内容
//
// TCView.h
// BottomPopViewDemo
//
// Created by yanjinlin on 2019/6/11.
// Copyright © 2019 yanjinlin. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum {
FormLeft = 0, //左边
FormRight, //右边
FormBottom, //底部
FormTop, //顶部
} formType;
@interface TCView : UIView
/**
初始化数据
@param frame 背景遮罩的大小
@param type 出来的方向
@param popviewHeight 需要弹出视图的高度(如果是左右的 为宽度)
@return 无
*/
-(instancetype)initWithFrame:(CGRect)frame formtype:(formType)type popviewHeight:(CGFloat)popviewHeight;
/**
显示视图
*/
-(void)show;
/**
关闭视图
*/
-(void)close;
/**
弹出方向记录
*/
@property (nonatomic, assign) formType type;
/**
需要弹出视图的高度(如果是左右的 为宽度)
*/
@property (nonatomic, assign) CGFloat popviewHeight;
@end
//
// TCView.m
// BottomPopViewDemo
//
// Created by yanjinlin on 2019/6/11.
// Copyright © 2019 yanjinlin. All rights reserved.
//
#import "TCView.h"
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_SCALE ((SCREEN_WIDTH > 414) ? (SCREEN_HEIGHT/375.0) : (SCREEN_WIDTH/375.0))
#define Handle(x) ((x)*SCREEN_SCALE)
#define UIColorFromHEXA(hex,a) [UIColor colorWithRed:((hex & 0xFF0000) >> 16) / 255.0f green:((hex & 0xFF00) >> 8) / 255.0f blue:(hex & 0xFF) / 255.0f alpha:a]
@interface TCView()
@property(nonatomic,strong) UIView * BottomView; //底部出来的
@property(nonatomic,strong) UIView * TopView; //上面出来
@property(nonatomic,strong) UIView * LeftView; //左边出来
@property(nonatomic,strong) UIView * RightView; //右边出来
@end
@implementation TCView
-(instancetype)initWithFrame:(CGRect)frame formtype:(formType)type popviewHeight:(CGFloat)popviewHeight{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = UIColorFromHEXA(0x000000, 0.5);
self.type = type;
self.popviewHeight = popviewHeight;
[self creatUI];
}
return self;
}
-(void)creatUI{
switch (self.type) {
case FormLeft:
{
UIView *BottomView = [[UIView alloc]initWithFrame:CGRectMake(-self.popviewHeight, 0, self.popviewHeight,SCREEN_HEIGHT)];
_LeftView = BottomView;
BottomView.backgroundColor = [UIColor whiteColor ];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:BottomView.bounds byRoundingCorners:UIRectCornerBottomRight | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = BottomView.bounds;
maskLayer.path = maskPath.CGPath;
BottomView.layer.mask = maskLayer;
}
break;
case FormRight:
{
UIView *BottomView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, self.popviewHeight,SCREEN_HEIGHT)];
_RightView = BottomView;
BottomView.backgroundColor = [UIColor whiteColor ];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:BottomView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = BottomView.bounds;
maskLayer.path = maskPath.CGPath;
BottomView.layer.mask = maskLayer;
}
break;
case FormBottom:
{
UIView *BottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, SCREEN_WIDTH,self.popviewHeight)];
_BottomView = BottomView;
BottomView.backgroundColor = [UIColor whiteColor];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:BottomView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = BottomView.bounds;
maskLayer.path = maskPath.CGPath;
BottomView.layer.mask = maskLayer;
UILabel *lab = [UILabel new];
lab.text = @"这是pop视图";
lab.textAlignment = NSTextAlignmentCenter;
lab.font = [UIFont systemFontOfSize:16];
lab.textColor = [UIColor redColor];
lab.frame = CGRectMake(0, 10, SCREEN_WIDTH, 20);
[_BottomView addSubview:lab];
}break;
case FormTop:
{
UIView *BottomView = [[UIView alloc]initWithFrame:CGRectMake(0, -self.popviewHeight, SCREEN_WIDTH,self.popviewHeight)];
_TopView = BottomView;
BottomView.backgroundColor = [UIColor whiteColor];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:BottomView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = BottomView.bounds;
maskLayer.path = maskPath.CGPath;
BottomView.layer.mask = maskLayer;
}
break;
default:
break;
}
}
//关闭
-(void)close{
[UIView animateWithDuration:0.15 animations:^{
self.BottomView.transform = CGAffineTransformIdentity;
self.LeftView.transform = CGAffineTransformIdentity;
self.RightView.transform = CGAffineTransformIdentity;
self.TopView.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
//展示
-(void)show{
switch (self.type) {
case FormLeft:
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self addSubview:self.LeftView];
[UIView animateWithDuration:0.15 animations:^{
self.LeftView.transform = CGAffineTransformMakeTranslation(self.popviewHeight,0);
} completion:^(BOOL finished) {
}];
}
break;
case FormRight:
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self addSubview:self.RightView];
[UIView animateWithDuration:0.15 animations:^{
self.RightView.transform = CGAffineTransformMakeTranslation(-self.popviewHeight,0);
} completion:^(BOOL finished) {
}];
}
break;
case FormBottom:
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self addSubview:self.BottomView];
[UIView animateWithDuration:0.15 animations:^{
self.BottomView.transform = CGAffineTransformMakeTranslation(0, -self.popviewHeight);
} completion:^(BOOL finished) {
}];
}break;
case FormTop:
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[self addSubview:self.TopView];
[UIView animateWithDuration:0.15 animations:^{
self.TopView.transform = CGAffineTransformMakeTranslation(0, self.popviewHeight);
} completion:^(BOOL finished) {
}];
}
break;
default:
break;
}
}
#pragma mark - 点击空白消失
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UITouch *touche = [touches anyObject];//获取触摸的东西
CGPoint point = [touche locationInView:self];//确定触摸的东西是自己
CGFloat height = self.BottomView.frame.size.height;
BOOL b = CGRectContainsPoint(CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-height), point);
// 该函数CGRectContainsPoint(CGRect rect, CGPoint point)是用于判断,参数2point是否包含在参数1rect中。
if (b)
{
[self close];
}
}
@end
调用方法 UIViewController
//
// ViewController.m
// BottomPopViewDemo
//
// Created by yanjinlin on 2019/6/11.
// Copyright © 2019 yanjinlin. All rights reserved.
//
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_SCALE ((SCREEN_WIDTH > 414) ? (SCREEN_HEIGHT/375.0) : (SCREEN_WIDTH/375.0))
#define Handle(x) ((x)*SCREEN_SCALE)
#import "ViewController.h"
#import "TCView.h"
@interface ViewController ()
@property (nonatomic, strong) TCView * tcview;//初始化
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_tcview = [[TCView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) formtype:FormBottom popviewHeight:Handle(350)];//底部
// _tcview = [[TCView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) formtype:FormLeft popviewHeight:Handle(200)];//左边
// _tcview = [[TCView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) formtype:FormRight popviewHeight:Handle(200)];//右边
// _tcview = [[TCView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) formtype:FormTop popviewHeight:Handle(350)];//顶部
}
#pragma mark - 点击空白
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_tcview show];
/*
UITouch *touche = [touches anyObject];//获取触摸的东西
CGPoint point = [touche locationInView:self.view];//确定触摸的东西是自己
CGFloat height = SCREEN_WIDTH/2;
BOOL b = CGRectContainsPoint(CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-height), point);
// 该函数CGRectContainsPoint(CGRect rect, CGPoint point)是用于判断,参数2point是否包含在参数1rect中。
if (b)
{
[_tcview show];
}else{
NSLog(@"点击下面啊");
}
*/
}
@end
底部效果图 其他控件可自行添加到相应的BottomView 中
底部弹出效果.png[百度云链接:https://pan.baidu.com/s/1WuSgoxFOi_4pzBmIMtGgBg 密码:aqsf]
网友评论