CustomAlertView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // CustomAlertView.m
  3. // JasonDevelop
  4. //
  5. // Created by Jason Lee on 3/6/14.
  6. // Copyright (c) jasondevelop. All rights reserved.
  7. //
  8. #import <objc/runtime.h>
  9. #import "NSString-Addtions.h"
  10. #import "CustomAlertView.h"
  11. #import "CustomButton.h"
  12. #import "Masonry.h"
  13. #import "ImageUtil.h"
  14. //#import "AppDelegate.h"
  15. #define kfButtonSize 260.0f
  16. #define kfBoxCap 4.0f
  17. //#define kfTopInset 0
  18. //#define kfRightInset 20
  19. //#define kfBottomInset 0
  20. //#define kfLeftInset 20
  21. static NSString *CustomAlertViewHandlerRunTimeAccosiationKey = @"CustomAlertViewHandlerRunTimeAccosiationKey";
  22. @interface CustomAlertView () {
  23. }
  24. @end
  25. @implementation CustomAlertView
  26. //커스텀 얼럿 인스턴스를 생성함, 단순 형태
  27. - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate OKButtonTitle:(NSString *)okButtonTitle cancelButtonTitle:(NSString *)cancelButtonTitle {
  28. NSArray *nibViews = [[NSBundle mainBundle] loadNibNamed:@"CustomAlertView"
  29. owner:nil
  30. options:nil];
  31. self = (CustomAlertView*)[nibViews firstObject];
  32. CGRect screenRect = [UIScreen mainScreen].bounds;
  33. self.frame = screenRect;
  34. //메시지, 버튼 타이틀 설정
  35. _okButtonTitle = okButtonTitle;
  36. _cancelButtonTitle = cancelButtonTitle;
  37. //팝업 이미지 Stretch
  38. [_imgTitleBar setImage:[UIImage imageNamed:@"img_popup_bg_head"]inset:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  39. _lblTitle.text = title;
  40. _lblMessage1.text = message;
  41. [_btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  42. [_btnConfirm setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_right_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  43. [_btnConfirmSingle setBackgroundImage:[UIImage imageNamed:@"img_popup_btn"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  44. [_btnConfirmSingle setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  45. [_btnConfirm setTitle:okButtonTitle forState:UIControlStateNormal];
  46. [_btnConfirmSingle setHidden:YES];
  47. [_btnConfirm setHidden:NO];
  48. [_btnCancel setHidden:NO];
  49. if (_cancelButtonTitle && ![_cancelButtonTitle isEmptyString]) {
  50. [_btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  51. [_btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  52. [_btnCancel setTitle:_cancelButtonTitle forState:UIControlStateNormal];
  53. }else{
  54. [_btnConfirmSingle setHidden:NO];
  55. [_btnConfirm setHidden:YES];
  56. [_btnCancel setHidden:YES];
  57. }
  58. return self;
  59. }
  60. - (void)didMoveToSuperview {
  61. if (!self.superview) {
  62. return;
  63. }
  64. if (!_cancelButtonTitle) {
  65. if (_btnCancel && [_btnCancel superview]) {
  66. [_containerView removeConstraint:_constraintBtnSameWidth];
  67. [_btnCancel removeFromSuperview];
  68. CGFloat constant = 15.0f;
  69. if (IPHONE_WIDTH == 414.0f) {//아이폰 6p일 경우,
  70. constant = 15 + 20.0f;
  71. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6일경우
  72. constant = 15 + 12.0f;
  73. }
  74. _constraintBtnConfirmWidth.constant = (IPHONE_WIDTH - (constant*2));
  75. _btnCancel = nil;
  76. }
  77. }
  78. }
  79. - (IBAction)btnConfirmTouched:(id)sender {
  80. [self alertView:self clickedButtonAtIndex:0];
  81. [self hide];
  82. }
  83. - (IBAction)btnConfirmSingleTouched:(id)sender {
  84. [self alertView:self clickedButtonAtIndex:0];
  85. [self hide];
  86. }
  87. - (IBAction)btnCancelTouched:(id)sender {
  88. [self alertView:self clickedButtonAtIndex:1];
  89. [self hide];
  90. }
  91. - (void)hide {//just hide
  92. [self removeFromSuperview];
  93. UIView *topview = [CommonUtil topView];
  94. for (UIView *subview in [topview.subviews reverseObjectEnumerator]) {
  95. if ([subview isKindOfClass:[CustomAlertView class]]) {
  96. [UIView animateWithDuration:kfAnimationDur animations:^{
  97. subview.alpha = 1.0f;
  98. }];
  99. break;
  100. }
  101. }
  102. }
  103. - (void)show {
  104. if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
  105. return;
  106. }
  107. //얼럿이 뜨는 경우, 로딩창을 종료함.
  108. UIView *topview = [CommonUtil topView];
  109. if (!topview) {
  110. [self performSelector:@selector(show) withObject:nil afterDelay:0.3f];
  111. return;
  112. }
  113. for (UIView *subview in [topview.subviews reverseObjectEnumerator]) {
  114. if ([subview isKindOfClass:[CustomAlertView class]]) {
  115. [UIView animateWithDuration:kfAnimationDur animations:^{
  116. subview.alpha = 0.0f;
  117. }];
  118. break;
  119. }
  120. }
  121. [topview addSubview:self];
  122. [topview bringSubviewToFront:self];
  123. // NSLog(@"%s\n %@, %zd : 0=active, 1=inactive, 2=background", __PRETTY_FUNCTION__, topView, [UIApplication sharedApplication].applicationState);
  124. }
  125. - (CGSize)sizeForMessage:(CGFloat)margin {
  126. if ([_lblMessage1.text isEqualToString:ksEmptyString]) {
  127. return CGSizeZero;
  128. }
  129. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  130. CGSize labelSize = (CGSize){IPHONE_WIDTH - (margin * 2) - 40, FLT_MAX};
  131. CGRect r = [_lblMessage1.text boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_lblMessage1.font} context:context];
  132. r = CGRectIntegral(r);
  133. return r.size;
  134. }
  135. - (void)layoutSubviews {
  136. [super layoutSubviews];
  137. CGRect screenRect = [UIScreen mainScreen].bounds;
  138. self.frame = screenRect;
  139. if (_maskingView.superview) {
  140. [_maskingView mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.width.mas_equalTo(screenRect.size.width);
  142. make.height.mas_equalTo(screenRect.size.height);
  143. make.center.equalTo(self);
  144. }];
  145. }
  146. if (_popUpView.superview) {
  147. // CGFloat constant = 15.0f;
  148. // if (IPHONE_WIDTH == 414.0f) {//아이폰 6p일 경우,
  149. // constant = 15 + 20.0f;
  150. // } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6일경우
  151. // constant = 15 + 12.0f;
  152. // }
  153. //
  154. // CGFloat adjustHeight = 0.0f;
  155. // if ([self isMemberOfClass:[CustomAlertView class]]) {
  156. // CGFloat height = [self sizeForMessage:constant].height;
  157. // adjustHeight = height > 17 ? height - 17 : 0;
  158. //
  159. // _constraintPopupHeight.constant = 199 + adjustHeight;
  160. // }
  161. // CGFloat pheight = _constraintPopupHeight ? _constraintPopupHeight.constant : _popUpView.frame.size.height;
  162. [_popUpView mas_makeConstraints:^(MASConstraintMaker *make) {
  163. // make.leading.equalTo(_maskingView).offset(constant); //remove at build time
  164. // make.trailing.equalTo(_maskingView).offset(-constant);
  165. CGFloat margin = ceil(screenRect.size.height - _popUpView.frame.size.height) / 2;
  166. make.top.mas_equalTo(margin);
  167. make.bottom.mas_equalTo(-margin);
  168. }];
  169. [self layoutIfNeeded];
  170. }
  171. }
  172. - (void)showWithCompletion:(CustomAlertViewCallBackHandler)completion {
  173. //set runtime accosiation of object
  174. //param - sourse object for association, association key, association value, policy of association
  175. objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  176. // objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
  177. [self show];
  178. }
  179. - (void)alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  180. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  181. if (completionHandler != nil) {
  182. completionHandler(alertView, buttonIndex);
  183. }
  184. }
  185. - (void)alertView:(CustomAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  186. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  187. if (completionHandler != nil) {
  188. completionHandler(alertView, buttonIndex);
  189. }
  190. }
  191. @end