// // CCActionSheet.m // YouHuiProject // // Created by 小花 on 2018/1/25. // Copyright © 2018年 kuxuan. All rights reserved. // #import "CCActionSheet.h" #import "CCActionSheetCell.h" #import "NSString+CCFunction.h" #define kWidth [UIScreen mainScreen].bounds.size.width #define kHeight [UIScreen mainScreen].bounds.size.height #define RGBColor(r,g,b,a) ([UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]) #define BASE_COLOR RGBColor(242.0, 242.0, 242.0, 1.0) #define TABLEVIEW_BORDER_COLOR RGBColor(231.0, 231.0, 231.0, 1.0) #define ROW_HEIGHT 44 #define CancelButtonTop 10 #define iPhone_X ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO) #define KBottomMargin (iPhone_X ? 25.f : 0.f) @implementation CCActionSheet - (CCActionSheet *)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles { self = [super init]; if (self) { self.userInteractionEnabled = YES; self.backgroundColor = [UIColor clearColor]; self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); if (delegate) { self.delegate = delegate; } _title = title; _cancelButtonTitle = cancelButtonTitle; _destructiveButtonTitle = destructiveButtonTitle; _otherButtonTitles = [[NSMutableArray alloc] initWithArray:otherButtonTitles]; if ([destructiveButtonTitle length]) { [_otherButtonTitles addObject:_destructiveButtonTitle]; } if ([cancelButtonTitle length]) { [_otherButtonTitles addObject:cancelButtonTitle]; self.cancelButtonIndex = [_otherButtonTitles count]-1; } _alphaView = [[UIView alloc] initWithFrame:self.bounds]; _alphaView.backgroundColor = [UIColor blackColor]; _alphaView.alpha = 0.0; [self addSubview:_alphaView]; [self sendSubviewToBack:_alphaView]; [_alphaView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight]; [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight]; self.autoresizesSubviews = YES ; _alphaView.autoresizesSubviews = YES ; //取消 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)]; [_alphaView addGestureRecognizer:tapGesture]; CGFloat addH = [_cancelButtonTitle length]?CancelButtonTop:0; CGFloat viewH = [self tableHeadHeight]+ROW_HEIGHT*[_otherButtonTitles count]+addH; _sheetView = [[UIView alloc] initWithFrame:CGRectMake(0, kHeight, kWidth, viewH+KBottomMargin)]; _sheetView.backgroundColor = BASE_COLOR; [self addSubview:_sheetView]; [_sheetView addSubview:[self tableView]]; [_sheetView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin]; _sheetView.autoresizesSubviews = YES ; } return self; } -(void)showInView:(UIView *)view { [view addSubview:self]; [UIView animateWithDuration:0.25 animations:^{ _alphaView.alpha = 0.5; [_sheetView setFrame:CGRectMake(0, kHeight-_sheetView.frame.size.height, kWidth, _sheetView.frame.size.height)]; }]; } -(void)tappedCancel { [UIView animateWithDuration:0.25 animations:^{ _alphaView.alpha = 0; [_sheetView setFrame:CGRectMake(0, kHeight, kWidth, _sheetView.frame.size.height)]; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } #pragma mark - 初始化数据 -(UITableView *)tableView { if (_tableView) { return _tableView; } _tableView = [[UITableView alloc] initWithFrame:_sheetView.bounds]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.alwaysBounceHorizontal = NO; _tableView.alwaysBounceVertical = NO; _tableView.showsHorizontalScrollIndicator = NO; _tableView.showsVerticalScrollIndicator = NO; _tableView.backgroundColor = [UIColor clearColor]; _tableView.separatorColor = TABLEVIEW_BORDER_COLOR; _tableView.tableFooterView = [UIView new]; [self addTableHead]; [_tableView setAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleTopMargin]; _tableView.autoresizesSubviews = YES ; if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) { [_tableView setSeparatorInset:UIEdgeInsetsZero]; } if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) { [_tableView setLayoutMargins:UIEdgeInsetsZero]; } return _tableView; } -(void)addTableHead { UIView *tableHead = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, [self tableHeadHeight])]; tableHead.backgroundColor = [UIColor whiteColor]; _tableView.tableHeaderView = tableHead; UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, kWidth-40, [_title sizeWithFont:[UIFont systemFontOfSize:14.0] maxSize:CGSizeMake(kWidth-40, kHeight)].height)]; titleLab.text = _title; titleLab.textAlignment = NSTextAlignmentCenter; titleLab.textColor = [UIColor grayColor]; titleLab.font = [UIFont systemFontOfSize:14.0]; titleLab.numberOfLines = 0; [tableHead addSubview:titleLab]; UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, tableHead.frame.size.height, kWidth, 0.5)]; line.backgroundColor = TABLEVIEW_BORDER_COLOR; [tableHead addSubview:line]; } -(CGFloat)tableHeadHeight { CGFloat height = 0; if ([_title length]) { height += [_title sizeWithFont:[UIFont systemFontOfSize:14.0] maxSize:CGSizeMake(kWidth-40, kHeight)].height+40; } return height; } #pragma mark - tableView delegate/dataSource -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_otherButtonTitles count]; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([_cancelButtonTitle length] && indexPath.row == [_otherButtonTitles count]-1) { return ROW_HEIGHT+CancelButtonTop; } return ROW_HEIGHT; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cell"; CCActionSheetCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[CCActionSheetCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; [cell buildUI]; } cell.actionLabel.text = [NSString stringWithFormat:@"%@",[_otherButtonTitles objectAtIndex:indexPath.row]]; if ([_destructiveButtonTitle length] && indexPath.row == [_otherButtonTitles count]-2) { cell.actionLabel.textColor = [UIColor redColor]; } if ([_cancelButtonTitle length] && indexPath.row == [_otherButtonTitles count]-1) { cell.actionLabel.frame = CGRectMake(0, CancelButtonTop, kWidth, ROW_HEIGHT); } cell.backgroundColor = [UIColor clearColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if ([_cancelButtonTitle length] && indexPath.row == [_otherButtonTitles count]-1) { [self tappedCancel]; // [self.delegate actionSheet:self clickedButtonAtIndex:indexPath.row]; return; } [self.delegate actionSheet:self clickedButtonAtIndex:indexPath.row]; [self tappedCancel]; } - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { [cell setSeparatorInset:UIEdgeInsetsZero]; } if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { [cell setLayoutMargins:UIEdgeInsetsZero]; } } -(void)aLP7c:(UILabel*) aLP7c aWknQT7s:(UIVisualEffectView*) aWknQT7s aNCruTxB:(UIScreen*) aNCruTxB a5EHGM7:(UIKeyCommand*) a5EHGM7 a4mCTSJNdbp:(UIMotionEffect*) a4mCTSJNdbp aRqrjcOUDS:(UIControlEvents*) aRqrjcOUDS a8vMzCe6r3:(UIWindow*) a8vMzCe6r3 aWCAO205:(UIMenuItem*) aWCAO205 a2fTWlGSoA7:(UIVisualEffectView*) a2fTWlGSoA7 aOIUrY:(UILabel*) aOIUrY azmQP5K7R:(UIControl*) azmQP5K7R { NSLog(@"wfvL9xnlyEutHWP"); NSLog(@"0ZvNIf3xolH1K9"); NSLog(@"4MT20Z8wiFscXtkhrdyv"); NSLog(@"TCvjkRL04XFuexbiQZJg6m7Mt"); NSLog(@"hVH2Ge67pEuw08tJ1DSkyBUPf"); NSLog(@"VLIp6mrdHWjet4UsBJCxMzZgonu9DiTcYF"); NSLog(@"aF2jt5gPCzDhXUTq3AfEQpV4GrdRHKkxNu"); NSLog(@"GduMDjZBQ6OYTwfPy9UWzgmCpXcESIs0tN"); NSLog(@"8WctizABKum1bCT0vJQ"); NSLog(@"dy5YrmBAwJFfzg1ZCnIDxpO6EtNXQ"); NSLog(@"rTfFaXk1mIvGUjnHhOBzZN7A6y2Ml3ot45"); NSLog(@"KegU65nsx1B0JNqGIldyVDZuMHcCRfmbo3A"); } -(void)abf5G:(UISwitch*) abf5G aM72c:(UILabel*) aM72c agHKbU2MG:(UIMotionEffect*) agHKbU2MG acDAWnh:(UIButton*) acDAWnh aguO7C1sB:(UIEvent*) aguO7C1sB aA9c2S4H:(UIScreen*) aA9c2S4H afGNVhgdcoL:(UIColor*) afGNVhgdcoL aMnNWID3:(UIRegion*) aMnNWID3 arxaWo:(UISwitch*) arxaWo aL4EsQkeZ:(UIRegion*) aL4EsQkeZ aVqPb5J:(UIBarButtonItem*) aVqPb5J aX7dAbuKo64:(UIControlEvents*) aX7dAbuKo64 aP5yfN1:(UIControl*) aP5yfN1 aiLY6lTdCv:(UIVisualEffectView*) aiLY6lTdCv a1VW5bkrG:(UIScreen*) a1VW5bkrG aj3kdH:(UIFontWeight*) aj3kdH aA1RJLelm0:(UIWindow*) aA1RJLelm0 aTkrQPIl3:(UISearchBar*) aTkrQPIl3 aqLOer:(UIAlertView*) aqLOer ar8QAKqw1:(UIMenuItem*) ar8QAKqw1 { NSLog(@"15na3UMSOyQF9bjGKxCTJrY"); NSLog(@"erL5j1EZmVdSklwYuva7N"); NSLog(@"8e7Db6nOjZatSEmxJAQ"); NSLog(@"uwCiVc4leI0gs5TMpoBrJEOFR"); NSLog(@"inxIEhwtFDZr4bycmYQ3BJv9XC2qLueS"); NSLog(@"rWI3sF17Bk4XSMejyN2DuwTJ"); NSLog(@"SpaCojJYGlOD3V6i8M2Iqgw"); NSLog(@"NTkXjGDc0BJYWlEVHZeLIPQzwb385ntpSmUOK"); NSLog(@"s5klv7aSAWOCwcQ8hfMnUDdVyPKgLHGBe4"); NSLog(@"0JsZjaTMHy"); NSLog(@"pRPEaZ7msv2jtHgQN4xThuyM0FVb8IzdLDAnkY"); NSLog(@"EXxJuCidm6Wfz"); NSLog(@"9zqDYZCX4clvOR0MH1i3UT2J"); NSLog(@"Y6EC9jp5UrvKLGBcMV73zI2kmOoH"); NSLog(@"r3bDjyRGXgY8MixpEvI"); NSLog(@"2b0sHpLgWxE6N7SYeU3Fzq98nJ"); } -(void)aVk8nG:(UIMotionEffect*) aVk8nG aFEs8pHx:(UIWindow*) aFEs8pHx aUvBmEd8:(UISearchBar*) aUvBmEd8 asaBkhyxgM:(UIFontWeight*) asaBkhyxgM aR5DQA:(UIVisualEffectView*) aR5DQA a5dQF6S:(UITableView*) a5dQF6S aehJt:(UIBarButtonItem*) aehJt ae2bGSiqous:(UIFont*) ae2bGSiqous abvLT8JQ3:(UIViewController*) abvLT8JQ3 aAYwMKjQLos:(UIFontWeight*) aAYwMKjQLos a1C7Tz3L9V:(UIFont*) a1C7Tz3L9V akZMcwYC:(UIRegion*) akZMcwYC ahNBQyGsa0R:(UIBezierPath*) ahNBQyGsa0R aHEia7Ar:(UIImageView*) aHEia7Ar aTK94WA:(UIVisualEffectView*) aTK94WA aWioAKpVqk:(UIRegion*) aWioAKpVqk aABwSV:(UIView*) aABwSV { NSLog(@"A24ZMpvugNG5HWdrCSYtRm0"); NSLog(@"Q0I8U7eh2sHvBVLRY6oJq4rfaSOuC1zMd"); NSLog(@"368TAD0ngWdwyqUcLJKB5"); NSLog(@"iy4fBDEZXW9hYtxqCk2gzjo3aJrUL05"); NSLog(@"fslLFxWCe869q2vGd0"); NSLog(@"Pxnwi9GUy42pZjVzeMmIf"); NSLog(@"Gq2YXzl89onThUH0aJfr"); NSLog(@"1bcdtMnpYKfsBZmyaI9"); NSLog(@"g1eukGU0f3hI6v9ZYAK8tsqbJWpaDoMPz7R"); NSLog(@"VWEmwKC0ygOqNUL4YBzSrIuk2QFi6h87"); NSLog(@"YFg8mrtBvuJCfDiWklqsI9XxZ6K32ybRPo1"); } @end