AUActionSheet 迁移自 APActionSheet,样式稍有调整,支持普通带删除按钮外观以及普通选项按钮外观。

效果图

  • 带删除按钮:

  • 选项卡按钮:

  • 红点:

依赖

AUActionSheet 的依赖如下:

    AntUI(iOS)
    1.0.0.161108003457
    APCommonUI(iOS)
    1.2.0.161108102201

接口说明

typedef NS_ENUM(NSInteger, AUActionSheetButtonType) {
        AUActionSheetButtonTypeDefault = 0,         // 默认
        AUActionSheetButtonTypeDisabled,            // 不可点击
        AUActionSheetButtonTypeDestructive,         // 红色删除性按钮
        AUActionSheetButtonTypeCustom               // 自定义
};

/**
 AUAction Sheet,接口迁移自 APActionSheet,展示样式做了一些调整
 */
@interface AUActionSheet : UIView<UIAppearanceContainer>

/// 按钮高度 默认为 42
@property (nonatomic) CGFloat buttonHeight UI_APPEARANCE_SELECTOR;
/// 取消按钮高度
@property (nonatomic) CGFloat cancelButtonHeight UI_APPEARANCE_SELECTOR;
/// 分割线颜色 默认为 AU_COLOR_LINE
@property (strong, nonatomic) UIColor *separatorColor UI_APPEARANCE_SELECTOR;
/// 按钮点击背景色
@property (strong, nonatomic) UIColor *selectedBackgroundColor UI_APPEARANCE_SELECTOR;
// UI 组件的一些 Attributes
@property (copy, nonatomic) NSDictionary *titleTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *buttonTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *disabledButtonTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *destructiveButtonTextAttributes UI_APPEARANCE_SELECTOR;
@property (copy, nonatomic) NSDictionary *cancelButtonTextAttributes UI_APPEARANCE_SELECTOR;

/// 显示隐藏动画时间,默认为 0.5
@property (nonatomic) NSTimeInterval animationDuration UI_APPEARANCE_SELECTOR;
/// 标题
@property(nonatomic,copy) NSString *title;
/// 是否已经展示
@property(nonatomic, readonly, getter=isVisible) BOOL visible;
/// 自定义按钮上方顶部视图
@property (strong, nonatomic) UIView *headerView;
/// ActionSheet 实例显示前的 keyWindow
@property (weak, nonatomic, readonly) UIWindow *previousKeyWindow;
/// 协议代理
@property(nonatomic,weak)id<UIActionSheetDelegate> delegate;
/// 取消按钮标题
@property (copy, nonatomic) NSString *cancelButtonTitle;
/// 按钮个数
@property(nonatomic, readonly) NSInteger numberOfButtons;
/// 取消按钮索引,默认为 -1
@property(nonatomic) NSInteger cancelButtonIndex;
/// 破坏性红色按钮索引值,默认为 -1,如果只有一个按钮则忽略。
@property(nonatomic) NSInteger destructiveButtonIndex;
/**
 AUActionSheet 初始化方法

 @param title 标题信息
 @param delegate 代理对象
 @param cancelButtonTitle 取消按钮标题
 @param destructiveButtonTitle 破坏性按钮标题
 @param otherButtonTitles 其他按钮标题参数列表
 @return AUActionSheet 实例
 */
- (instancetype)initWithTitle:(NSString *)title delegate:(id<UIActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

    /**
 AUActionSheet 初始化方法

 @param title 标题信息
 @param delegate 代理对象
 @param cancelButtonTitle 取消按钮标题
 @param destructiveButtonTitle 破坏性按钮标题
 @param items customOption 数据列表(自定义 titleColor,红点)
 @return AUActionSheet 实例
 */
- (instancetype)initWithTitle:(NSString *)title
                     delegate:(id<UIActionSheetDelegate>)delegate
            cancelButtonTitle:(NSString *)cancelButtonTitle
       destructiveButtonTitle:(NSString *)destructiveButtonTitle
                        items:(NSArray<AUActionSheetItem *> *)items;

/**
 增加一个按钮, 类型为默认类型

 @param title 按钮标题
 @return 按钮索引值,从 0 起
 */
- (NSInteger)addButtonWithTitle:(NSString *)title;

/**
 增加一个按钮

 @param title 按钮标题
 @param type 按钮类型
 @return 按钮索引值,从 0 起
 */
- (NSInteger)addButtonWithTitle:(NSString *)title type:(AUActionSheetButtonType)type;

/**
 通过索引值获取按钮标题

 @param buttonIndex 按钮索引值
 @return 按钮标题
 */
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

/**
 设置某个位置的按钮

 @param item 封装信息后的按钮类型
 @param index 需要替换的索引值,小于现有按钮个数
 */
- (void)setButton:(AUActionSheetItem *)item atIndex:(NSInteger)index;

/** ActionSheet 展示方法 */
- (void)show;

/**
 手动调用隐藏方法

 @param animate 隐藏是否带动画
 */
- (void)closeWithAnimate:(BOOL)animate;

/**
 手动模拟按钮点击隐藏方法(会回调按钮点击相关的协议方法)

 @param buttonIndex 按钮索引
 @param animated 隐藏是否带动画
 */
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

/**
 *  动态添加 item
 *  注意:请在 actionSheet show 出来后并显示在屏幕时调用;如需在 show 前 addButton,请使用 addButtonWithTitle
 *
 *  @param item  自定义 item
 *  @param index 添加的位置
 */
- (void)addButton:(AUActionSheetItem *)item atIndex:(NSInteger)index;

// 设置后台模式,如果为 YES 或者 @(YES) 则隐藏所有已经展示的 ActionSheet。默认为 NO
+(void)setIsBackGroundMode:(BOOL)isBackGroundMode;
+(void)weakSetIsBackGroundMode:(id)isBackGroundMode;

- (void)showFromToolbar:(UIToolbar *)view;
- (void)showFromTabBar:(UITabBar *)view;
- (void)showFromBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated NS_AVAILABLE_IOS(3_2);
- (void)showInView:(UIView *)view;

@end


/** 封装后的 ActionSheet 按钮类 */
@interface AUActionSheetItem : NSObject
/// 按钮标题
@property (copy, nonatomic) NSString *title;
/// 按钮的类型
@property (nonatomic) AUActionSheetButtonType type;
/// 按钮标题颜色,如果设置该值,请手动将按钮类型调整为 AUActionSheetButtonTypeCustom
@property (strong,nonatomic) UIColor *titleColor;

    /**
 * 设置显示“红点”样式
 *
 *        badgeValue:  @"."   显示红点
 *                     @"new" 显示 new
 *                     @"数字" 显示数字,大于 99 则显示图片 more(...)
 *                     @"惠"/"hui"  显示“惠”字
 *                     @"xin" 显示"新"字
 *                     nil    清除当前显示
 */
@property (nonatomic, copy) NSString *badgeValue;

@end

代码示例

  • 带删除按钮:

    AUActionSheet *actionSheet = [[AUActionSheet alloc] initWithTitle:@"这是提供一行或二行注释, 通过信息澄清的方式避免用户产生疑问"
                                                               delegate:self
                                                      cancelButtonTitle:@"取消"
                                                 destructiveButtonTitle:@"确认删除"
                                                      otherButtonTitles:nil];
    [actionSheet show];
  • 选项卡按钮:

    AUActionSheet *actionSheet = [[AUActionSheet alloc] initWithTitle:nil
                                                               delegate:self
                                                      cancelButtonTitle:@"取消"
                                                 destructiveButtonTitle:nil
                                                      otherButtonTitles:@"选项一",@"选项二",@"选项三", nil];
    [actionSheet show];
  • 设置某个选项添加红点:

      AUActionSheet *actionSheet = [[AUActionSheet alloc] initWithTitle:nil
                                                               delegate:self
                                                      cancelButtonTitle:@"取消"
                                                 destructiveButtonTitle:nil
                                                      otherButtonTitles:@"选项一",@"选项二",@"选项三", nil];
      AUActionSheetItem *item = [[AUActionSheetItem alloc] init];
      item.title = @"选项三";
      item.type = AUActionSheetButtonTypeCustom;
      item.badgeValue = @"new";
      item.titleColor = [UIColor redColor];
      [actionSheet setButton:item atIndex:2];
    
      [actionSheet show];