CustomAlertView.m 7.0 KB

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