您的位置:首页 > 其它

仿QQ控件右上角按钮

2015-07-02 15:31 288 查看
首先封装一个view

.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;
}];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: