IOS界面弹窗显示多个按钮

本文介绍如何在iOS应用中使用MLKMenuPopover类来创建并展示一个悬浮窗,该窗体包含多个可选按钮。通过遵守协议,实现菜单选择后的回调方法,提供显示、隐藏悬浮窗的动画效果。开发者可以在`viewDidLoad`等需要的地方调用相关方法进行显示和隐藏操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在下面网站可以找到demo:
http://123.th7.cn/code/MLKMenuPopover_2415.html

主要内容:
增加 MLKMenuPopover 这样一个类。

这个类用来显示悬浮窗。

增加一个 协议。
协议里面有一下方法:
- (void)menuPopover:(MLKMenuPopover *)menuPopover didSelectMenuItemAtIndex:(NSInteger)selectedIndex;

用来提供 按下按键的操作。

这个类 提供四个方法

  • (id)initWithFrame:(CGRect)frame menuItems:(NSArray *)menuItems; 创建悬浮窗
  • (void)showInView:(UIView *)view; 显示悬浮窗
  • (void)dismissMenuPopover; 隐藏悬浮窗
  • (void)layoutUIForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

@property(nonatomic,retain) NSArray *menuItems; 悬浮窗口的内容数组

//创建悬浮窗口 aMenuItems 这个参数是内容 frame是大小
- (id)initWithFrame:(CGRect)frame menuItems:(NSArray *)aMenuItems
{
self = [super initWithFrame:frame];

if (self)
{
    self.menuItems = aMenuItems;

    // Adding Container Button which will take care of hiding menu when user taps outside of menu area
    self.containerButton = [[UIButton alloc] init];//创建一个按钮
    [self.containerButton setBackgroundColor:CONTAINER_BG_COLOR];
    [self.containerButton addTarget:self action:@selector(dismissMenuPopover) forControlEvents:UIControlEventTouchUpInside];
    [self.containerButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin];

    // Adding Menu Options Pointer   创建图标
    UIImageView *menuPointerView = [[UIImageView alloc] initWithFrame:MENU_POINTER_RECT];
    menuPointerView.image = [UIImage imageNamed:@"options_pointer"];
    menuPointerView.tag = MENU_POINTER_TAG;
    [self.containerButton addSubview:menuPointerView];

    // Adding menu Items table   创建表格
    UITableView *menuItemsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 11, frame.size.width, frame.size.height)];

    menuItemsTableView.dataSource = self;
    menuItemsTableView.delegate = self;
    menuItemsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    menuItemsTableView.scrollEnabled = NO;
    menuItemsTableView.backgroundColor = [UIColor clearColor];
    menuItemsTableView.tag = MENU_TABLE_VIEW_TAG;

    UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Menu_PopOver_BG"]];
    menuItemsTableView.backgroundView = bgView;

//
[self addSubview:menuItemsTableView];

    [self.containerButton addSubview:self];
}

return self;

}

////显示/隐藏图层
- (void)dismissMenuPopover
{
[self hide];
}

  • (void)showInView:(UIView *)view
    {
    self.containerButton.alpha = ZERO;
    self.containerButton.frame = view.bounds;
    [view addSubview:self.containerButton];
    //用动画的效果
    [UIView animateWithDuration:ANIMATION_DURATION
    animations:^{
    self.containerButton.alpha = ONE;
    }
    completion:^(BOOL finished) {}];
    }

  • (void)hide
    {
    [UIView animateWithDuration:ANIMATION_DURATION
    animations:^{
    self.containerButton.alpha = ZERO;
    }
    completion:^(BOOL finished) {
    [self.containerButton removeFromSuperview];
    }];
    }

在需要用到的地方
这个类必须遵守这个协议
- (void)viewDidLoad
{
[super viewDidLoad];

self.title = @"Menu Popover";
//填充这个  数组内容
self.menuItems = [NSArray arrayWithObjects:@"Menu Item 1", @"Menu Item 2", nil];

}

//创建这个图层
self.menuPopover = [[MLKMenuPopover alloc] initWithFrame:MENU_POPOVER_FRAME menuItems:self.menuItems];

self.menuPopover.menuPopoverDelegate = self;
[self.menuPopover showInView:self.view];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值