SPAlertController.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // SPAlertController.h
  3. // SPAlertController
  4. //
  5. // Created by 乐升平 on 17/10/12. https://github.com/SPStore/SPAlertController
  6. // Copyright © 2017年 iDress. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. NS_ASSUME_NONNULL_BEGIN
  10. typedef NS_ENUM(NSInteger, SPAlertActionStyle) {
  11. SPAlertActionStyleDefault = 0, // 默认样式
  12. SPAlertActionStyleCancel, // 取消样式,只有在SPAlertControllerStyleActionSheet下才有效果
  13. SPAlertActionStyleDestructive // 红色字体样式,SPAlertControllerStyleActionSheet和SPAlertControllerStyleAlert均有效
  14. };
  15. typedef NS_ENUM(NSInteger, SPAlertControllerStyle) {
  16. SPAlertControllerStyleActionSheet = 0, // 从底部或顶部弹出
  17. SPAlertControllerStyleAlert, // 从中间弹出
  18. };
  19. typedef NS_ENUM(NSInteger, SPAlertAnimationType) {
  20. SPAlertAnimationTypeDefault = 0, // 默认动画,如果是actionSheet,默认为SPAlertAnimationTypeRaiseUp,如果是alert,默认为SPAlertAnimationTypeAlpha
  21. SPAlertAnimationTypeRaiseUp, // 从下往上弹,一般用于actionSheet
  22. SPAlertAnimationTypeDropDown, // 从上往下弹,一般用于actionSheet
  23. SPAlertAnimationTypeAlpha, // 透明度从0到1,一般用于alert
  24. SPAlertAnimationTypeExpand, // 发散动画,一般用于alert
  25. SPAlertAnimationTypeShrink // 收缩动画,一般用于alert
  26. };
  27. // ================================ action类 ================================
  28. @interface SPAlertAction : NSObject <NSCopying>
  29. /**
  30. * 创建一个action
  31. *
  32. * @param title 标题
  33. * @param style action样式
  34. * @param handler 点中后的bolok回调
  35. */
  36. + (instancetype)actionWithTitle:(nullable NSString *)title style:(SPAlertActionStyle)style handler:(void (^ __nullable)(SPAlertAction *action))handler;
  37. /* action的标题 */
  38. @property (nullable, nonatomic, readonly) NSString *title;
  39. /* 样式 */
  40. @property (nonatomic, readonly) SPAlertActionStyle style;
  41. /* 是否能点击,默认为YES,当为NO时,action的文字颜色为浅灰色,字体17号,且无法修改 */
  42. @property (nonatomic, getter=isEnabled) BOOL enabled;
  43. /* action的标题颜色 */
  44. @property (nonatomic, strong) UIColor *titleColor;
  45. /* action的标题字体 */
  46. @property (nonatomic, strong) UIFont *titleFont;
  47. @end
  48. // ================================ 控制器类 ================================
  49. @interface SPAlertController : UIViewController
  50. /**
  51. * 创建控制器
  52. *
  53. * @param title 大标题
  54. * @param message 副标题
  55. * @param preferredStyle 样式
  56. * @param animationType 动画类型
  57. */
  58. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType;
  59. /**
  60. * 创建控制器
  61. *
  62. * @param title 大标题
  63. * @param message 副标题
  64. * @param preferredStyle 样式
  65. * @param animationType 动画类型
  66. * @param customView 自定义的view,如果customView传nil,就跟第一个方法等效
  67. */
  68. // 对话框的大小取决于customView的大小
  69. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customView:(nullable UIView *)customView;
  70. /**
  71. * 创建控制器
  72. *
  73. * @param preferredStyle 样式
  74. * @param animationType 动画类型
  75. * @param customTitleView 自定义的titleView,如果customTitleView传nil,就跟第一个方法等效
  76. */
  77. // 如果customTitleView宽度小于等于0,或者大于等于了对话框的宽度,会自动等宽于对话框,除此之外,宽度会按照customTitleView的宽度居中布局
  78. + (instancetype)alertControllerWithPreferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customTitleView:(nullable UIView *)customTitleView;
  79. /**
  80. * 创建控制器
  81. *
  82. * @param title 大标题
  83. * @param message 副标题
  84. * @param preferredStyle 样式
  85. * @param animationType 动画类型
  86. * @param customCenterView 自定义的centerView,如果customCenterView传nil,就跟第一个方法等效
  87. */
  88. // 如果customCenterView宽度小于等于0,或者大于等于了对话框的宽度,会自动等宽于对话框,除此之外,宽度会按照customCenterView的宽度居中布局
  89. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customCenterView:(nullable UIView *)customCenterView;
  90. /**
  91. * 创建控制器
  92. *
  93. * @param title 大标题
  94. * @param message 副标题
  95. * @param preferredStyle 样式
  96. * @param animationType 动画类型
  97. * @param customFooterView 自定义的customFooterView,如果customFooterView传nil,就跟第一个方法等效
  98. */
  99. // 如果customFooterView宽度小于等于0,或者大于等于了对话框的宽度,会自动等宽于对话框,除此之外,宽度会按照customCenterView的宽度居中布局
  100. + (instancetype)alertControllerWithTitle:(nullable NSString *)title message:(nullable NSString *)message preferredStyle:(SPAlertControllerStyle)preferredStyle animationType:(SPAlertAnimationType)animationType customFooterView:(nullable UIView *)customFooterView;
  101. /**
  102. * 添加action
  103. */
  104. - (void)addAction:(SPAlertAction *)action;
  105. /** action数组 */
  106. @property (nonatomic, readonly) NSArray<SPAlertAction *> *actions;
  107. /**
  108. * 添加文本输入框
  109. *
  110. * @param configurationHandler 一旦添加后就会回调一次(仅回调一次,因此可以在这个block块里面自由定制textFiled,如设置textField的属性,设置代理,添加addTarget,监听通知等),只有present后,textField才有superView
  111. */
  112. - (void)addTextFieldWithConfigurationHandler:(void (^ __nullable)(UITextField *textField))configurationHandler;
  113. /** textField的数组 */
  114. @property (nullable, nonatomic, readonly) NSArray<UITextField *> *textFields;
  115. /** 样式 */
  116. @property (nonatomic, readonly) SPAlertControllerStyle preferredStyle;
  117. /** 动画类型 */
  118. @property (nonatomic, readonly) SPAlertAnimationType animationType;
  119. /** 大标题 */
  120. @property (nullable, nonatomic, copy) NSString *title;
  121. /** 副标题 */
  122. @property (nullable, nonatomic, copy) NSString *message;
  123. /** 大标题颜色 */
  124. @property (nonatomic, strong) UIColor *titleColor;
  125. /** 副标题颜色 */
  126. @property (nonatomic, strong) UIColor *messageColor;
  127. /** 大标题字体 */
  128. @property (nonatomic, strong) UIFont *titleFont;
  129. /** 副标题字体 */
  130. @property (nonatomic, strong) UIFont *messageFont;
  131. /** actionSheet样式下,最大的顶部间距,默认为0,iPhoneX下默认44 */
  132. // 如果是从顶部弹出来,该属性则充当底部间距
  133. @property (nonatomic, assign) CGFloat maxTopMarginForActionSheet;
  134. /** alert样式下,四周的最大间距,默认为20 */
  135. @property (nonatomic, assign) CGFloat maxMarginForAlert;
  136. /** alert样式下,圆角半径 */
  137. @property (nonatomic, assign) CGFloat cornerRadiusForAlert;
  138. /** alert样式下,弹窗的中心y值,为正向下偏移,为负向上偏移 */
  139. @property (nonatomic, assign) CGFloat offsetYForAlert;
  140. /** alert样式下,水平排列的最大个数,如果大于了这个数,则所有action将垂直排列;默认是2 */
  141. // 当自定义了footerView时,该属性不起作用
  142. @property (nonatomic, assign) NSInteger maxNumberOfActionHorizontalArrangementForAlert;
  143. /** 是否需要毛玻璃效果,默认为YES */
  144. @property (nonatomic, assign) BOOL needBlur;
  145. @end
  146. @interface SPAlertPresentationController : UIPresentationController
  147. @end
  148. // ================================ 动画类 ================================
  149. @interface SPAlertAnimation : NSObject <UIViewControllerAnimatedTransitioning>
  150. + (instancetype)animationIsPresenting:(BOOL)presenting;
  151. @end
  152. NS_ASSUME_NONNULL_END