// MyActionSheetView.h
#import <UIKit/UIKit.h>
typedef void(^MyDialogCommand)();
typedef enum {
MyDialog_White_Style = 0,
MyDialog_Blue_Style = 1,
MyDialog_Red_Style = 2,
MyDialog_Green_Style = 3,
MyDialog_black_Style = 4
}MyButtonStyle;
@interface MyActionSheetView : UIView
{
NSMutableArray *_commands;
NSMutableArray *_buttons;
}
- (void)addCommand:(NSString*)name
buttonStype:(MyButtonStyle)style
callback:(MyDialogCommand)command;
- (void)showView;
@end
// MyActionSheetView.m
#import "MyActionSheetView.h"
@implementation MyActionSheetView
- (id)init
{
self = [super initWithFrame:[UIScreen mainScreen].bounds];
if (self) {
self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
_buttons = [[NSMutableArray alloc] init];
_commands = [[NSMutableArray alloc] init];
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self removeFromSuperview];
}
- (void)showView
{
UIView *cancleView = [[UIView alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width - 20, 40)];
cancleView.backgroundColor = [UIColor whiteColor];
UIButton *cancelbutton = [_buttons objectAtIndex:0];
cancleView.layer.cornerRadius = 5.0;
cancelbutton.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width - 20, 40);
[cancelbutton setBackgroundColor:[UIColor clearColor]];
[cancleView addSubview:cancelbutton];
[self addSubview:cancleView];
UIView *otherView = [[UIView alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.height - 50-10 - 40*(_buttons.count - 1), [UIScreen mainScreen].bounds.size.width-20, 40*(_buttons.count - 1))];
otherView.backgroundColor = [UIColor whiteColor];
otherView.layer.cornerRadius = 5.0;
for (int i = 1; i < [_buttons count]; i++) {
UIButton *button = [_buttons objectAtIndex:i];
button.frame = CGRectMake(0,otherView.frame.size.height-40*i, [UIScreen mainScreen].bounds.size.width- 20, 40);
button.backgroundColor = [UIColor clearColor];
[otherView addSubview:button];
UIView *grayLine = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.origin.y+button.frame.size.height-0.3, button.frame.size.width, 0.3)];
grayLine.backgroundColor = [UIColor grayColor];
[otherView addSubview:grayLine];
}
[self addSubview:otherView];
[[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)addCommand:(NSString*)name
buttonStype:(MyButtonStyle)style
callback:(MyDialogCommand)command
{
UIButton *button = nil;
switch (style) {
case MyDialog_White_Style:
button = [self createWhiteStyleButton:name];
break;
case MyDialog_Blue_Style:
button = [self createBlueStyleButton:name];
break;
case MyDialog_Red_Style:
button = [self createRedStyleButton:name];
break;
case MyDialog_Green_Style:
button = [self createGreenStyleButton:name];
break;
case MyDialog_black_Style:
button = [self createBlackStyleButton:name];
break;
}
if (button == nil) {
return;
}
[self addButton:button callback:command];
}
- (void)addButton:(UIButton*)button
callback:(MyDialogCommand)command {
[_buttons addObject:button];
[button addTarget:self
action:@selector(onTapCommandButton:)
forControlEvents:UIControlEventTouchUpInside];
[_commands addObject:command];
}
- (void)onTapCommandButton:(id)sender {
NSInteger index = [_buttons indexOfObject:sender];
MyDialogCommand command = [_commands objectAtIndex:index];
if (command) {
command();
[self removeFromSuperview];
}
}
- (UIButton *)createGreenStyleButton:(NSString *)title
{
UIButton *button = [[UIButton alloc] init];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button.titleLabel setShadowOffset:CGSizeMake(0.0, 1.0)];
[button setTitle:title forState:UIControlStateNormal];
return button;
}
- (UIButton *)createBlackStyleButton:(NSString *)title
{
UIButton *button = [[UIButton alloc] init];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button.titleLabel setShadowOffset:CGSizeMake(0.0, 1.0)];
[button setTitle:title forState:UIControlStateNormal];
return button;
}
- (UIButton*)createWhiteStyleButton:(NSString*)title {
UIButton *button = [[UIButton alloc] init];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
return button;
}
- (UIButton*)createBlueStyleButton:(NSString*)title {
UIButton *button = [[UIButton alloc] init];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button.titleLabel setShadowOffset:CGSizeMake(0.0, 1.0)];
[button setTitle:title forState:UIControlStateNormal];
return button;
}
- (UIButton*)createRedStyleButton:(NSString*)title {
UIButton *button = [[UIButton alloc] init];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button.titleLabel setShadowOffset:CGSizeMake(0.0, 1.0)];
[button setTitle:title forState:UIControlStateNormal];
return button;
}
@end