actionSheetView

//  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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值