CustomAlertView.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. [_btnCancelSingle setHidden:YES];
  48. [_btnConfirm setHidden:NO];
  49. [_btnCancel setHidden:NO];
  50. if (_cancelButtonTitle && ![_cancelButtonTitle isEmptyString]) {
  51. [_btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  52. [_btnCancel setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_left_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  53. [_btnCancel setTitle:_cancelButtonTitle forState:UIControlStateNormal];
  54. [_btnCancelSingle setBackgroundImage:[UIImage imageNamed:@"img_popup_btn"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  55. [_btnCancelSingle setBackgroundImage:[UIImage imageNamed:@"img_popup_btn_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  56. [_btnCancelSingle setTitle:_cancelButtonTitle forState:UIControlStateNormal];
  57. }else{
  58. [_btnConfirmSingle setHidden:NO];
  59. [_btnConfirm setHidden:YES];
  60. [_btnCancel setHidden:YES];
  61. }
  62. return self;
  63. }
  64. - (void)didMoveToSuperview {
  65. if (!self.superview) {
  66. return;
  67. }
  68. if (!_cancelButtonTitle) {
  69. if (_btnCancel && [_btnCancel superview]) {
  70. [_containerView removeConstraint:_constraintBtnSameWidth];
  71. [_btnCancel removeFromSuperview];
  72. CGFloat constant = 15.0f;
  73. if (IPHONE_WIDTH == 414.0f) {//아이폰 6p일 경우,
  74. constant = 15 + 20.0f;
  75. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6일경우
  76. constant = 15 + 12.0f;
  77. }
  78. _constraintBtnConfirmWidth.constant = (IPHONE_WIDTH - (constant*2));
  79. _btnCancel = nil;
  80. }
  81. }
  82. }
  83. - (void)hideWithButtonIndex:(NSInteger)index {
  84. [self alertView:self clickedButtonAtIndex:index];
  85. [self hide];
  86. }
  87. - (IBAction)btnConfirmTouched:(id)sender {
  88. [self alertView:self clickedButtonAtIndex:0];
  89. [self hide];
  90. }
  91. - (IBAction)btnConfirmSingleTouched:(id)sender {
  92. [self alertView:self clickedButtonAtIndex:0];
  93. [self hide];
  94. }
  95. - (IBAction)btnCancelTouched:(id)sender {
  96. [self alertView:self clickedButtonAtIndex:1];
  97. [self hide];
  98. }
  99. - (IBAction)btnCancelSingleTouched:(id)sender {
  100. [self alertView:self clickedButtonAtIndex:1];
  101. [self hide];
  102. }
  103. - (void)hide {//just hide
  104. [self removeFromSuperview];
  105. UIView *topview = [CommonUtil topView];
  106. for (UIView *subview in [topview.subviews reverseObjectEnumerator]) {
  107. if ([subview isKindOfClass:[CustomAlertView class]]) {
  108. [UIView animateWithDuration:kfAnimationDur animations:^{
  109. subview.alpha = 1.0f;
  110. }];
  111. break;
  112. }
  113. }
  114. }
  115. - (void)show {
  116. if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
  117. return;
  118. }
  119. //얼럿이 뜨는 경우, 로딩창을 종료함.
  120. UIView *topview = [CommonUtil topView];
  121. if (!topview) {
  122. [self performSelector:@selector(show) withObject:nil afterDelay:0.3f];
  123. return;
  124. }
  125. for (UIView *subview in [topview.subviews reverseObjectEnumerator]) {
  126. if ([subview isKindOfClass:[CustomAlertView class]]) {
  127. [UIView animateWithDuration:kfAnimationDur animations:^{
  128. subview.alpha = 0.0f;
  129. }];
  130. break;
  131. }
  132. }
  133. [topview addSubview:self];
  134. [topview bringSubviewToFront:self];
  135. // NSLog(@"%s\n %@, %zd : 0=active, 1=inactive, 2=background", __PRETTY_FUNCTION__, topView, [UIApplication sharedApplication].applicationState);
  136. }
  137. - (CGSize)sizeForMessage:(CGFloat)margin {
  138. if ([_lblMessage1.text isEqualToString:ksEmptyString]) {
  139. return CGSizeZero;
  140. }
  141. NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
  142. CGSize labelSize = (CGSize){IPHONE_WIDTH - (margin * 2) - 40, FLT_MAX};
  143. CGRect r = [_lblMessage1.text boundingRectWithSize:labelSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:_lblMessage1.font} context:context];
  144. r = CGRectIntegral(r);
  145. return r.size;
  146. }
  147. - (void)layoutSubviews {
  148. [super layoutSubviews];
  149. CGRect screenRect = [UIScreen mainScreen].bounds;
  150. self.frame = screenRect;
  151. if (_maskingView.superview) {
  152. [_maskingView mas_makeConstraints:^(MASConstraintMaker *make) {
  153. make.width.mas_equalTo(screenRect.size.width);
  154. make.height.mas_equalTo(screenRect.size.height);
  155. make.center.equalTo(self);
  156. }];
  157. }
  158. if (_popUpView.superview) {
  159. // CGFloat constant = 15.0f;
  160. // if (IPHONE_WIDTH == 414.0f) {//아이폰 6p일 경우,
  161. // constant = 15 + 20.0f;
  162. // } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6일경우
  163. // constant = 15 + 12.0f;
  164. // }
  165. //
  166. // CGFloat adjustHeight = 0.0f;
  167. // if ([self isMemberOfClass:[CustomAlertView class]]) {
  168. // CGFloat height = [self sizeForMessage:constant].height;
  169. // adjustHeight = height > 17 ? height - 17 : 0;
  170. //
  171. // _constraintPopupHeight.constant = 199 + adjustHeight;
  172. // }
  173. // CGFloat pheight = _constraintPopupHeight ? _constraintPopupHeight.constant : _popUpView.frame.size.height;
  174. [_popUpView mas_makeConstraints:^(MASConstraintMaker *make) {
  175. // make.leading.equalTo(_maskingView).offset(constant); //remove at build time
  176. // make.trailing.equalTo(_maskingView).offset(-constant);
  177. CGFloat margin = ceil(screenRect.size.height - _popUpView.frame.size.height) / 2;
  178. make.top.mas_equalTo(margin);
  179. make.bottom.mas_equalTo(-margin);
  180. }];
  181. [self layoutIfNeeded];
  182. }
  183. }
  184. - (void)showWithCompletion:(CustomAlertViewCallBackHandler)completion {
  185. //set runtime accosiation of object
  186. //param - sourse object for association, association key, association value, policy of association
  187. objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  188. // objc_setAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey), completion, OBJC_ASSOCIATION_COPY_NONATOMIC);
  189. [self show];
  190. }
  191. - (void)alertView:(CustomAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  192. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  193. if (completionHandler != nil) {
  194. completionHandler(alertView, buttonIndex);
  195. }
  196. }
  197. - (void)alertView:(CustomAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  198. CustomAlertViewCallBackHandler completionHandler = objc_getAssociatedObject(self, (__bridge const void *)(CustomAlertViewHandlerRunTimeAccosiationKey));
  199. if (completionHandler != nil) {
  200. completionHandler(alertView, buttonIndex);
  201. }
  202. }
  203. @end