123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // 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
|