Skip to content

Commit

Permalink
1.7.0
Browse files Browse the repository at this point in the history
1. 支持自定义动画
2. 优化 API
  • Loading branch information
yulingtianxia committed Jan 12, 2019
1 parent 78ba88e commit 042847a
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 217 deletions.
20 changes: 15 additions & 5 deletions Example/TBAlertControllerDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ - (IBAction)clickActionSheet:(UIButton *)sender {
// [addBtn addTarget:self action:@selector(addButton:) forControlEvents:UIControlEventTouchUpInside];
// self.actionSheet.customView = addBtn;

TBActionButton *destructiveBtn = [self.actionSheet buttonAtIndex:self.actionSheet.destructiveButtonIndex];
destructiveBtn.animation = ^(UIImageView * _Nonnull background, UIView * _Nonnull container, void (^ _Nonnull completion)(void)) {
[UIView animateWithDuration:0.5 animations:^{
background.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
container.frame = CGRectMake(CGRectGetMidX(container.frame), CGRectGetMidY(container.frame), 0, 0);
} completion:^(BOOL finished) {
completion();
}];
};

__weak __typeof(ViewController *) weakSelf = self;
[self.actionSheet addButtonWithTitle:@"支持 block" style:TBActionButtonStyleCancel handler:^(TBActionButton * _Nonnull button) {
NSLog(@"%@ %@",button.currentTitle,weakSelf.leakTest);
}];
TBActionButton *btn = [self.actionSheet buttonAtIndex:self.actionSheet.numberOfButtons - 1];
btn.normalColor = [UIColor yellowColor];
btn.highlightedColor = [UIColor greenColor];
TBActionButton *blockBtn = [self.actionSheet buttonAtIndex:self.actionSheet.numberOfButtons - 1];
blockBtn.normalColor = [UIColor yellowColor];
blockBtn.highlightedColor = [UIColor greenColor];

//创建NSMutableAttributedString
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"支持 block\n限时推广"];
Expand All @@ -83,8 +93,8 @@ - (IBAction)clickActionSheet:(UIButton *)sender {
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, 9)];
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(9, 4)];

[btn setAttributedTitle:attrStr forState:UIControlStateNormal];
btn.height = 70;
[blockBtn setAttributedTitle:attrStr forState:UIControlStateNormal];
blockBtn.height = 70;
[self.actionSheet show];
[self.conditioner setUpUI];
}
Expand Down
157 changes: 7 additions & 150 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ Here are some pictures showing TBActionSheet's powerful individuation. You can c
![](images/demo.gif)
![](images/addButton.gif)

**TBActionSheet supports autorotation**, but there is some bugs on iOS7 :(

BTW, `TBActionSheet` also suppots BLOCK now!
BTW, `TBActionSheet` also supports **CUSTOM ANIMATION**!

## 🌟 Feature

- [x] Title
- [x] Description message
- [x] Insert a custom view at anywhere
- [x] Custom animation.
- [x] Customizations: fonts, colors, dimensions, corners & more
- [x] Closure when a button or background is pressed
- [x] Similar implementation to UIActionSheet
- [x] Add button using block syntax
- [x] Scroll the whole action sheet.
- [x] Blur Effect under iOS7
- [x] Autorotation under iOS8
- [x] Cocoapods
- [x] CocoaPods
- [x] Carthage

## 📚 Article
Expand All @@ -52,8 +51,8 @@ To run the example project, clone the repo and run MTDemo target.

## 💰 Requirement

- iOS 6.0+ (iOS 8.0+ full features supported)
- Xcode 9.1+
- iOS 7.0+ (iOS 8.0+ full features supported)
- Xcode 10.1+

## 📲 Installation

Expand Down Expand Up @@ -124,149 +123,9 @@ rectCornerRadius = 0;

### More than what you want

The base usage is same to `UIActionSheet`. You can just replace `UIActionSheet` with `TBActionSheet`. If you want to customize your action sheet, just configure some properties. I believe the header file can tell you much more than me.
The base usage is same to `UIActionSheet`. You can just replace `UIActionSheet` with `TBActionSheet`. If you want to customize your action sheet, just configure some properties.

```
@interface TBActionSheet : UIView
@property(nullable,nonatomic,weak) id<TBActionSheetDelegate> delegate;
@property(nullable,nonatomic,copy) NSString *title;
@property(nullable,nonatomic,copy) NSString *message;
/**
* 标记藏于 ActionSheet 下面的 UIWindow
*/
@property (weak, nonatomic, readonly) UIWindow *previousKeyWindow;
- (instancetype)initWithTitle:(nullable NSString *)title delegate:(nullable id <TBActionSheetDelegate>)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithTitle:(nullable NSString *)title message:(nullable NSString *)message delegate:(nullable id <TBActionSheetDelegate>)delegate cancelButtonTitle:(nullable NSString *)cancelButtonTitle destructiveButtonTitle:(nullable NSString *)destructiveButtonTitle otherButtonTitles:(nullable NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// destructive and cancel button which will be positioned based on HI requirements. buttons cannot be customized.
- (NSInteger)addButtonWithTitle:(nullable NSString *)title;
- (NSInteger)addButtonWithTitle:(nullable NSString *)title style:(TBActionButtonStyle)style; // returns index of button. 0 based.
- (NSInteger)addButtonWithTitle:(nullable NSString *)title style:(TBActionButtonStyle)style handler:(nullable void (^)(TBActionButton * button))handler;
- (nullable NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
- (nullable TBActionButton *)buttonAtIndex:(NSInteger)buttonIndex;
@property(nonatomic,readonly) NSInteger numberOfButtons;
@property(nonatomic) NSInteger cancelButtonIndex; // if the delegate does not implement -actionSheetCancel:, we pretend this button was clicked on. default is -1
@property(nonatomic) NSInteger destructiveButtonIndex; // sets destructive (red) button. -1 means none set. default is -1. ignored if only one button
@property(nonatomic,readonly) NSInteger firstOtherButtonIndex; // -1 if no otherButtonTitles or initWithTitle:... not used
/**
自定义视图在按钮中插入的 Index,值在 [0...numberOfButtons] 之间。如果 customView 为 nil,则 customViewIndex 值为 -1
*/
@property(nonatomic) NSInteger customViewIndex;
/**
* 是否可见
*/
@property(nonatomic,readonly,getter=isVisible) BOOL visible;
/**
* 显示 ActionSheet
*/
- (void)show;
/**
* 显示 ActionSheet,已废弃
*
* @param view 此参数直接传 nil
*/
- (void)showInView:(nullable UIView *)view __deprecated;
//custom UI
/**
* 按钮高度
*/
@property(nonatomic) CGFloat buttonHeight UI_APPEARANCE_SELECTOR;
/**
* actionsheet下方的 y 轴位移,向下为正,非负值无效,默认值为 -8
*/
@property(nonatomic) CGFloat offsetY UI_APPEARANCE_SELECTOR;
/**
* 标题 UILabel
*/
@property(nonatomic,strong,nullable,readonly) UILabel *titleLabel;
/**
* Message UILabel
*/
@property(nonatomic,strong,nullable,readonly) UILabel *messageLabel;
/**
* 文字颜色
*/
@property(nonatomic,strong) UIColor *tintColor UI_APPEARANCE_SELECTOR;
/**
* Destructive 按钮文字颜色
*/
@property(nonatomic,strong) UIColor *destructiveButtonColor UI_APPEARANCE_SELECTOR;
/**
* Cancel 按钮文字颜色
*/
@property(nonatomic,strong) UIColor *cancelButtonColor UI_APPEARANCE_SELECTOR;
/**
* 分割线颜色
*/
@property(nonatomic,strong) UIColor *separatorColor UI_APPEARANCE_SELECTOR;
/**
* 按钮字体
*/
@property(nonatomic,strong) UIFont *buttonFont UI_APPEARANCE_SELECTOR;
/**
* sheet 的宽度,也就是按钮宽度
*/
@property(nonatomic) CGFloat sheetWidth UI_APPEARANCE_SELECTOR;
/**
* 是否让 ActionSheet 背景透明
*/
@property(nonatomic, getter=isBackgroundTransparentEnabled) NSInteger backgroundTransparentEnabled UI_APPEARANCE_SELECTOR;
/**
* 是否点击背景后关闭 ActionSheet
*/
@property(nonatomic, getter=isBackgroundTouchClosureEnabled) NSInteger backgroundTouchClosureEnabled UI_APPEARANCE_SELECTOR;
/**
* 是否启用毛玻璃效果
*/
@property(nonatomic, getter=isBlurEffectEnabled) NSInteger blurEffectEnabled UI_APPEARANCE_SELECTOR;
/**
* 矩形圆角半径
*/
@property(nonatomic,assign) CGFloat rectCornerRadius UI_APPEARANCE_SELECTOR;
/**
* ActionSheet 的环境色
*/
@property(nonatomic,strong) UIColor *ambientColor UI_APPEARANCE_SELECTOR;
/**
* 自定义视图
*/
@property(nonatomic,strong,nullable) UIView *customView;
/**
* 动画持续时长
*/
@property(nonatomic,assign) NSTimeInterval animationDuration UI_APPEARANCE_SELECTOR;
/**
* 动画弹簧效果衰弱比例,值为 1 时无摆动,值越接近 0 摆动越大
*/
@property(nonatomic,assign) CGFloat animationDampingRatio UI_APPEARANCE_SELECTOR;
/**
* 动画弹簧效果初速度。如果动画总距离为 200 点,想让初速度为每秒 100 点,那么将值设为 0.5
*/
@property(nonatomic,assign) CGFloat animationVelocity UI_APPEARANCE_SELECTOR;
/**
* 支持的朝向
*/
@property(nonatomic,assign) UIInterfaceOrientationMask supportedInterfaceOrientations UI_APPEARANCE_SELECTOR;
/**
* 设置布局
*/
- (void)setupLayout;
/**
* 设置毛玻璃效果、圆角、背景颜色等风格
*/
- (void)setupStyle;
/**
* 设置容器 frame
*/
- (void)setupContainerFrame;
@end
```
[GitHub wiki](https://github.com/yulingtianxia/TBActionSheet/wiki) can help you master advanced usage.

There is also an example project for `TBActionSheet`.

Expand All @@ -291,8 +150,6 @@ TBAlertAction *cancel = [TBAlertAction actionWithTitle:@"取消" style: TBAlertA
[self presentViewController:controller animated:YES completion:nil];
```



For more infomation about `TBAlertController`, please visit [this post](http://yulingtianxia.com/blog/2015/11/13/Summary-of-the-first-month-in-the-internship-of-Tencent/) of my blog.

## ❤️ Contributed
Expand Down
24 changes: 22 additions & 2 deletions Source/TBActionSheet/TBActionButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,40 @@ typedef NS_ENUM(NSInteger, TBActionButtonStyle) {
TBActionButtonStyleDestructive,
};


/**
自定义动画 Block
@param background 全屏背景视图,可用做阴影渐变等动画
@param container ActionSheet 容器视图,可设置 frame 等做动画。做 show 动画时,建议调用 updateContainerFrame 方法来设定动画的结束 frame(而不是手动随便设个 frame)。
@param ^completion 动画结束的回调 block,一定要调用。
*/
typedef void(^TBActionSheetAnimation)(UIImageView *background, UIView *container, void(^completion)(void));
@class TBActionButton;
typedef void(^TBActionButtonHandler)(TBActionButton * button);

@interface TBActionButton : UIButton

@property (nonatomic, nullable) UIColor *normalColor;
@property (nonatomic, nullable) UIColor *highlightedColor;
@property (nonatomic) TBActionButtonStyle style;
@property (nonatomic, nullable, strong, readonly) void (^handler)(TBActionButton * button);
/**
点击按钮后的响应回调
*/
@property (nonatomic, nullable, strong, readonly) TBActionButtonHandler handler;
/**
点击按钮后 TBActionSheet 的自定义关闭动画
*/
@property (nonatomic, nullable, strong) TBActionSheetAnimation animation;
/**
* 位于按钮后面的调节颜色的图层,在没有 `normalColor` 或 `highlightedColor` 时使用 ambientColor 替代
*/
@property (weak,nonatomic) UIView *behindColorView;
@property (nonatomic) CGFloat height;

+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style;
+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style handler:(nullable void (^)(TBActionButton * button))handler;
+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style handler:(nullable TBActionButtonHandler)handler;
+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style handler:(nullable TBActionButtonHandler)handler animation:(nullable TBActionSheetAnimation)animation;

@end

Expand Down
10 changes: 8 additions & 2 deletions Source/TBActionSheet/TBActionButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,20 @@ @implementation TBActionButton

+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style
{
return [TBActionButton buttonWithTitle:title style:style handler:nil];
return [self buttonWithTitle:title style:style handler:nil];
}

+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style handler:(nullable void (^)(TBActionButton * button))handler
+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style handler:(nullable TBActionButtonHandler)handler
{
return [self buttonWithTitle:title style:style handler:handler animation:nil];
}

+ (instancetype)buttonWithTitle:(NSString *)title style:(TBActionButtonStyle)style handler:(nullable TBActionButtonHandler)handler animation:(nullable TBActionSheetAnimation)animation
{
TBActionButton *button = [TBActionButton buttonWithType:UIButtonTypeCustom];
button.style = style;
button.handler = handler;
button.animation = animation;
button.clipsToBounds = YES;
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
Expand Down
Loading

0 comments on commit 042847a

Please sign in to comment.