底滑抽屉(分享)
来源:互联网 发布:java的框架 编辑:程序博客网 时间:2024/05/01 12:09
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() {
return45 * kRATIO;
}
CGFloat heighOfShareBar() {
returnheighOfShareItemBar() + heighOfShareCancel();
}
CGFloat heighOfShareItemBar() {
return95 * kRATIO;
}
CGFloat heighOfShareCancel(){
return46 * kRATIO;
}
- (instancetype)initWithFrame:(CGRect)frame style:(CNBottomViewStyle)style {
self = [superinitWithFrame:frame];
_style = style;
if (self) {
if (style ==CNBottomViewStyleNewsDetail) {
//1. 背景图片
self.imgvBackground = [[UIImageViewalloc] 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 = [UIBezierPathbezierPathWithRect:CGRectMake(0,-1.5,SCREENW , 1.5)].CGPath;
[selfaddSubview: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 = [[UITextFieldalloc] initWithFrame:CGRectMake(k_SpanLeft,0, field_w, field_h)];
_field.delegate =self;
_field.borderStyle =UITextBorderStyleRoundedRect;
_field.placeholder =@"我也说一句…";
_field.centerY =self.height *0.5;
[selfaddSubview:_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 = [UIButtonxk_buttonWithFrame:CGRectMake(0,0, share_w, share_h) image:@"ic_share"target:selfaction:@selector(btnClick:)];
_btnHeart = [UIButtonxk_buttonWithFrame:CGRectMake(0,0, heart_w, heart_h) image:@"ic_like"target:selfaction:@selector(btnClick:)];
_btnCommi = [UIButtonxk_buttonWithFrame:CGRectMake(0,0, commi_w, commi_h) image:@"ic_coment"target:selfaction:@selector(btnClick:)];
_labelCom = [UILabellabelFrame: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;
[selfaddSubview:_btnCommi];
[selfaddSubview:_labelCom];
[selfaddSubview:_btnHeart];
[selfaddSubview:_btnShare];
}
}
returnself;
}
#pragma mark - CNBottomViewStyleNewsDetail
- (void)btnClick:(UIButton *)btn {
CNBottomEvent event =CNBottomEventNone;
if (btn ==_btnHeart) {
event = CNBottomEventHeart;
}elseif (btn == _btnCommi) {
event = CNBottomEventComment;
}elseif (btn == _btnShare) {
event = CNBottomEventShare;
}
else{
event = btn.tag;// facebook twitter whatsapp more
}
if ([self.delegaterespondsToSelector:@selector(bottomBtnClickEvent:)]) {
[self.delegatebottomBtnClickEvent:event];
}
}
- (void)setCommentCount:(NSString *)commentCount {
_commentCount = commentCount;
_labelCom.text = commentCount;
[selfreloadFrame];
}
- (void)reloadFrame {
[_labelComsizeToFit];
_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.delegaterespondsToSelector:@selector(bottomFieldShouldBeginEditing)]) {
return [self.delegatebottomFieldShouldBeginEditing];
}
returnNO;
}
#pragma mark - CNBottomViewStyleShare
- (instancetype)initWithStyle:(CNBottomViewStyle)style {
self = [selfinitWithFrame:CGRectMake(0,0, SCREENW,SCREENH)];
_style = style;
if (self) {
self.imgvBackground = [selfbackImageViewWithGesture];
[selfaddSubview:self.imgvBackground];
[selfaddSubview:self.viewSharePlat];
}
returnself;
}
- (UIImageView *)backImageViewWithGesture {
UIImageView *imageView = [[UIImageViewalloc] initWithFrame:self.bounds];
imageView.backgroundColor = [UIColorblackColor];
imageView.alpha =0.4;
imageView.userInteractionEnabled =YES;
[imageView addGestureRecognizer:[[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapEvent)]];
UISwipeGestureRecognizer *swipeGR = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipeEvent:)];
swipeGR.direction =UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;
[imageView addGestureRecognizer:swipeGR];
return imageView;
}
- (void)swipeEvent:(UISwipeGestureRecognizer *)swipeGR{
NSLog(@"%lu",swipeGR.direction);
[selfsetHidden:YES];
}
- (void)tapEvent {
[selfsetHidden:YES];
}
- (UIView *)viewSharePlat {
if (!_viewSharePlat) {
_viewSharePlat = [[UIViewalloc] initWithFrame:CGRectMake(0,SCREENH, SCREENW,heighOfShareBar())];
_viewSharePlat.backgroundColor =RGBACOLOR_HEX(0xF0F0F0,1);
NSMutableArray *shareViewArr = [NSMutableArrayarrayWithObjects: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);
[_viewSharePlataddSubview:viewShare];
}
UIButton *btnCancel = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btnCancel setFrame:CGRectMake(0,heighOfShareItemBar(),SCREENW,heighOfShareCancel())];
[btnCancel setTitle:@"Cancel"forState:UIControlStateNormal];
btnCancel.tag =CNBottomEventShareCancel;
[btnCancel addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[btnCancel setTitleColor:RGBCOLOR_HEX(0x575757)forState:UIControlStateNormal];
btnCancel.backgroundColor = [UIColorwhiteColor];
[btnCancel.titleLabelsetFont:[UIFontsystemFontOfSize:18 *kRATIO]];
[_viewSharePlataddSubview:btnCancel];
}
return_viewSharePlat;
}
- (UIView *)viewWhatsapp {
if (!_viewWhatsapp) {
_viewWhatsapp = [selfcreateShareItemWithImage:@"wahtsapp"title:@"Whatapp"tag:CNBottomEventShareWhatsapp];
}
return_viewWhatsapp;
}
- (UIView *)viewFacebook {
if (!_viewFacebook) {
_viewFacebook = [selfcreateShareItemWithImage:@"Facebook"title:@"Facebook"tag:CNBottomEventShareFacebook];
}
return_viewFacebook;
}
- (UIView *)viewTwitter {
if (!_viewTwitter) {
_viewTwitter = [selfcreateShareItemWithImage:@"twitter"title:@"Twitter"tag:CNBottomEventShareTwitter];
}
return_viewTwitter;
}
- (UIView *)viewMore {
if (!_viewMore) {
_viewMore = [selfcreateShareItemWithImage:@"sharemore"title:@"More"tag:CNBottomEventShareMore];
}
return_viewMore;
}
- (UIView *)createShareItemWithImage:(NSString *)imgName title:(NSString *)title tag:(NSInteger)tag{
UIView *platView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 80,80)];
CGFloat btn_w =50 * kRATIO;
CGFloat span_in =5;
CGFloat label_h =20;
UIButton *btn = [UIButtonbuttonWithType: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:[UIImageimageNamed:imgName] forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
[platView addSubview:btn];
UILabel *label = [[UILabelalloc] 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 = [UIFontsystemFontOfSize:13 *kRATIO];
[platView addSubview:label];
return platView;
}
-(void)setHidden:(BOOL)hidden{
weakSelf();
self.alpha =1;
if (hidden) {
[UIViewanimateWithDuration:0.2delay:0.0options:0animations:^{
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 = [selfappRootViewController];
[topVC.viewaddSubview:self];
// [self.nav.view addSubview:self];
[UIViewanimateWithDuration:0.2delay:0.0options:0animations:^{
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 = [UIApplicationsharedApplication].keyWindow.rootViewController;
UIViewController *topVC = appRootVC;
while (topVC.presentedViewController) {
topVC = topVC.presentedViewController;
}
return topVC;
#endif
}
- 底滑抽屉(分享)
- JavaScript 动画之抽屉(分享到)
- Android:Navigationdrawer导航抽屉功能的实现(源代码分享)
- 创建导航抽屉(侧滑菜单)
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- Android开发之Navigationdrawer导航抽屉功能的实现(源代码分享)
- MaterialDrawer+ToolBar飘逸的导航和抽屉侧滑(用户登录信息的抽屉布局)
- android SlidingDrawer(抽屉)
- 抽屉菜单(DrawerLayout)
- 抽屉效果(DrawLayout)
- 抽屉
- 抽屉
- 抽屉。
- 抽屉
- Axure RP 模拟抽屉导航效果(侧滑菜单)
- Android: Drawer Layout(抽屉)侧滑页面
- 小鸡腿U T8 CERC2015
- [CF]Ilya and Queries
- hrbust 1759 Lawnmower【思维题】
- php.ini 中配置项的设置
- lightoj 1138 (二分)
- 底滑抽屉(分享)
- JNI常用知识点总结
- HDU 4411 费用流,诡异的构图
- 373. Find K Pairs with Smallest Sums
- hdu 1023
- 自动化渗透测试工具包APT2
- POJ 1026 Cipher(置换群)循环节
- SpringMVC框架笔记整理(一): SpringMVC详细运行流程
- 《数据结构与算法》学习笔记20 递归_归并排序