// // LXActionSheet.m // LXActionSheetDemo // // Created by lixiang on 14-3-10. // Copyright (c) 2014年 lcolco. All rights reserved. // #import "LXActionSheet.h" #define CANCEL_BUTTON_COLOR [UIColor colorWithRed:53/255.00f green:53/255.00f blue:53/255.00f alpha:1] #define DESTRUCTIVE_BUTTON_COLOR [UIColor colorWithRed:185/255.00f green:45/255.00f blue:39/255.00f alpha:1] #define OTHER_BUTTON_COLOR [UIColor whiteColor] #define ACTIONSHEET_BACKGROUNDCOLOR [UIColor colorWithRed:83/255.00f green:202/255.00f blue:195/255.00f alpha:1] #define WINDOW_COLOR [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4] #define CORNER_RADIUS 5 #define BUTTON_INTERVAL_HEIGHT 20 #define BUTTON_HEIGHT 40 #define BUTTON_INTERVAL_WIDTH 30 #define BUTTON_WIDTH (JX_SCREEN_WIDTH - 60) #define BUTTONTITLE_FONT [UIFont fontWithName:@"HelveticaNeue-Bold" size:18] #define BUTTON_BORDER_WIDTH 0.5f #define BUTTON_BORDER_COLOR [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8].CGColor #define TITLE_INTERVAL_HEIGHT 15 #define TITLE_HEIGHT 35 #define TITLE_INTERVAL_WIDTH 30 #define TITLE_WIDTH 260 #define TITLE_FONT [UIFont fontWithName:@"Helvetica-Bold" size:14] #define SHADOW_OFFSET CGSizeMake(0, 0.8f) #define TITLE_NUMBER_LINES 2 #define ANIMATE_DURATION 0.25f @interface LXActionSheet () @property (nonatomic,strong) UIView *backGroundView; @property (nonatomic,strong) NSString *actionTitle; @property (nonatomic,assign) NSInteger postionIndexNumber; @property (nonatomic,assign) BOOL isHadTitle; @property (nonatomic,assign) BOOL isHadDestructionButton; @property (nonatomic,assign) BOOL isHadOtherButton; @property (nonatomic,assign) BOOL isHadCancelButton; @property (nonatomic,assign) CGFloat LXActionSheetHeight; @property (nonatomic,weak) iddelegate; @end @implementation LXActionSheet #pragma mark - Public method - (id)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitlesArray; { self = [super init]; if (self) { //初始化背景视图,添加手势 self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); self.backgroundColor = WINDOW_COLOR; self.userInteractionEnabled = YES; UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)]; [self addGestureRecognizer:tapGesture]; if (delegate) { self.delegate = delegate; } [self creatButtonsWithTitle:title cancelButtonTitle:cancelButtonTitle destructionButtonTitle:destructiveButtonTitle otherButtonTitles:otherButtonTitlesArray]; } return self; } - (void)showInView:(UIView *)view { [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:self]; } #pragma mark - CreatButtonAndTitle method - (void)creatButtonsWithTitle:(NSString *)title cancelButtonTitle:(NSString *)cancelButtonTitle destructionButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitlesArray { //初始化 self.isHadTitle = NO; self.isHadDestructionButton = NO; self.isHadOtherButton = NO; self.isHadCancelButton = NO; //初始化LXACtionView的高度为0 self.LXActionSheetHeight = 0; //初始化IndexNumber为0; self.postionIndexNumber = 0; //生成LXActionSheetView self.backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)]; self.backGroundView.layer.cornerRadius = 20; self.backGroundView.clipsToBounds = YES; self.backGroundView.backgroundColor = ACTIONSHEET_BACKGROUNDCOLOR; //给LXActionSheetView添加响应事件 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedBackGroundView)]; [self.backGroundView addGestureRecognizer:tapGesture]; [self addSubview:self.backGroundView]; if (title) { self.isHadTitle = YES; UILabel *titleLabel = [self creatTitleLabelWith:title]; self.LXActionSheetHeight = self.LXActionSheetHeight + 2*TITLE_INTERVAL_HEIGHT+TITLE_HEIGHT; [self.backGroundView addSubview:titleLabel]; } if (destructiveButtonTitle) { self.isHadDestructionButton = YES; UIButton *destructiveButton = [self creatDestructiveButtonWith:destructiveButtonTitle]; destructiveButton.tag = self.postionIndexNumber; [destructiveButton addTarget:self action:@selector(clickOnButtonWith:) forControlEvents:UIControlEventTouchUpInside]; if (self.isHadTitle == YES) { //当有title时 [destructiveButton setFrame:CGRectMake(destructiveButton.frame.origin.x, self.LXActionSheetHeight, destructiveButton.frame.size.width, destructiveButton.frame.size.height)]; if (otherButtonTitlesArray && otherButtonTitlesArray.count > 0) { self.LXActionSheetHeight = self.LXActionSheetHeight + destructiveButton.frame.size.height+BUTTON_INTERVAL_HEIGHT/2; } else{ self.LXActionSheetHeight = self.LXActionSheetHeight + destructiveButton.frame.size.height+BUTTON_INTERVAL_HEIGHT; } } else{ //当无title时 if (otherButtonTitlesArray && otherButtonTitlesArray.count > 0) { self.LXActionSheetHeight = self.LXActionSheetHeight + destructiveButton.frame.size.height+(BUTTON_INTERVAL_HEIGHT+(BUTTON_INTERVAL_HEIGHT/2)); } else{ self.LXActionSheetHeight = self.LXActionSheetHeight + destructiveButton.frame.size.height+(2*BUTTON_INTERVAL_HEIGHT); } } [self.backGroundView addSubview:destructiveButton]; self.postionIndexNumber++; } if (otherButtonTitlesArray) { if (otherButtonTitlesArray.count > 0) { self.isHadOtherButton = YES; //当无title与destructionButton时 if (self.isHadTitle == NO && self.isHadDestructionButton == NO) { for (int i = 0; i