您的位置:首页 > 移动开发 > IOS开发

IOS界面弹窗显示多个按钮

2016-01-12 17:06 441 查看
在下面网站可以找到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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: