口袋优选

UIView+TYAlertView.m 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. // UIView+TYAlertView.m
  3. // TYAlertControllerDemo
  4. //
  5. // Created by tanyang on 15/9/7.
  6. // Copyright (c) 2015年 tanyang. All rights reserved.
  7. //
  8. #import "UIView+TYAlertView.h"
  9. @implementation UIView (TYAlertView)
  10. + (instancetype)createViewFromNibName:(NSString *)nibName
  11. {
  12. NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil];
  13. return [nib objectAtIndex:0];
  14. }
  15. + (instancetype)createViewFromNib
  16. {
  17. return [self createViewFromNibName:NSStringFromClass(self.class)];
  18. }
  19. - (UIViewController*)viewController
  20. {
  21. for (UIView* next = [self superview]; next; next = next.superview) {
  22. UIResponder* nextResponder = [next nextResponder];
  23. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  24. return (UIViewController*)nextResponder;
  25. }
  26. }
  27. return nil;
  28. }
  29. #pragma mark - show in window
  30. - (void)showInWindow
  31. {
  32. [self showInWindowWithBackgoundTapDismissEnable:NO];
  33. }
  34. - (void)showInWindowWithBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  35. {
  36. if (self.superview) {
  37. [self removeFromSuperview];
  38. }
  39. [TYShowAlertView showAlertViewWithView:self backgoundTapDismissEnable:backgoundTapDismissEnable];
  40. }
  41. - (void)showInWindowWithOriginY:(CGFloat)OriginY
  42. {
  43. [self showInWindowWithOriginY:OriginY backgoundTapDismissEnable:NO];
  44. }
  45. - (void)showInWindowWithOriginY:(CGFloat)OriginY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  46. {
  47. if (self.superview) {
  48. [self removeFromSuperview];
  49. }
  50. [TYShowAlertView showAlertViewWithView:self originY:OriginY backgoundTapDismissEnable:backgoundTapDismissEnable];
  51. }
  52. - (void)hideInWindow
  53. {
  54. if ([self isShowInWindow]) {
  55. [(TYShowAlertView *)self.superview hide];
  56. }else {
  57. NSLog(@"self.superview is nil, or isn't TYShowAlertView");
  58. }
  59. }
  60. #pragma mark - show in controller
  61. - (void)showInController:(UIViewController *)viewController
  62. {
  63. [self showInController:viewController preferredStyle:TYAlertControllerStyleAlert transitionAnimation:TYAlertTransitionAnimationFade];
  64. }
  65. - (void)showInController:(UIViewController *)viewController preferredStyle:(TYAlertControllerStyle)preferredStyle
  66. {
  67. [self showInController:viewController preferredStyle:preferredStyle transitionAnimation:TYAlertTransitionAnimationFade];
  68. }
  69. - (void)showInController:(UIViewController *)viewController preferredStyle:(TYAlertControllerStyle)preferredStyle backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  70. {
  71. [self showInController:viewController preferredStyle:preferredStyle transitionAnimation:TYAlertTransitionAnimationFade backgoundTapDismissEnable:backgoundTapDismissEnable];
  72. }
  73. - (void)showInController:(UIViewController *)viewController preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation
  74. {
  75. [self showInController:viewController preferredStyle:preferredStyle transitionAnimation:transitionAnimation backgoundTapDismissEnable:NO];
  76. }
  77. - (void)showInController:(UIViewController *)viewController preferredStyle:(TYAlertControllerStyle)preferredStyle transitionAnimation:(TYAlertTransitionAnimation)transitionAnimation backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  78. {
  79. if (self.superview) {
  80. [self removeFromSuperview];
  81. }
  82. TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:self preferredStyle:preferredStyle transitionAnimation:transitionAnimation];
  83. alertController.backgoundTapDismissEnable = backgoundTapDismissEnable;
  84. [viewController presentViewController:alertController animated:YES completion:nil];
  85. }
  86. - (void)hideInController
  87. {
  88. if ([self isShowInAlertController]) {
  89. [(TYAlertController *)self.viewController dismissViewControllerAnimated:YES];
  90. }else {
  91. NSLog(@"self.viewController is nil, or isn't TYAlertController");
  92. }
  93. }
  94. #pragma mark - hide
  95. - (BOOL)isShowInAlertController
  96. {
  97. UIViewController *viewController = self.viewController;
  98. if (viewController && [viewController isKindOfClass:[TYAlertController class]]) {
  99. return YES;
  100. }
  101. return NO;
  102. }
  103. - (BOOL)isShowInWindow
  104. {
  105. if (self.superview && [self.superview isKindOfClass:[TYShowAlertView class]]) {
  106. return YES;
  107. }
  108. return NO;
  109. }
  110. - (void)hideView
  111. {
  112. if ([self isShowInAlertController]) {
  113. [self hideInController];
  114. }else if ([self isShowInWindow]) {
  115. [self hideInWindow];
  116. }else {
  117. NSLog(@"self.viewController is nil, or isn't TYAlertController,or self.superview is nil, or isn't TYShowAlertView");
  118. }
  119. }
  120. @end