UIView+ViewController.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // UIView+ViewController.m
  3. // Created by MacKun on 15/4/25.
  4. // Copyright (c) 2015年 MacKun All rights reserved.
  5. //
  6. #import "UIView+ViewController.h"
  7. @implementation UIView (ViewController)
  8. /**
  9. * @brief 找到当前view所在的viewcontroler
  10. */
  11. - (UIViewController *)viewController
  12. {
  13. UIResponder *responder = self.nextResponder;
  14. do {
  15. if ([responder isKindOfClass:[UIViewController class]]) {
  16. return (UIViewController *)responder;
  17. }
  18. responder = responder.nextResponder;
  19. } while (responder);
  20. return nil;
  21. }
  22. - (void)removeAllSubviews {
  23. while (self.subviews.count) {
  24. [self.subviews.lastObject removeFromSuperview];
  25. }
  26. }
  27. - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(UIView *)view {
  28. if (!view) {
  29. if ([self isKindOfClass:[UIWindow class]]) {
  30. return [((UIWindow *)self) convertPoint:point toWindow:nil];
  31. } else {
  32. return [self convertPoint:point toView:nil];
  33. }
  34. }
  35. UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  36. UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  37. if ((!from || !to) || (from == to)) return [self convertPoint:point toView:view];
  38. point = [self convertPoint:point toView:from];
  39. point = [to convertPoint:point fromWindow:from];
  40. point = [view convertPoint:point fromView:to];
  41. return point;
  42. }
  43. - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(UIView *)view {
  44. if (!view) {
  45. if ([self isKindOfClass:[UIWindow class]]) {
  46. return [((UIWindow *)self) convertPoint:point fromWindow:nil];
  47. } else {
  48. return [self convertPoint:point fromView:nil];
  49. }
  50. }
  51. UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  52. UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  53. if ((!from || !to) || (from == to)) return [self convertPoint:point fromView:view];
  54. point = [from convertPoint:point fromView:view];
  55. point = [to convertPoint:point fromWindow:from];
  56. point = [self convertPoint:point fromView:to];
  57. return point;
  58. }
  59. - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(UIView *)view {
  60. if (!view) {
  61. if ([self isKindOfClass:[UIWindow class]]) {
  62. return [((UIWindow *)self) convertRect:rect toWindow:nil];
  63. } else {
  64. return [self convertRect:rect toView:nil];
  65. }
  66. }
  67. UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  68. UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  69. if (!from || !to) return [self convertRect:rect toView:view];
  70. if (from == to) return [self convertRect:rect toView:view];
  71. rect = [self convertRect:rect toView:from];
  72. rect = [to convertRect:rect fromWindow:from];
  73. rect = [view convertRect:rect fromView:to];
  74. return rect;
  75. }
  76. - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(UIView *)view {
  77. if (!view) {
  78. if ([self isKindOfClass:[UIWindow class]]) {
  79. return [((UIWindow *)self) convertRect:rect fromWindow:nil];
  80. } else {
  81. return [self convertRect:rect fromView:nil];
  82. }
  83. }
  84. UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window;
  85. UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window;
  86. if ((!from || !to) || (from == to)) return [self convertRect:rect fromView:view];
  87. rect = [from convertRect:rect fromView:view];
  88. rect = [to convertRect:rect fromWindow:from];
  89. rect = [self convertRect:rect fromView:to];
  90. return rect;
  91. }
  92. - (void)clearScrollViewDelegate {
  93. if ([self isKindOfClass:[UIScrollView class]]) {
  94. ((UIScrollView *)self).delegate = nil;
  95. if ([self isKindOfClass:[UITableView class]]) {
  96. ((UITableView *)self).delegate = nil;
  97. }
  98. }
  99. for (UIView *sub in self.subviews) {
  100. [sub clearScrollViewDelegate];
  101. }
  102. }
  103. - (void)removeAllGestures {
  104. NSArray *gs = [self.gestureRecognizers copy];
  105. for (UIGestureRecognizer *g in gs) {
  106. [self removeGestureRecognizer:g];
  107. }
  108. }
  109. - (void)removeAllGesturesWithSubViews {
  110. [self removeAllGestures];
  111. for (UIView *v in self.subviews) {
  112. [v removeAllGesturesWithSubViews];
  113. }
  114. [UIView animateWithDuration:0 animations:^{
  115. }];
  116. }
  117. + (void)disableAnimationWithBlock:(void (^)(void))block {
  118. if (!block) return;
  119. BOOL aniEnabled = [CATransaction disableActions];
  120. [CATransaction setDisableActions:YES];
  121. block();
  122. [CATransaction setDisableActions:aniEnabled];
  123. }
  124. @end