猎豆优选

UIView+TYAlertView.m 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. alertController.modalPresentationStyle = UIModalPresentationFullScreen;
  85. [viewController presentViewController:alertController animated:YES completion:nil];
  86. }
  87. - (void)hideInController
  88. {
  89. if ([self isShowInAlertController]) {
  90. [(TYAlertController *)self.viewController dismissViewControllerAnimated:YES];
  91. }else {
  92. NSLog(@"self.viewController is nil, or isn't TYAlertController");
  93. }
  94. }
  95. #pragma mark - hide
  96. - (BOOL)isShowInAlertController
  97. {
  98. UIViewController *viewController = self.viewController;
  99. if (viewController && [viewController isKindOfClass:[TYAlertController class]]) {
  100. return YES;
  101. }
  102. return NO;
  103. }
  104. - (BOOL)isShowInWindow
  105. {
  106. if (self.superview && [self.superview isKindOfClass:[TYShowAlertView class]]) {
  107. return YES;
  108. }
  109. return NO;
  110. }
  111. - (void)hideView
  112. {
  113. if ([self isShowInAlertController]) {
  114. [self hideInController];
  115. }else if ([self isShowInWindow]) {
  116. [self hideInWindow];
  117. }else {
  118. NSLog(@"self.viewController is nil, or isn't TYAlertController,or self.superview is nil, or isn't TYShowAlertView");
  119. }
  120. }
  121. @end