1. 代码片段, 实现一个底部上滑抽屉,携带暗黑遮罩效果,
2. 主要在于函数
- (UIViewController *)appRootViewController
获取到rooView。
3. 定义几个常用的宏,注意类实现使用到的技巧,枚举的使用,协议代理其实也可以使用Block来进行毁掉,根据实际的经验,这种功能其实如果使用协议代理,会感觉更加优雅,
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]
#define RGBCOLOR_HEX(h) RGBCOLOR((((h)>>16)&0xFF), (((h)>>8)&0xFF), ((h)&0xFF))
#define RGBACOLOR_HEX(h,a) RGBACOLOR((((h)>>16)&0xFF), (((h)>>8)&0xFF), ((h)&0xFF), (a))
//屏幕高度
#define SCREENH [UIScreen mainScreen].bounds.size.height
/// 设计图纸尺寸比例
#define kRATIO (SCREENH/667.0)
=============。h文件
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, CNBottomViewStyle) {
CNBottomViewStyleNewsDetail,
CNBottomViewStyleShare,
CNBottomViewStyleOther,
};
typedef NS_ENUM(NSUInteger, CNBottomEvent) {
CNBottomEventShare,
CNBottomEventHeart,
CNBottomEventComment,
CNBottomEventFieldTouch,
CNBottomEventShareWhatsapp = 100,
CNBottomEventShareFacebook = 200,
CNBottomEventShareTwitter = 300,
CNBottomEventShareMore = 400,
CNBottomEventShareCancel = 500,
CNBottomEventNone,
};
@protocol CNBottomViewDelegate <NSObject>
@optional
- (BOOL)bottomFieldShouldBeginEditing;
- (void)bottomBtnClickEvent:(CNBottomEvent)event;
@end
@interface CNBottomView : UIView
@property (nonatomic, strong) UIImageView *imgvBackground;
@property (nonatomic, strong) NSString *commentCount;
@property (nonatomic, assign, readonly) CNBottomViewStyle style;
@property (nonatomic, assign)id<CNBottomViewDelegate>delegate;
@property (nonatomic, strong) UINavigationController *nav;
- (instancetype)initWithFrame:(CGRect)frame style:(CNBottomViewStyle)style;
- (instancetype)initWithStyle:(CNBottomViewStyle)style;
- (void)setHidden:(BOOL)hidden;
CGFloat heighOfInputBar();
CGFloat heighOfShareBar();
@end
===================。m文件
#import "CNBottomView.h"
@interface CNBottomView ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *field;
@property (nonatomic, strong) UIButton *btnCommi;
@property (nonatomic, strong) UILabel *labelCom;
@property (nonatomic, strong) UIButton *btnHeart;
@property (nonatomic, strong) UIButton *btnShare;
@property (nonatomic, strong) UIView *viewSharePlat;
@property (nonatomic, strong) UIView *viewWhatsapp;
@property (nonatomic, strong) UIView *viewFacebook;
@property (nonatomic, strong) UIView *viewTwitter;
@property (nonatomic, strong) UIView *viewMore;
@end
@implementation CNBottomView
CGFloat heighOfInputBar() {
return 45 * kRATIO;
}
CGFloat heighOfShareBar() {
return heighOfShareItemBar() + heighOfShareCancel();
}
CGFloat heighOfShareItemBar() {
return 95 * kRATIO;
}
CGFloat heighOfShareCancel(){
return 46 * kRATIO;
}
- (instancetype)initWithFrame:(CGRect)frame style:(CNBottomViewStyle)style {
self = [super initWithFrame:frame];
_style = style;
if (self) {
if (style == CNBottomViewStyleNewsDetail) {
//1. 背景图片
self.imgvBackground = [[UIImageView alloc] initWithFrame:self.bounds];
self.imgvBackground.backgroundColor = RGBACOLOR_HEX(0xF0F0F0, 1);
self.layer.shadowColor = RGBACOLOR_HEX(0xd3d3d3, 1).CGColor;
self.layer.shadowOffset = CGSizeMake(0, 0);
self.layer.shadowRadius = 1.5;
// self.layer.shadowOpacity= 0.8;
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,-1.5, SCREENW , 1.5)].CGPath;
[self addSubview:self.imgvBackground];
//2. 添加 输入框
CGFloat field_w = self.width - 2*(k_SpanLeft+k_SpanIn) - 50;//450
CGFloat field_h = self.height - 2*k_SpanIn;//56
_field = [[UITextField alloc] initWithFrame:CGRectMake(k_SpanLeft,0, field_w, field_h)];
_field.delegate = self;
_field.borderStyle = UITextBorderStyleRoundedRect;
_field.placeholder = @"我也说一句…";
_field.centerY = self.height * 0.5;
[self addSubview:_field];
// 3. 分享按钮,评论按钮,收藏按钮
CGFloat share_w = 30 * kRATIO;
CGFloat share_h = 34 * kRATIO;
CGFloat heart_w = 35 * kRATIO;
CGFloat heart_h = 31 * kRATIO;
CGFloat commi_w = 34 * kRATIO;
CGFloat commi_h = 35 * kRATIO;
_btnShare = [UIButton xk_buttonWithFrame:CGRectMake(0, 0, share_w, share_h) image:@"ic_share" target:self action:@selector(btnClick:)];
_btnHeart = [UIButton xk_buttonWithFrame:CGRectMake(0, 0, heart_w, heart_h) image:@"ic_like" target:self action:@selector(btnClick:)];
_btnCommi = [UIButton xk_buttonWithFrame:CGRectMake(0, 0, commi_w, commi_h) image:@"ic_coment" target:self action:@selector(btnClick:)];
_labelCom = [UILabel labelFrame:CGRectMake(0, 0, commi_w, commi_h) fontSize:13 * kRATIO textColor:RGBCOLOR_HEX(0xa6a6a5)];
_btnShare.right = self.width - k_SpanLeft/2;
_btnHeart.right = _btnShare.left - k_SpanIn;
_btnShare.centerY = _btnHeart.centerY = _btnCommi.centerY = self.height * 0.5;
[self addSubview:_btnCommi];
[self addSubview:_labelCom];
[self addSubview:_btnHeart];
[self addSubview:_btnShare];
}
}
return self;
}
#pragma mark - CNBottomViewStyleNewsDetail
- (void)btnClick:(UIButton *)btn {
CNBottomEvent event = CNBottomEventNone;
if (btn == _btnHeart) {
event = CNBottomEventHeart;
}else if (btn == _btnCommi) {
event = CNBottomEventComment;
}else if (btn == _btnShare) {
event = CNBottomEventShare;
}
else{
event = btn.tag;// facebook twitter whatsapp more
}
if ([self.delegate respondsToSelector:@selector(bottomBtnClickEvent:)]) {
[self.delegate bottomBtnClickEvent:event];
}
}
- (void)setCommentCount:(NSString *)commentCount {
_commentCount = commentCount;
_labelCom.text = commentCount;
[self reloadFrame];
}
- (void)reloadFrame {
[_labelCom sizeToFit];
_labelCom.right = _btnHeart.left - k_SpanIn;
_btnCommi.right = _labelCom.left - k_SpanIn/2;
_labelCom.centerY = self.height * 0.5;
_field.width = _btnCommi.left - 2 * k_SpanLeft;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if ([self.delegate respondsToSelector:@selector(bottomFieldShouldBeginEditing)]) {
return [self.delegate bottomFieldShouldBeginEditing];
}
return NO;
}
#pragma mark - CNBottomViewStyleShare
- (instancetype)initWithStyle:(CNBottomViewStyle)style {
self = [self initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
_style = style;
if (self) {
self.imgvBackground = [self backImageViewWithGesture];
[self addSubview:self.imgvBackground];
[self addSubview:self.viewSharePlat];
}
return self;
}
- (UIImageView *)backImageViewWithGesture {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
imageView.backgroundColor = [UIColor blackColor];
imageView.alpha = 0.4;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEvent)]];
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeEvent:)];
swipeGR.direction = UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;
[imageView addGestureRecognizer:swipeGR];
return imageView;
}
- (void)swipeEvent:(UISwipeGestureRecognizer *)swipeGR{
NSLog(@"%lu",swipeGR.direction);
[self setHidden:YES];
}
- (void)tapEvent {
[self setHidden:YES];
}
- (UIView *)viewSharePlat {
if (!_viewSharePlat) {
_viewSharePlat = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENH, SCREENW, heighOfShareBar())];
_viewSharePlat.backgroundColor = RGBACOLOR_HEX(0xF0F0F0, 1);
NSMutableArray *shareViewArr = [NSMutableArray arrayWithObjects:self.viewWhatsapp,self.viewFacebook,self.viewTwitter,self.viewMore, nil];
CGFloat span_w = (SCREENW - 80.0*(shareViewArr.count>4?4:shareViewArr.count) )/((shareViewArr.count > 4 ? 4:shareViewArr.count)+1);
for (int i=0; i<shareViewArr.count; i++) {
UIView *viewShare = shareViewArr[i];
viewShare.top = k_SpanIn + (viewShare.height + 1.5*k_SpanIn)*(i/4);
viewShare.centerX = span_w + viewShare.width*0.5 + (viewShare.width + span_w)*(i%4);
[_viewSharePlat addSubview:viewShare];
}
UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnCancel setFrame:CGRectMake(0,heighOfShareItemBar(),SCREENW, heighOfShareCancel())];
[btnCancel setTitle:@"Cancel" forState:UIControlStateNormal];
btnCancel.tag = CNBottomEventShareCancel;
[btnCancel addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[btnCancel setTitleColor: RGBCOLOR_HEX(0x575757) forState:UIControlStateNormal];
btnCancel.backgroundColor = [UIColor whiteColor];
[btnCancel.titleLabel setFont:[UIFont systemFontOfSize:18 * kRATIO]];
[_viewSharePlat addSubview:btnCancel];
}
return _viewSharePlat;
}
- (UIView *)viewWhatsapp {
if (!_viewWhatsapp) {
_viewWhatsapp = [self createShareItemWithImage:@"wahtsapp" title:@"Whatapp" tag:CNBottomEventShareWhatsapp];
}
return _viewWhatsapp;
}
- (UIView *)viewFacebook {
if (!_viewFacebook) {
_viewFacebook = [self createShareItemWithImage:@"Facebook" title:@"Facebook" tag:CNBottomEventShareFacebook];
}
return _viewFacebook;
}
- (UIView *)viewTwitter {
if (!_viewTwitter) {
_viewTwitter = [self createShareItemWithImage:@"twitter" title:@"Twitter" tag:CNBottomEventShareTwitter];
}
return _viewTwitter;
}
- (UIView *)viewMore {
if (!_viewMore) {
_viewMore = [self createShareItemWithImage:@"sharemore" title:@"More" tag:CNBottomEventShareMore];
}
return _viewMore;
}
- (UIView *)createShareItemWithImage:(NSString *)imgName title:(NSString *)title tag:(NSInteger)tag{
UIView *platView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
CGFloat btn_w = 50 * kRATIO;
CGFloat span_in = 5;
CGFloat label_h = 20;
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,0, btn_w, btn_w)];
btn.centerX = platView.width * 0.5;
// btn.backgroundColor = RGBCOLOR_HEX(0x514e5e);
btn.layer.cornerRadius = 7.0;
btn.tag = tag;
[btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[platView addSubview:btn];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,btn.origin.y+btn.size.height+span_in,platView.width,label_h)];
label.text = title;
label.textColor = RGBCOLOR_HEX(0x6b6b6b);
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13 * kRATIO];
[platView addSubview:label];
return platView;
}
-(void)setHidden:(BOOL)hidden{
weakSelf();
self.alpha = 1;
if (hidden) {
[UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
self.imgvBackground.alpha = 0;
if (_viewSharePlat) {
_viewSharePlat.center = CGPointMake(_viewSharePlat.centerX, weakSelf.height+_viewSharePlat.height);
}
} completion:^(BOOL finished) {
NSLog(@"hidden");
[weakSelf removeFromSuperview];
}];
}else{
UIViewController *topVC = [self appRootViewController];
[topVC.view addSubview:self];
// [self.nav.view addSubview:self];
[UIView animateWithDuration:0.2 delay:0.0 options:0 animations:^{
self.imgvBackground.alpha = 0.4;
if (_viewSharePlat) {
_viewSharePlat.center = CGPointMake(_viewSharePlat.centerX, weakSelf.height-_viewSharePlat.height*0.5);
}
} completion:^(BOOL finished) {
NSLog(@"nohidden");
}];
}
}
- (UIViewController *)appRootViewController
{
// UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
// return window.rootViewController;
#if 1
UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *topVC = appRootVC;
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
#endif
}