From f8a5f8e4a929cbce5f2fcde8526cb34762417690 Mon Sep 17 00:00:00 2001 From: zachgenius Date: Wed, 16 Dec 2015 16:32:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=B7=BB=E5=8A=A0=E6=8C=89=E9=92=AE=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=8C=89=E9=92=AE=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=8C=96=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E6=8C=89=E9=92=AE=E9=A2=9C=E8=89=B2=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E4=B8=94=E4=BC=98=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LCActionSheet/LCActionSheet.h | 24 ++++ LCActionSheet/LCActionSheet.m | 129 +++++++++++++-------- LCActionSheetDemo/Classes/ViewController.m | 6 +- 3 files changed, 111 insertions(+), 48 deletions(-) diff --git a/LCActionSheet/LCActionSheet.h b/LCActionSheet/LCActionSheet.h index 7547d3e..8adb058 100644 --- a/LCActionSheet/LCActionSheet.h +++ b/LCActionSheet/LCActionSheet.h @@ -27,7 +27,22 @@ typedef void(^LCActionSheetBlock)(NSInteger buttonIndex); @interface LCActionSheet : UIView +@property (nonatomic, copy) LCActionSheetBlock clickedBlock; +/** + * localized cancel text. Default is "取消" + */ +@property (nonatomic, strong) NSString *cancelText; + +/** + * Default is [UIFont systemFontOfSize:18]; + */ +@property (nonatomic, strong) UIFont *textFont; + +/** + * Default is Black + */ +@property (nonatomic, strong) UIColor *textColor; #pragma mark - Delegate Way @@ -97,6 +112,15 @@ typedef void(^LCActionSheetBlock)(NSInteger buttonIndex); clicked:(LCActionSheetBlock)clicked; +#pragma mark - Custom Way + +/** + * Add a button with callback block + * + * @param button + * @param block + */ +- (void) addButtonTitle:(NSString*)button; #pragma mark - Show diff --git a/LCActionSheet/LCActionSheet.m b/LCActionSheet/LCActionSheet.m index 83f2d2e..5307b7d 100644 --- a/LCActionSheet/LCActionSheet.m +++ b/LCActionSheet/LCActionSheet.m @@ -22,7 +22,7 @@ @interface LCActionSheet () /** 所有按钮 */ -@property (nonatomic, strong) NSArray *buttonTitles; +@property (nonatomic, strong) NSMutableArray *buttonTitles; /** 暗黑色的view */ @property (nonatomic, strong) UIView *darkView; @@ -35,8 +35,6 @@ @interface LCActionSheet () @property (nonatomic, strong) UIWindow *backWindow; -@property (nonatomic, copy) LCActionSheetBlock clickedBlock; - @property (nonatomic, copy) NSString *title; @property (nonatomic, assign) NSInteger redButtonIndex; @@ -47,6 +45,40 @@ @interface LCActionSheet () @implementation LCActionSheet +#pragma mark - getter + +- (NSString *)cancelText +{ + if (_cancelText) { + return _cancelText; + } + + _cancelText = @"取消"; + return _cancelText; +} + +- (UIFont *)textFont +{ + if (_textFont) { + return _textFont; + } + + _textFont = LC_ACTION_SHEET_TITLE_FONT; + + return _textFont; +} + +- (UIColor *)textColor +{ + if (_textColor) { + return _textColor; + } + + _textColor = [UIColor blackColor]; + return _textColor; +} + +#pragma mark - methods + (instancetype)sheetWithTitle:(NSString *)title buttonTitles:(NSArray *)buttonTitles redButtonIndex:(NSInteger)redButtonIndex delegate:(id)delegate { return [[self alloc] initWithTitle:title buttonTitles:buttonTitles redButtonIndex:redButtonIndex delegate:delegate]; @@ -65,11 +97,9 @@ - (instancetype)initWithTitle:(NSString *)title if (self = [super init]) { self.title = title; - self.buttonTitles = buttonTitles; + self.buttonTitles = [[NSMutableArray alloc] initWithArray:buttonTitles]; self.redButtonIndex = redButtonIndex; self.delegate = delegate; - - [self setupMainView]; } return self; @@ -83,11 +113,9 @@ - (instancetype)initWithTitle:(NSString *)title if (self = [super init]) { self.title = title; - self.buttonTitles = buttonTitles; + self.buttonTitles = [[NSMutableArray alloc] initWithArray:buttonTitles]; self.redButtonIndex = redButtonIndex; self.clickedBlock = clicked; - - [self setupMainView]; } return self; @@ -149,7 +177,7 @@ - (void)setupMainView { [btn setTag:i]; [btn setBackgroundColor:[UIColor whiteColor]]; [btn setTitle:self.buttonTitles[i] forState:UIControlStateNormal]; - [[btn titleLabel] setFont:LC_ACTION_SHEET_TITLE_FONT]; + [[btn titleLabel] setFont:self.textFont]; UIColor *titleColor = nil; if (i == self.redButtonIndex) { @@ -157,7 +185,7 @@ - (void)setupMainView { } else { - titleColor = [UIColor blackColor] ; + titleColor = self.textColor ; } [btn setTitleColor:titleColor forState:UIControlStateNormal]; @@ -191,9 +219,9 @@ - (void)setupMainView { UIButton *cancelBtn = [[UIButton alloc] init]; [cancelBtn setTag:self.buttonTitles.count]; [cancelBtn setBackgroundColor:[UIColor whiteColor]]; - [cancelBtn setTitle:@"取消" forState:UIControlStateNormal]; - [[cancelBtn titleLabel] setFont:LC_ACTION_SHEET_TITLE_FONT]; - [cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; + [cancelBtn setTitle:self.cancelText forState:UIControlStateNormal]; + [[cancelBtn titleLabel] setFont:self.textFont]; + [cancelBtn setTitleColor:self.textColor forState:UIControlStateNormal]; [cancelBtn setBackgroundImage:[UIImage imageNamed:@"bgImage_HL"] forState:UIControlStateHighlighted]; [cancelBtn addTarget:self action:@selector(didClickCancelBtn) forControlEvents:UIControlEventTouchUpInside]; @@ -240,13 +268,13 @@ - (void)dismiss:(UITapGestureRecognizer *)tap { [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ - [_darkView setAlpha:0]; - [_darkView setUserInteractionEnabled:NO]; - - CGRect frame = _bottomView.frame; - frame.origin.y += frame.size.height; - [_bottomView setFrame:frame]; - + [_darkView setAlpha:0]; + [_darkView setUserInteractionEnabled:NO]; + + CGRect frame = _bottomView.frame; + frame.origin.y += frame.size.height; + [_bottomView setFrame:frame]; + } completion:^(BOOL finished) { [self removeFromSuperview]; @@ -259,34 +287,34 @@ - (void)didClickCancelBtn { [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{ - [_darkView setAlpha:0]; - [_darkView setUserInteractionEnabled:NO]; - - CGRect frame = _bottomView.frame; - frame.origin.y += frame.size.height; - [_bottomView setFrame:frame]; - - } completion:^(BOOL finished) { - - if ([_delegate respondsToSelector:@selector(actionSheet:didClickedButtonAtIndex:)]) { - - [_delegate actionSheet:self didClickedButtonAtIndex:self.buttonTitles.count]; - } - - if (self.clickedBlock) { - - __weak typeof(self) weakSelf = self; - self.clickedBlock(weakSelf.buttonTitles.count); - } - - [self removeFromSuperview]; - - self.backWindow.hidden = YES; - }]; + [_darkView setAlpha:0]; + [_darkView setUserInteractionEnabled:NO]; + + CGRect frame = _bottomView.frame; + frame.origin.y += frame.size.height; + [_bottomView setFrame:frame]; + + } completion:^(BOOL finished) { + + if ([_delegate respondsToSelector:@selector(actionSheet:didClickedButtonAtIndex:)]) { + + [_delegate actionSheet:self didClickedButtonAtIndex:self.buttonTitles.count]; + } + + if (self.clickedBlock) { + + __weak typeof(self) weakSelf = self; + self.clickedBlock(weakSelf.buttonTitles.count); + } + + [self removeFromSuperview]; + + self.backWindow.hidden = YES; + }]; } - (void)show { - + [self setupMainView]; self.backWindow.hidden = NO; [self addSubview:self.bottomView]; @@ -304,4 +332,13 @@ - (void)show { } completion:nil]; } +- (void)addButtonTitle:(NSString *)button +{ + if (!_buttonTitles) { + _buttonTitles = [[NSMutableArray alloc] init]; + } + + [_buttonTitles addObject:button]; +} + @end diff --git a/LCActionSheetDemo/Classes/ViewController.m b/LCActionSheetDemo/Classes/ViewController.m index bfe08c4..2237099 100644 --- a/LCActionSheetDemo/Classes/ViewController.m +++ b/LCActionSheetDemo/Classes/ViewController.m @@ -20,12 +20,14 @@ - (IBAction)logout { // 实例方法 LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:@"你确定要注销吗?" - buttonTitles:@[@"确定"] + buttonTitles:nil redButtonIndex:0 delegate:self]; + [sheet addButtonTitle:@"注销"]; + [sheet show]; -// [[[UIActionSheet alloc] initWithTitle:@"A very very very very very very very very very very very very very very long title" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"OK" otherButtonTitles:@"haha", @"2333", nil] showInView:self.view]; + // [[[UIActionSheet alloc] initWithTitle:@"A very very very very very very very very very very very very very very long title" delegate:nil cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"OK" otherButtonTitles:@"haha", @"2333", nil] showInView:self.view]; } /** 修改头像 */ From 9bd1dfdef007a4814fdc1724ca9664f8072368af Mon Sep 17 00:00:00 2001 From: zachgenius Date: Wed, 16 Dec 2015 16:40:27 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=B0=86=E6=A0=87=E9=A2=98=E5=92=8C?= =?UTF-8?q?=E7=BA=A2=E8=89=B2=E5=AD=97=E5=BA=8F=E6=9A=B4=E9=9C=B2=E7=BB=99?= =?UTF-8?q?=E5=A4=96=E9=83=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LCActionSheet/LCActionSheet.h | 4 ++++ LCActionSheet/LCActionSheet.m | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/LCActionSheet/LCActionSheet.h b/LCActionSheet/LCActionSheet.h index 8adb058..6410497 100644 --- a/LCActionSheet/LCActionSheet.h +++ b/LCActionSheet/LCActionSheet.h @@ -27,6 +27,10 @@ typedef void(^LCActionSheetBlock)(NSInteger buttonIndex); @interface LCActionSheet : UIView +@property (nonatomic, copy) NSString *title; + +@property (nonatomic, assign) NSInteger redButtonIndex; + @property (nonatomic, copy) LCActionSheetBlock clickedBlock; /** diff --git a/LCActionSheet/LCActionSheet.m b/LCActionSheet/LCActionSheet.m index 5307b7d..8588cab 100644 --- a/LCActionSheet/LCActionSheet.m +++ b/LCActionSheet/LCActionSheet.m @@ -35,10 +35,6 @@ @interface LCActionSheet () @property (nonatomic, strong) UIWindow *backWindow; -@property (nonatomic, copy) NSString *title; - -@property (nonatomic, assign) NSInteger redButtonIndex; - @end From 02bc8554fa2e30203aa2735d0101210e51515321 Mon Sep 17 00:00:00 2001 From: zachgenius Date: Wed, 16 Dec 2015 16:53:30 +0800 Subject: [PATCH 3/3] update Readme.md --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 7a7b623..aa68923 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ OK,这次我是看系统的 UIActionSheet 不爽。不能更改 tintColor (蓝 * 在相应位置导入头文件: `#import "LCActionSheet.h"`,遵守协议``。 * 调用下面的方法即可: + ````objc // 1. 类方法 + Block LCActionSheet *sheet = [LCActionSheet sheetWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] redButtonIndex:-1 clicked:^(NSInteger buttonIndex) { @@ -47,10 +48,28 @@ LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:@"你确定要注销 ```` * 监听方法(代理方法,可选实现): + ````objc - (void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex; ```` +````objc +// 3. 自定义实现 +LCActionSheet* sheet = [[LCActionSheet alloc] init]; +float version = [[[UIDevice currentDevice] systemVersion] floatValue]; +if (version < 8.0) +{ + [sheet addButtonTitle:@"iOS 7.x"]; +} +else +{ + [sheet addButtonTitle:@"iOS 8+"]; +} +sheet.clickedBlock = ^(NSInteger buttonIndex) { + }; +[sheet show]; + +```` ## TODO @@ -129,3 +148,5 @@ LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:@"你确定要注销 ## 授权 License 本项目采用 [MIT license](http://opensource.org/licenses/MIT) 开源,你可以利用采用该协议的代码做任何事情,只需要继续继承 MIT 协议即可。 + +