// // UIView+ViewController.m // Created by MacKun on 15/4/25. // Copyright (c) 2015年 MacKun All rights reserved. // #import "UIView+ViewController.h" @implementation UIView (ViewController) /** * @brief 找到当前view所在的viewcontroler */ - (UIViewController *)viewController { UIResponder *responder = self.nextResponder; do { if ([responder isKindOfClass:[UIViewController class]]) { return (UIViewController *)responder; } responder = responder.nextResponder; } while (responder); return nil; } - (void)removeAllSubviews { while (self.subviews.count) { [self.subviews.lastObject removeFromSuperview]; } } - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertPoint:point toWindow:nil]; } else { return [self convertPoint:point toView:nil]; } } UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; if ((!from || !to) || (from == to)) return [self convertPoint:point toView:view]; point = [self convertPoint:point toView:from]; point = [to convertPoint:point fromWindow:from]; point = [view convertPoint:point fromView:to]; return point; } - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertPoint:point fromWindow:nil]; } else { return [self convertPoint:point fromView:nil]; } } UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; if ((!from || !to) || (from == to)) return [self convertPoint:point fromView:view]; point = [from convertPoint:point fromView:view]; point = [to convertPoint:point fromWindow:from]; point = [self convertPoint:point fromView:to]; return point; } - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertRect:rect toWindow:nil]; } else { return [self convertRect:rect toView:nil]; } } UIWindow *from = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; UIWindow *to = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; if (!from || !to) return [self convertRect:rect toView:view]; if (from == to) return [self convertRect:rect toView:view]; rect = [self convertRect:rect toView:from]; rect = [to convertRect:rect fromWindow:from]; rect = [view convertRect:rect fromView:to]; return rect; } - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(UIView *)view { if (!view) { if ([self isKindOfClass:[UIWindow class]]) { return [((UIWindow *)self) convertRect:rect fromWindow:nil]; } else { return [self convertRect:rect fromView:nil]; } } UIWindow *from = [view isKindOfClass:[UIWindow class]] ? (id)view : view.window; UIWindow *to = [self isKindOfClass:[UIWindow class]] ? (id)self : self.window; if ((!from || !to) || (from == to)) return [self convertRect:rect fromView:view]; rect = [from convertRect:rect fromView:view]; rect = [to convertRect:rect fromWindow:from]; rect = [self convertRect:rect fromView:to]; return rect; } - (void)clearScrollViewDelegate { if ([self isKindOfClass:[UIScrollView class]]) { ((UIScrollView *)self).delegate = nil; if ([self isKindOfClass:[UITableView class]]) { ((UITableView *)self).delegate = nil; } } for (UIView *sub in self.subviews) { [sub clearScrollViewDelegate]; } } - (void)removeAllGestures { NSArray *gs = [self.gestureRecognizers copy]; for (UIGestureRecognizer *g in gs) { [self removeGestureRecognizer:g]; } } - (void)removeAllGesturesWithSubViews { [self removeAllGestures]; for (UIView *v in self.subviews) { [v removeAllGesturesWithSubViews]; } [UIView animateWithDuration:0 animations:^{ }]; } + (void)disableAnimationWithBlock:(void (^)(void))block { if (!block) return; BOOL aniEnabled = [CATransaction disableActions]; [CATransaction setDisableActions:YES]; block(); [CATransaction setDisableActions:aniEnabled]; } @end