美文网首页
UIView生命周期

UIView生命周期

作者: SDBridge | 来源:发表于2017-04-09 10:34 被阅读261次
    //  SKView.m
    //  SKProject01
    //
    //  Created by Code_Hou on 2017/4/6.
    //  Copyright © 2017年 侯森魁. All rights reserved.
    //
    
    #import "SKView.h"
    
    //使用inline函数
    /*
     函数的参数压栈,减少了调用的开销 
     当函数体语句较少(1-5行),且没有复杂的循环语句,且调用次数较多时,就可以用内联函数。
     http://www.cnblogs.com/pengyingh/articles/2405718.html
     */
    static inline void SKLog(SEL _cmd){
    
        NSLog(@"方法名:%@",NSStringFromSelector(_cmd));
    
    };
    @implementation SKView
    
    - (void)removeFromSuperview{
        
        [super removeFromSuperview];
        SKLog(_cmd);
    
    }
    - (void)insertSubview:(UIView *)view atIndex:(NSInteger)index{
        [super insertSubview:view atIndex:index];
        SKLog(_cmd);
    
    }
    - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2{
        
        [super exchangeSubviewAtIndex:index1 withSubviewAtIndex:index2];
        SKLog(_cmd);
    
    }
    
    - (void)addSubview:(UIView *)view{
        [super addSubview:view];
        SKLog(_cmd);
    
    }
    - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview{
        
        [super insertSubview:view belowSubview:siblingSubview];
        SKLog(_cmd);
    
    }
    - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview{
        [super insertSubview:view aboveSubview:siblingSubview];
        SKLog(_cmd);
    
    }
    
    - (void)bringSubviewToFront:(UIView *)view{
        [super bringSubviewToFront:view];
        SKLog(_cmd);
    
    }
    - (void)sendSubviewToBack:(UIView *)view{
        [super sendSubviewToBack:view];
        SKLog(_cmd);
    
    }
    
    - (void)didAddSubview:(UIView *)subview{
        [super didAddSubview:subview];
        SKLog(_cmd);
    
    }
    - (void)willRemoveSubview:(UIView *)subview{
        [super willRemoveSubview:subview];
        SKLog(_cmd);
    
    }
    
    /*
     
     先添加到父视图上面
     再添加到UIWindow上面
     最后调用layoutSubviews
     */
    - (void)willMoveToSuperview:(nullable UIView *)newSuperview{
        [super willMoveToSuperview:newSuperview];
        NSLog(@"newSuperview =%@",newSuperview);
    
        NSLog(@"[[UIApplication sharedApplication].delegate window]; =%@",[[UIApplication sharedApplication].delegate window]);
    
        
        SKLog(_cmd);
    
    }
    - (void)didMoveToSuperview{
        [super didMoveToSuperview];
        SKLog(_cmd);
    
    }
    - (void)willMoveToWindow:(nullable UIWindow *)newWindow{
        
        
        [super willMoveToWindow:newWindow];
        
        NSLog(@"newWindow =%@",newWindow);
        SKLog(_cmd);
    
    }
    - (void)didMoveToWindow{
        [super didMoveToWindow];
        SKLog(_cmd);
    
    }
    
    - (BOOL)isDescendantOfView:(UIView *)view{
        SKLog(_cmd);
    
       return  [super isDescendantOfView:view];
    }// returns YES for self.
    - (nullable __kindof UIView *)viewWithTag:(NSInteger)tag{
        
        SKLog(_cmd);
    
        return [super viewWithTag:tag];
    }// recursive search. includes self
    
    // Allows you to perform layout before the drawing cycle happens. -layoutIfNeeded forces layout early
    - (void)setNeedsLayout{
        
        [super setNeedsLayout];
        SKLog(_cmd);
    
    }
    - (void)layoutIfNeeded{
        [super layoutIfNeeded];
        SKLog(_cmd);
    
    }
    
    - (void)layoutSubviews{
        
        [super layoutSubviews];
        
    //    NSLog(@"类:%@ 方法名:%@",NSStringFromClass([self class]),NSStringFromSelector(_cmd));
        SKLog(_cmd);
        NSLog(@"self.window =%@",self.window);
    
    }// override point. cal
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    //- (void)drawRect:(CGRect)rect {
    //    // Drawing code
    //    [super drawRect:rect];
    //    SKLog(_cmd);
    //
    //}
    //setNeedsDisplay 这个方法一定会调用,属于图形渲染,只有获得了渲染的图形,才能进行后续的View操作
    - (void)setNeedsDisplay{
        [super setNeedsDisplay];
        SKLog(_cmd);
    
    }
    - (void)setNeedsDisplayInRect:(CGRect)rect{
        [super setNeedsDisplayInRect:rect];
        SKLog(_cmd);
    
    }
    
    @end
    控制台日志:
    2017-04-09 10:30:34.195 SKViewHierarchy[1510:67633] 方法名:setNeedsDisplay
    2017-04-09 10:30:34.196 SKViewHierarchy[1510:67633] newSuperview =<UIScrollView: 0x7fe553840a00; frame = (0 100; 300 400); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000049ff0>; layer = <CALayer: 0x60800002be40>; contentOffset: {0, 0}; contentSize: {0, 0}>
    2017-04-09 10:30:34.196 SKViewHierarchy[1510:67633] [[UIApplication sharedApplication].delegate window]; =<UIWindow: 0x7fe551d06cc0; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x60800004d110>; layer = <UIWindowLayer: 0x60800002b8c0>>
    2017-04-09 10:30:34.196 SKViewHierarchy[1510:67633] 方法名:willMoveToSuperview:
    2017-04-09 10:30:34.197 SKViewHierarchy[1510:67633] 方法名:didMoveToSuperview
    2017-04-09 10:30:34.197 SKViewHierarchy[1510:67633] newWindow =<UIWindow: 0x7fe551d06cc0; frame = (0 0; 375 667); gestureRecognizers = <NSArray: 0x60800004d110>; layer = <UIWindowLayer: 0x60800002b8c0>>
    2017-04-09 10:30:34.198 SKViewHierarchy[1510:67633] 方法名:willMoveToWindow:
    2017-04-09 10:30:34.199 SKViewHierarchy[1510:67633] 方法名:didMoveToWindow
    2017-04-09 10:30:34.276 SKViewHierarchy[1510:67633] 方法名:layoutSubviews
    2017-04-09 10:30:34.277 SKViewHierarchy[1510:67633] self.window =<UIWindow: 0x7fe551d06cc0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x60800004d110>; layer = <UIWindowLayer: 0x60800002b8c0>>
    2017-04-09 10:30:36.204 SKViewHierarchy[1510:67633] 方法名:setNeedsDisplay
    
    UIView有个属性Window,这就是[[UIApplication sharedApplication].delegate window],神奇
    无论创建多少个View的实例,其window属性,都是[[UIApplication sharedApplication].delegate window]
    

    相关文章

      网友评论

          本文标题:UIView生命周期

          本文链接:https://www.haomeiwen.com/subject/xzcdattx.html