Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化了一些功能 #9

Merged
merged 3 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions LCActionSheet/LCActionSheet.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,26 @@ typedef void(^LCActionSheetBlock)(NSInteger buttonIndex);

@interface LCActionSheet : UIView

@property (nonatomic, copy) NSString *title;

@property (nonatomic, assign) NSInteger redButtonIndex;

@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

Expand Down Expand Up @@ -97,6 +116,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
Expand Down
131 changes: 82 additions & 49 deletions LCActionSheet/LCActionSheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@interface LCActionSheet ()

/** 所有按钮 */
@property (nonatomic, strong) NSArray *buttonTitles;
@property (nonatomic, strong) NSMutableArray *buttonTitles;

/** 暗黑色的view */
@property (nonatomic, strong) UIView *darkView;
Expand All @@ -35,18 +35,46 @@ @interface LCActionSheet ()

@property (nonatomic, strong) UIWindow *backWindow;

@property (nonatomic, copy) LCActionSheetBlock clickedBlock;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, assign) NSInteger redButtonIndex;
@end

@implementation LCActionSheet

#pragma mark - getter

@end
- (NSString *)cancelText
{
if (_cancelText) {
return _cancelText;
}

_cancelText = @"取消";
return _cancelText;
}

@implementation LCActionSheet
- (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<LCActionSheetDelegate>)delegate {

return [[self alloc] initWithTitle:title buttonTitles:buttonTitles redButtonIndex:redButtonIndex delegate:delegate];
Expand All @@ -65,11 +93,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;
Expand All @@ -83,11 +109,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;
Expand Down Expand Up @@ -149,15 +173,15 @@ - (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) {

titleColor = LCColor(255, 10, 10);

} else {

titleColor = [UIColor blackColor] ;
titleColor = self.textColor ;
}
[btn setTitleColor:titleColor forState:UIControlStateNormal];

Expand Down Expand Up @@ -191,9 +215,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];

Expand Down Expand Up @@ -240,13 +264,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];
Expand All @@ -259,34 +283,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];
Expand All @@ -304,4 +328,13 @@ - (void)show {
} completion:nil];
}

- (void)addButtonTitle:(NSString *)button
{
if (!_buttonTitles) {
_buttonTitles = [[NSMutableArray alloc] init];
}

[_buttonTitles addObject:button];
}

@end
6 changes: 4 additions & 2 deletions LCActionSheetDemo/Classes/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/** 修改头像 */
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ OK,这次我是看系统的 UIActionSheet 不爽。不能更改 tintColor (蓝

* 在相应位置导入头文件: `#import "LCActionSheet.h"`,遵守协议`<LCActionSheetDelegate>`。
* 调用下面的方法即可:

````objc
// 1. 类方法 + Block
LCActionSheet *sheet = [LCActionSheet sheetWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] redButtonIndex:-1 clicked:^(NSInteger buttonIndex) {
Expand All @@ -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
Expand Down Expand Up @@ -129,3 +148,5 @@ LCActionSheet *sheet = [[LCActionSheet alloc] initWithTitle:@"你确定要注销
## 授权 License

本项目采用 [MIT license](http://opensource.org/licenses/MIT) 开源,你可以利用采用该协议的代码做任何事情,只需要继续继承 MIT 协议即可。