Nav apraksta

FKAlertView.m 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // FKAlertView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/3/14.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKAlertView.h"
  9. @interface FKAlertView ()
  10. @property (nonatomic, strong) UIImage *image;
  11. @property (nonatomic, strong) NSString *title;
  12. @property (nonatomic, strong) NSString *message;
  13. @property (nonatomic, strong) NSString *cancelTitle;
  14. @property (nonatomic, strong) NSString *doneTitle;
  15. @property (nonatomic, strong) UIImageView *imageView;
  16. @property (nonatomic, strong) UIView *coverView;
  17. @property (nonatomic, strong) UIView *contentView;
  18. @property (nonatomic, strong) UILabel *titleLabel;
  19. @property (nonatomic, strong) UILabel *messageLabel;
  20. @property (nonatomic, strong) UIButton *cancelBtn;
  21. @property (nonatomic, strong) UIButton *doneBtn;
  22. @property (nonatomic, weak) id <FKAlertViewDelegate> delegate;
  23. @property (nonatomic, strong) NSDictionary *userInfo;
  24. @end
  25. @implementation FKAlertView
  26. - (instancetype)initWithFrame:(CGRect)frame{
  27. if (self = [super initWithFrame:frame]){
  28. }
  29. return self;
  30. }
  31. - (instancetype)initWithMessage:(NSString *)message
  32. delegate:(id <FKAlertViewDelegate>)delegate
  33. cancelButtonTitle:(NSString *)cancelTitle
  34. doneButtonTitle:(NSString *)doneTitle
  35. info:(NSDictionary *)info{
  36. return [self initWithImage:nil
  37. title:nil
  38. message:message
  39. delegate:delegate
  40. cancelButtonTitle:cancelTitle
  41. doneButtonTitle:doneTitle
  42. info:info];
  43. }
  44. - (instancetype)initWithImage:(UIImage *)image
  45. title:(NSString *)title
  46. message:(NSString *)message
  47. delegate:(id<FKAlertViewDelegate>)delegate
  48. cancelButtonTitle:(NSString *)cancelTitle
  49. doneButtonTitle:(NSString *)doneTitle
  50. info:(NSDictionary *)info{
  51. if (self = [super init]) {
  52. self.image = image;
  53. self.title = title;
  54. self.message = message;
  55. self.cancelTitle = cancelTitle;
  56. self.doneTitle = doneTitle;
  57. self.delegate = delegate;
  58. self.userInfo = info;
  59. [self _initialize];
  60. }
  61. return self;
  62. }
  63. - (void)_initialize{
  64. UIView *vertLine = ({
  65. UIView *view = [[UIView alloc]init];
  66. view.backgroundColor = UIColorFromRGB(0xe5e5e5);
  67. view;
  68. });
  69. UIView *horLine = ({
  70. UIView *view = [[UIView alloc]init];
  71. view.backgroundColor = UIColorFromRGB(0xe5e5e5);
  72. view;
  73. });
  74. BOOL showTitleArea = YES;
  75. CGFloat contentH = 150.0f;
  76. if (!self.image || !self.title){
  77. showTitleArea = NO;
  78. contentH = 120.0f;
  79. }
  80. [self addSubview:self.coverView];
  81. [self addSubview:self.contentView];
  82. [self.contentView addSubview:vertLine];
  83. [self.contentView addSubview:horLine];
  84. [self.contentView addSubview:self.messageLabel];
  85. [self.contentView addSubview:self.cancelBtn];
  86. [self.contentView addSubview:self.doneBtn];
  87. if (showTitleArea){
  88. [self.contentView addSubview:self.imageView];
  89. [self.contentView addSubview:self.titleLabel];
  90. }
  91. [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.edges.insets(UIEdgeInsetsZero);
  93. }];
  94. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  95. make.top.equalTo(self).offset(150);
  96. make.centerX.equalTo(self);
  97. make.size.mas_equalTo(CGSizeMake(250, contentH));
  98. }];
  99. if (showTitleArea){
  100. [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
  101. make.top.equalTo(self.contentView).offset(20);
  102. make.left.equalTo(self.contentView).offset(49);
  103. make.width.height.mas_equalTo(35);
  104. }];
  105. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  106. make.left.equalTo(self.imageView.mas_right).offset(15);
  107. make.centerY.equalTo(self.imageView);
  108. }];
  109. }
  110. [horLine mas_makeConstraints:^(MASConstraintMaker *make) {
  111. make.left.right.equalTo(self.contentView);
  112. make.bottom.equalTo(self.contentView).offset(- 50);
  113. make.height.mas_equalTo(0.5);
  114. }];
  115. [vertLine mas_makeConstraints:^(MASConstraintMaker *make) {
  116. make.top.equalTo(horLine.mas_bottom);
  117. make.centerX.bottom.equalTo(self.contentView);
  118. make.width.mas_equalTo(0.5);
  119. }];
  120. [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  121. if (showTitleArea){
  122. make.top.equalTo(self.imageView.mas_bottom);
  123. }else{
  124. make.top.equalTo(self.contentView);
  125. }
  126. make.bottom.equalTo(horLine.mas_top);
  127. make.centerX.equalTo(self.contentView);
  128. }];
  129. [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  130. make.left.bottom.equalTo(self.contentView);
  131. make.top.equalTo(horLine.mas_bottom);
  132. make.right.equalTo(vertLine.mas_left);
  133. }];
  134. [self.doneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  135. make.right.bottom.equalTo(self.contentView);
  136. make.left.equalTo(vertLine.mas_right);
  137. make.top.equalTo(horLine.mas_bottom);
  138. }];
  139. }
  140. - (void)showInView:(UIView *)view{
  141. if (!view) return;
  142. self.coverView.backgroundColor = [UIColor clearColor];
  143. self.contentView.alpha = 0.0f;
  144. [view addSubview:self];
  145. [self mas_makeConstraints:^(MASConstraintMaker *make) {
  146. make.edges.insets(UIEdgeInsetsZero);
  147. }];
  148. [self setNeedsLayout];
  149. [self layoutIfNeeded];
  150. WeakSelf(weakSelf);
  151. [UIView animateWithDuration:0.15f animations:^{
  152. weakSelf.coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  153. weakSelf.contentView.alpha = 1.0f;
  154. } completion:nil];
  155. }
  156. - (void)dismiss{
  157. [self removeFromSuperview];
  158. }
  159. #pragma mark - action
  160. - (void)clickDoneBtn:(UIButton *)sender{
  161. if ([self.delegate respondsToSelector:@selector(fk_alertView:doneWithConfirm:info:)]){
  162. [self.delegate fk_alertView:self
  163. doneWithConfirm:YES
  164. info:self.userInfo];
  165. }
  166. [self dismiss];
  167. }
  168. - (void)clickCancelBtn:(UIButton *)sender{
  169. if ([self.delegate respondsToSelector:@selector(fk_alertView:doneWithConfirm:info:)]){
  170. [self.delegate fk_alertView:self
  171. doneWithConfirm:NO
  172. info:self.userInfo];
  173. }
  174. [self dismiss];
  175. }
  176. #pragma mark - property
  177. - (UIView *)coverView{
  178. if (_coverView == nil) {
  179. _coverView = [[UIView alloc]init];
  180. _coverView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
  181. }
  182. return _coverView;
  183. }
  184. - (UIView *)contentView {
  185. if (_contentView == nil) {
  186. _contentView = [[UIView alloc]init];
  187. _contentView.backgroundColor = UIColorFromRGB(0xffffff);
  188. _contentView.layer.cornerRadius = 8;
  189. }
  190. return _contentView;
  191. }
  192. - (UIImageView *)imageView{
  193. if (_imageView == nil) {
  194. _imageView = [[UIImageView alloc]initWithImage:self.image];
  195. }
  196. return _imageView;
  197. }
  198. - (UILabel *)titleLabel{
  199. if (_titleLabel == nil) {
  200. _titleLabel = [[UILabel alloc]init];
  201. _titleLabel.font = [UIFont systemFontOfSize:27];
  202. _titleLabel.textColor = UIColorFromRGB(0xff6362);
  203. _titleLabel.numberOfLines = 1;
  204. _titleLabel.text = self.title;
  205. }
  206. return _titleLabel;
  207. }
  208. - (UILabel *)messageLabel{
  209. if (_messageLabel == nil) {
  210. _messageLabel = [[UILabel alloc]init];
  211. _messageLabel.font = [UIFont systemFontOfSize:15];
  212. _messageLabel.textColor = UIColorFromRGB(0x333333);
  213. _messageLabel.numberOfLines = 1;
  214. _messageLabel.textAlignment = NSTextAlignmentCenter;
  215. _messageLabel.text = self.message;
  216. }
  217. return _messageLabel;
  218. }
  219. - (UIButton *)cancelBtn{
  220. if (_cancelBtn == nil) {
  221. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  222. _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  223. [_cancelBtn setTitleColor:UIColorFromRGB(0x999999) forState:UIControlStateNormal];
  224. [_cancelBtn addTarget:self action:@selector(clickCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
  225. [_cancelBtn setTitle:self.cancelTitle forState:UIControlStateNormal];
  226. }
  227. return _cancelBtn;
  228. }
  229. - (UIButton *)doneBtn{
  230. if (_doneBtn == nil) {
  231. _doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  232. _doneBtn.titleLabel.font = [UIFont systemFontOfSize:15];
  233. [_doneBtn setTitleColor:UIColorFromRGB(0xff624a) forState:UIControlStateNormal];
  234. [_doneBtn addTarget:self action:@selector(clickDoneBtn:) forControlEvents:UIControlEventTouchUpInside];
  235. [_doneBtn setTitle:self.doneTitle forState:UIControlStateNormal];
  236. }
  237. return _doneBtn;
  238. }
  239. @end