iOS开发项目篇—15菜单栏扩展

iOS开发项目篇—16菜单栏扩展

一、简单说明

在15中菜单栏的内在实现效果:

       

15中是通过Button来监听外部的点击,并做出响应。如果只是单纯的监听点击事件,去掉button,直接用View,给View添加一个手势识别器以监听。

二、在按钮的背后添加一个蒙版

  自定义类中增加一个BOOL型的属性

 1 //
 2 //  YYPopMenu.h
 3
 4 #import <UIKit/UIKit.h>
 5 @class YYPopMenu;
 6
 7 @protocol YYPopMenuDelegate <NSObject>
 8
 9 @optional
10 -(void)popMenuDidDismissed:(YYPopMenu *)popMenu;
11 @end
12
13
14 @interface YYPopMenu : UIView
15 /**
16  *  初始化方法
17  */
18 -(instancetype)initWithContentView:(UIView *)contentView;
19 +(instancetype)popMenuWithContentView:(UIView *)contentView;
20
21 /**
22  *  设置菜单的背景图片
23  */
24 -(void)setBackground:(UIImage *)background;
25 /**
26  * 显示菜单
27  */
28 -(void)showInRect:(CGRect)rect;
29
30 /**
31  *  关闭菜单
32  */
33 -(void)dismiss;
34
35 @property(nonatomic,strong)id<YYPopMenuDelegate> delegate;
36 @property(nonatomic,assign,getter = isdimBackground)BOOL dimBackground;
37 @end

在类的实现中重写set方法,设置蒙版

 1 #pragma mark-公共方法
 2 -(void)setDimBackground:(BOOL)dimBackground
 3 {
 4     _dimBackground=dimBackground;
 5     if (self.isdimBackground) {
 6         self.cover.backgroundColor=[UIColor blackColor];
 7         self.cover.alpha=0.3;
 8     }else
 9     {
10         self.cover.backgroundColor=[UIColor clearColor];
11         self.cover.alpha=1.0;
12     }
13 }

点击的时候在背后增加一层蒙版

1   [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
2
3         UITableView *tableView=[[UITableView alloc]init];
4         [tableView setBackgroundColor:[UIColor yellowColor]];
5         YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView];
6         [menu showInRect:CGRectMake(60, 55, 200, 200)];
7         menu.dimBackground=YES;
8         menu.delegate=self;

实现效果:

三、设置弹出菜单的箭头的位置为左、中、右

  自定义类的头文件

 1 //
 2 //  YYPopMenu.h
 3 //  08-微博弹出菜单
 4 //
 5
 6 #import <UIKit/UIKit.h>
 7 @class YYPopMenu;
 8 typedef enum {
 9     YYPopMenuArrowPositionCenter=0,
10     YYPopMenuArrowPositionLeft=1,
11     YYPopMenuArrowPositionRight=2,
12
13 }YYPopMenuArrowPosition;
14
15 @protocol YYPopMenuDelegate <NSObject>
16
17 @optional
18 -(void)popMenuDidDismissed:(YYPopMenu *)popMenu;
19 @end
20
21
22 @interface YYPopMenu : UIView
23 /**
24  *  初始化方法
25  */
26 -(instancetype)initWithContentView:(UIView *)contentView;
27 +(instancetype)popMenuWithContentView:(UIView *)contentView;
28
29 /**
30  *  设置菜单的背景图片
31  */
32 -(void)setBackground:(UIImage *)background;
33 /**
34  * 显示菜单
35  */
36 -(void)showInRect:(CGRect)rect;
37
38 /**
39  *  关闭菜单
40  */
41 -(void)dismiss;
42
43 @property(nonatomic,strong)id<YYPopMenuDelegate> delegate;
44 @property(nonatomic,assign,getter = isdimBackground)BOOL dimBackground;
45 @property(nonatomic,assign) YYPopMenuArrowPosition arrowPosition;
46 @end

在类的实现中,重写arrowPosition的set方法

  1 //
  2 //  YYPopMenu.m
  3 //  08-微博弹出菜单
  4 //
  5
  6 #import "YYPopMenu.h"
  7
  8 @interface YYPopMenu()
  9 @property(nonatomic,strong)UIView *contentView;
 10 /**
 11  *  最底部的遮盖 :屏蔽除菜单以外控件的事件
 12  */
 13 @property(nonatomic,strong)UIImageView *container;
 14 /**
 15  *  最底部的遮盖 :屏蔽除菜单以外控件的事件
 16  */
 17 @property(nonatomic,strong)UIButton *cover;
 18 @end
 19 @implementation YYPopMenu
 20
 21 #pragma mark-初始化方法
 22 //init方法会调用该方法
 23 - (id)initWithFrame:(CGRect)frame
 24 {
 25     self = [super initWithFrame:frame];
 26     if (self) {
 27         /**添加菜单内部的两个子控件*/
 28         //1.添加一个遮盖按钮
 29         UIButton *cover=[[UIButton alloc]init];
 30         //清除颜色
 31         cover.backgroundColor=[UIColor clearColor];
 32         [cover addTarget:self action:@selector(coverClick) forControlEvents:UIControlEventTouchUpInside];
 33         [self addSubview:cover];
 34         self.cover=cover;
 35
 36         //2.添加单箭头的菜单图片
 37         UIImageView *container=[[UIImageView alloc]init];
 38         //设置为可交互的
 39         container.userInteractionEnabled=YES;
 40         container.size=CGSizeMake(200, 100);
 41         container.image=[UIImage resizedImage:@"popover_background"];
 42         [self addSubview:container];
 43         self.container=container;
 44     }
 45     return self;
 46 }
 47
 48 -(instancetype)initWithContentView:(UIView *)contentView
 49 {
 50     if (self=[super init]) {
 51         self.contentView=contentView;
 52     }
 53     return self;
 54 }
 55
 56 +(instancetype)popMenuWithContentView:(UIView *)contentView
 57 {
 58     return [[self alloc]initWithContentView:contentView];
 59 }
 60
 61 -(void)layoutSubviews
 62 {
 63     [super layoutSubviews];
 64     self.cover.frame=self.bounds;
 65 }
 66 #pragma mark-内部方法
 67 -(void)coverClick
 68 {
 69     [self dismiss];
 70 }
 71
 72 #pragma mark-公共方法
 73 -(void)setDimBackground:(BOOL)dimBackground
 74 {
 75     _dimBackground=dimBackground;
 76     if (self.isdimBackground) {
 77         self.cover.backgroundColor=[UIColor blackColor];
 78         self.cover.alpha=0.3;
 79     }else
 80     {
 81         self.cover.backgroundColor=[UIColor clearColor];
 82         self.cover.alpha=1.0;
 83     }
 84 }
 85
 86 -(void)setArrowPosition:(YYPopMenuArrowPosition)arrowPosition
 87 {
 88     _arrowPosition=arrowPosition;
 89     switch (_arrowPosition) {
 90         case YYPopMenuArrowPositionCenter:
 91              self.container.image=[UIImage resizedImage:@"popover_background"];
 92             break;
 93
 94         case YYPopMenuArrowPositionLeft:
 95             self.container.image=[UIImage resizedImage:@"popover_background_left"];
 96             break;
 97
 98         case YYPopMenuArrowPositionRight:
 99             self.container.image=[UIImage resizedImage:@"popover_background_right"];
100             break;
101     }
102 }
103 -(void)setBackground:(UIImage *)background
104 {
105     self.container.image=background;
106 }
107
108 -(void)showInRect:(CGRect)rect
109 {
110     //添加菜单到整体的窗口上
111     UIWindow *window=[UIApplication sharedApplication].keyWindow;
112     self.frame=window.bounds;
113     [window addSubview:self];
114
115     //设置容器的frame
116     self.container.frame=rect;
117     [self.container addSubview:self.contentView];
118
119     // 设置容器里面内容的frame
120     CGFloat topMargin = 12;
121     CGFloat leftMargin = 5;
122     CGFloat rightMargin = 5;
123     CGFloat bottomMargin = 8;
124
125     self.contentView.y = topMargin;
126     self.contentView.x = leftMargin;
127     self.contentView.width = self.container.width - leftMargin - rightMargin;
128     self.contentView.height = self.container.height - topMargin - bottomMargin;
129
130 }
131
132 -(void)dismiss
133 {
134     //一旦调用了该方法,就通知代理删除菜单栏
135     if ([self.delegate respondsToSelector:@selector(popMenuDidDismissed:)]) {
136         [self.delegate popMenuDidDismissed:self];
137     }
138     [self removeFromSuperview];
139 }
140 @end

调用代码:

 1 -(void)titleButtonClick:(UIButton *)titleButton
 2 {
 3 //    UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"];
 4 //
 5 //    if (titleButton.currentImage==titleImage) {
 6         //换成箭头向上
 7         [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
 8
 9         UITableView *tableView=[[UITableView alloc]init];
10         [tableView setBackgroundColor:[UIColor yellowColor]];
11         YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView];
12         [menu showInRect:CGRectMake(60, 55, 200, 200)];
13         menu.dimBackground=YES;
14 //    menu.arrowPosition=YYPopMenuArrowPositionLeft;
15     menu.arrowPosition=YYPopMenuArrowPositionRight;
16         menu.delegate=self;
17
18 //    }else
19 //    {
20 //        //换成箭头向下
21 //        [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
22 //    }
23 }

实现效果:

箭头在左边和箭头在右边:

        

iOS开发项目篇—15菜单栏扩展

时间: 2024-08-05 23:40:59

iOS开发项目篇—15菜单栏扩展的相关文章

iOS开发项目篇—14点击标题按钮弹出菜单

iOS开发项目篇—14点击标题按钮弹出菜单 一.简单说明 (1)简单实现 点击标题按钮弹框,在箭头向上的时候,显示标题菜单 把ImageView添加到谁的身上?三角形的箭头在导航栏上,因此不能添加到tableview上. 分析图示: 有两个两种方法可以考虑: (1)添加到导航控制器上,因为导航栏是在导航控制器上的. (2)不用考虑控制器,直接添加到窗口上. 拿到窗口 (1)self.view.window这个属性谨慎使用,当开始加载的时候,为空 (2)获取主窗口  [UIApplication

iOS开发项目篇—28自定义UITextView

iOS开发项目篇—28自定义UITextView 一.简单说明 1.要实现的效果 2.分析 (1)UITextField 1.最多只能输入一行文字 2.能设置提醒文字(placehoder) 3.不具备滚动功能 (2)UITextView 1.能输入N行文字(N>0) 2.不能设置提醒文字(没有placehoder属性) 3.具备滚动功能 需求:技能输入多行文字,又具备文字提醒功能. 这里选择自定义一个类,让其继承自UITextView类,为其添加一个设置文字提醒的功能. 二.实现 自定义UI控

iOS开发项目篇—20存储账号信息

iOS开发项目篇—20存储账号信息 一.简单说明 1.不论请求是否成功,都在发送Post请求后,隐藏遮罩. 2.在授权成功后,切换根控制器. (1)说明 切换到控制器有几种情况,要么是切换到新特性,要么是切换到“首页”. 没有必要每次进入程序都需要登录,且每次返回的数据都是一样的,所以只需要把拿到的信息保存到沙盒里就可以了. 判断上次有没有登录成功(把拿到的access_token保存到沙盒中,如果沙盒中有access_token,说明上次登录成功),如果上次登陆成功,那么就判断是否要进入新特性

iOS开发项目篇—54&quot;设置&quot;界面的搭建

iOS开发项目篇—54"设置"界面的搭建 一.实现 新建一个设置控制器,当点击“我”控制器导航栏“设置”按钮时,即跳转到该界面 1.在“我”控制器中对导航栏“设置按钮”的处理 1 // 2 // YYProfileViewController.m 3 // 4 5 #import "YYProfileViewController.h" 6 #import "YYCommonGroup.h" 7 #import "YYCommonItem

iOS开发项目篇—36封装微博业务

iOS开发项目篇—36封装微博业务 一.简单说明 1.请求参数面向模型 2.请求结果面向模型 3.对控制器来说应该屏蔽业务细节.不让控制器关心(知道)业务细节,它只需要知道自己在做某个业务 @通过一个专门的业务处理类:处理微博业务细节 说明: 业务:加载新的微博首页数据 实现:给新浪服务器发送一个GET请求 业务:加载更多的首页微博数据 实现1:给新浪服务器发送一个GET请求 实现2:去沙盒中加载以前离线缓存的微博数据  二.实现 1.新建一个微博业务处理类,继承自NSObject 微博业务处理

iOS开发项目篇—34获取用户信息

iOS开发项目篇—34获取用户信息 一.简单说明 需求:获取当前用户的昵称 ,需要获取当前登录用户的个人信息. 查看接口 要求传递的参数 这里要获取的时用户的昵称(所以使用用户id作为参数传入) 二.实现代码 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 //设置导航栏内容 6 [self setupNavBar]; 7 8 //集成刷新控件 9 [self setupRefresh]; 10 11 //设置用户的昵称为标题 12 [s

iOS开发项目篇—29自定义工具条

iOS开发项目篇—29自定义工具条 一.简单说明 1.实现效果: 2.实现思路: (1)尝试: 1 //添加子控件 2 -(void)setupTextView 3 { 4 //1.创建输入控件 5 YYTextView *textView=[[YYTextView alloc]init]; 6 //设置frame 7 textView.frame=self.view.bounds; 8 [self.view addSubview:textView]; 9 self.textView=textV

iOS开发项目篇—25字典转模型第三方框架、运行时机制简介

iOS开发项目篇—25字典转模型第三方框架.运行时机制简介 一.使用第三方框架完成字典转模型 1.获取框架 在www.code4app.com网站中,搜索字典转模型 可以点击下载代码进行下载,也可以带github上去下载. 管理框架的好处:点击刷新按钮会刷新所有的项目. 2.使用 1.导入第三方框架 2.使用示例 错误提示: 在刷新的时候直接使用一行代码即可: 1 /**加载最新微博数据*/ 2 -(void)loadNewStatus 3 { 4 //1.获得请求管理者 5 AFHTTPReq

iOS开发项目篇—27自定义UITabBar

iOS开发项目篇—27自定义UITabBar 一.自定义 思路: (1)新建一个继承自UITabBar的类,自定义一个UITabBar (2)用自定义的UITabBar换掉系统的UItabBar(使用了KVC) (3)监听控制器的切换,只要控制器一切换,就调用代理方法强制重新布局子控件(内部会调用layoutSubviews). YYTabBar.m文件代码: 1 // 2 // YYTabBar.m 3 // 4 5 #import "YYTabBar.h" 6 7 @interfa