仿QQ控件右上角按钮

首先封装一个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;
        }];

    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值