仿QQ控件右上角按钮
2015-07-02 15:31
288 查看
首先封装一个view
.h文件
.m文件
控制器中的代码
点击按钮触发的方法
.h文件
#import <UIKit/UIKit.h> @interface menuView : UIView @property (nonatomic, strong)UIView *view; @end
.m文件
#import "menuView.h" @implementation menuView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.frame = CGRectMake(0, 0, BOUNDS.size.width, BOUNDS.size.height-64); // self.alpha = 0.4;//本身是遮罩层 再在遮罩层上面添加按钮 _view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, BOUNDS.size.width, BOUNDS.size.height)]; [self addSubview:_view]; _view.backgroundColor = [UIColor blackColor]; _view.alpha = 0.4; [self layOutUI]; } return self; } //添加四个按钮 -(void)layOutUI { CGFloat buttonW = BOUNDS.size.width/4; CGFloat buttonH = buttonW; for (int i = 0; i<4; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(i*buttonW, 0, buttonW, buttonH); NSString *name = [NSString stringWithFormat:@"第%i个按钮",i]; [button setBackgroundColor:[UIColor redColor]]; [button setTitle:name forState:UIControlStateNormal]; [self addSubview:button]; if (i<3) { UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((buttonW-1)+i*buttonW, 0, 1, buttonH)]; label.backgroundColor = [UIColor blackColor]; [self addSubview:label]; } } } @end
控制器中的代码
@interface MainViewController () { BOOL _show; menuView *_menu; } @property (nonatomic ,strong)UINavigationButton *rightBtn; - (void)viewDidLoad { [super viewDidLoad]; _rightBtn = [[MyNavigationButton alloc]initWithFrame:CGRectMake(0, 0, 25, 20)]; _rightBtn.imageStr = @"gouwuche"; [_rightBtn addTarget:self action:@selector(rightBarButtonItemAction:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:_rightBtn]; self.navigationItem.rightBarButtonItem = item; _show = NO; _menu = [[menuView alloc]init]; [self.view addSubview:_menu]; [_menu setHidden:YES]; }
点击按钮触发的方法
- (void)rightBarButtonItemAction:(UIButton *)sender { // CartViewController *cart = [CartViewController alloc]; // [[NSNotificationCenter defaultCenter] postNotificationName:@"HIDETABBAR" object:nil]; // [self.navigationController pushViewController:cart animated:YES]; //点击了按钮 且下拉菜单是显示状态 if (_show) {//_show 为yes时 隐藏 [_menu setHidden:YES]; [UIView animateWithDuration:0.5 animations:^{ _rightBtn.transform = CGAffineTransformMakeRotation(0); } completion:^(BOOL finished) { _show = NO; }]; } else {////_show 为no时 显示 [_menu setHidden:NO]; [UIView animateWithDuration:0.5 animations:^{ _rightBtn.transform = CGAffineTransformMakeRotation(M_PI); } completion:^(BOOL finished) { _show = YES; }]; } }
相关文章推荐
- Codeforces Round #311 (Div. 2) —— C
- Android:Layout_weight的理解
- 黑马day12 数据库之元数据
- ArcGIS教程:面积制表
- ios之UIScrollerView滚动视图
- 对于空字符串split()操作获取的数组的长度为1解释
- 欢迎使用CSDN-markdown编辑器
- shu_1241 邮局位置问题
- C#增删改查
- 且行且珍惜,我的极客导航
- 【PHP】session失效时间
- memcached安装
- JSP 页面中用绝对路径显示图片
- android GPS度分秒与double之间的相互转化
- Java 递归解决 "只能两数相乘的计算器计算x^y" 问题
- mongo导出导入
- 润乾填报表的时间参数设置
- android 动画资源
- 无法打开文件“atlsd.lib”
- ArduinoYun教程之Arduino环境与Linux环境的桥梁Bridge