美文网首页YYKit学习YYKit源码探究
YYKit源码探究(二十三) —— UIView分类(一)

YYKit源码探究(二十三) —— UIView分类(一)

作者: 刀客传奇 | 来源:发表于2018-03-27 00:00 被阅读55次

    版本记录

    版本号 时间
    V1.0 2018.03.26

    前言

    iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - ibireme等。这里有一篇唐巧对他的专访,还有他的 GitHub - Yaoyuan博客,这里贴出来框架YYKit 框架。接下来几篇我们就一起来看一下这个框架。感兴趣的可以看上面写的几篇。
    1. YYKit源码探究(一) —— 基本概览
    2. YYKit源码探究(二) —— NSString分类之Hash(一)
    3. YYKit源码探究(三) —— NSString分类之Encode and decode(二)
    4. YYKit源码探究(四) —— NSString分类之Drawing(三)
    5. YYKit源码探究(五) —— NSString分类之Regular Expression(四)
    6. YYKit源码探究(六) —— NSString分类之NSNumber Compatible(五)
    7. YYKit源码探究(七) —— NSString分类之Utilities(六)
    8. YYKit源码探究(八) —— NSNumber分类(一)
    9. YYKit源码探究(九) —— UIFont分类之架构分析和Font Traits(一)
    10. YYKit源码探究(十) —— UIFont分类之Create font(二)
    11. YYKit源码探究(十一) —— UIFont分类之Load and unload font(三)
    12. YYKit源码探究(十二) —— UIFont分类之Dump font data(四)
    13. YYKit源码探究(十三) —— UIImage分类之框架结构和Create image部分(一)
    14. YYKit源码探究(十四) —— UIImage分类之Image Info(二)
    15. YYKit源码探究(十五) —— UIImage分类之Modify Image(三)
    16. YYKit源码探究(十六) —— UIImage分类之Image Effect(四)
    17. YYKit源码探究(十七) —— UIImageView分类之架构和image部分(一)
    18. YYKit源码探究(十八) —— UIImageView分类之highlight image部分(二)
    19. YYKit源码探究(十九) —— UIScreen分类(一)
    20. YYKit源码探究(二十) —— UIScrollView分类(一)
    21. YYKit源码探究(二十一) —— UITableView分类(一)
    22. YYKit源码探究(二十二) —— UITextField分类(一)

    回顾

    上一篇主要介绍了UITextField分类,这一篇主要看一下UIView部分。


    API

    下面我们看一下API。

    /**
     Create a snapshot image of the complete view hierarchy.
     */
    - (nullable UIImage *)snapshotImage;
    
    /**
     Create a snapshot image of the complete view hierarchy.
     @discussion It's faster than "snapshotImage", but may cause screen updates.
     See -[UIView drawViewHierarchyInRect:afterScreenUpdates:] for more information.
     */
    - (nullable UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates;
    
    /**
     Create a snapshot PDF of the complete view hierarchy.
     */
    - (nullable NSData *)snapshotPDF;
    
    /**
     Shortcut to set the view.layer's shadow
     
     @param color  Shadow Color
     @param offset Shadow offset
     @param radius Shadow radius
     */
    - (void)setLayerShadow:(nullable UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius;
    
    /**
     Remove all subviews.
     
     @warning Never call this method inside your view's drawRect: method.
     */
    - (void)removeAllSubviews;
    
    /**
     Returns the view's view controller (may be nil).
     */
    @property (nullable, nonatomic, readonly) UIViewController *viewController;
    
    /**
     Returns the visible alpha on screen, taking into account superview and window.
     */
    @property (nonatomic, readonly) CGFloat visibleAlpha;
    
    /**
     Converts a point from the receiver's coordinate system to that of the specified view or window.
     
     @param point A point specified in the local coordinate system (bounds) of the receiver.
     @param view  The view or window into whose coordinate system point is to be converted. 
        If view is nil, this method instead converts to window base coordinates.
     @return The point converted to the coordinate system of view.
     */
    - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(nullable UIView *)view;
    
    /**
     Converts a point from the coordinate system of a given view or window to that of the receiver.
     
     @param point A point specified in the local coordinate system (bounds) of view.
     @param view  The view or window with point in its coordinate system. 
        If view is nil, this method instead converts from window base coordinates.
     @return The point converted to the local coordinate system (bounds) of the receiver.
     */
    - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(nullable UIView *)view;
    
    /**
     Converts a rectangle from the receiver's coordinate system to that of another view or window.
     
     @param rect A rectangle specified in the local coordinate system (bounds) of the receiver.
     @param view The view or window that is the target of the conversion operation. If view is nil, this method instead converts to window base coordinates.
     @return The converted rectangle.
     */
    - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(nullable UIView *)view;
    
    /**
     Converts a rectangle from the coordinate system of another view or window to that of the receiver.
     
     @param rect A rectangle specified in the local coordinate system (bounds) of view.
     @param view The view or window with rect in its coordinate system.
        If view is nil, this method instead converts from window base coordinates.
     @return The converted rectangle.
     */
    - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(nullable UIView *)view;
    
    
    @property (nonatomic) CGFloat left;        ///< Shortcut for frame.origin.x.
    @property (nonatomic) CGFloat top;         ///< Shortcut for frame.origin.y
    @property (nonatomic) CGFloat right;       ///< Shortcut for frame.origin.x + frame.size.width
    @property (nonatomic) CGFloat bottom;      ///< Shortcut for frame.origin.y + frame.size.height
    @property (nonatomic) CGFloat width;       ///< Shortcut for frame.size.width.
    @property (nonatomic) CGFloat height;      ///< Shortcut for frame.size.height.
    @property (nonatomic) CGFloat centerX;     ///< Shortcut for center.x
    @property (nonatomic) CGFloat centerY;     ///< Shortcut for center.y
    @property (nonatomic) CGPoint origin;      ///< Shortcut for frame.origin.
    @property (nonatomic) CGSize  size;        ///< Shortcut for frame.size.
    

    下面我们就看一下该API文档。

    1. - (nullable UIImage *)snapshotImage;

    该方法的作用就是创建完整视图层次结构的快照映像。

    方法实现

    - (UIImage *)snapshotImage {
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return snap;
    }
    

    2. - (nullable UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates;

    该方法的作用就是创建完整视图层次结构的快照映像。它比snapshotImage快,但可能导致屏幕更新。
    有关更多信息,请参阅- [UIView drawViewHierarchyInRect:afterScreenUpdates:]

    方法实现

    下面看一下方法实现。

    - (UIImage *)snapshotImageAfterScreenUpdates:(BOOL)afterUpdates {
        if (![self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
            return [self snapshotImage];
        }
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, 0);
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:afterUpdates];
        UIImage *snap = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return snap;
    }
    

    3. - (nullable NSData *)snapshotPDF;

    该方法的作用是创建完整视图层次结构的快照PDF。

    方法实现

    - (NSData *)snapshotPDF {
        CGRect bounds = self.bounds;
        NSMutableData *data = [NSMutableData data];
        CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)data);
        CGContextRef context = CGPDFContextCreate(consumer, &bounds, NULL);
        CGDataConsumerRelease(consumer);
        if (!context) return nil;
        CGPDFContextBeginPage(context, NULL);
        CGContextTranslateCTM(context, 0, bounds.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        [self.layer renderInContext:context];
        CGPDFContextEndPage(context);
        CGPDFContextClose(context);
        CGContextRelease(context);
        return data;
    }
    

    4. - (void)setLayerShadow:(nullable UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius;

    该方法的作用就是设置layer的阴影。

    方法实现

    下面看一下方法实现。

    - (void)setLayerShadow:(UIColor*)color offset:(CGSize)offset radius:(CGFloat)radius {
        self.layer.shadowColor = color.CGColor;
        self.layer.shadowOffset = offset;
        self.layer.shadowRadius = radius;
        self.layer.shadowOpacity = 1;
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
    }
    

    5. - (void)removeAllSubviews;

    该方法的作用就是移除所有子视图。

    注意:不要在viewdrawRect:方法里面调用这个方法。

    方法实现

    下面我们就看一下方法的实现。

    - (void)removeAllSubviews {
        //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        while (self.subviews.count) {
            [self.subviews.lastObject removeFromSuperview];
        }
    }
    

    6. @property (nullable, nonatomic, readonly) UIViewController *viewController;

    该属性的作用就是获取viewviewController,可能为nil。

    方法实现

    下面我们就看一下方法的实现。

    - (UIViewController *)viewController {
        for (UIView *view = self; view; view = view.superview) {
            UIResponder *nextResponder = [view nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                return (UIViewController *)nextResponder;
            }
        }
        return nil;
    }
    

    7. @property (nonatomic, readonly) CGFloat visibleAlpha;

    该属性的作用就是考虑到父视图和window,返回屏幕上的可见alpha。

    方法实现

    下面我们就看一下方法的实现。

    - (CGFloat)visibleAlpha {
        if ([self isKindOfClass:[UIWindow class]]) {
            if (self.hidden) return 0;
            return self.alpha;
        }
        if (!self.window) return 0;
        CGFloat alpha = 1;
        UIView *v = self;
        while (v) {
            if (v.hidden) {
                alpha = 0;
                break;
            }
            alpha *= v.alpha;
            v = v.superview;
        }
        return alpha;
    }
    

    8. - (CGPoint)convertPoint:(CGPoint)point toViewOrWindow:(nullable UIView *)view;

    该方法的作用就是在两个视图上的点的转换。

    方法实现

    下面我们就看一下方法的实现。

    - (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;
    }
    

    9. - (CGPoint)convertPoint:(CGPoint)point fromViewOrWindow:(nullable UIView *)view;

    该方法的作用就是在两个视图上的点的转换,与上面那个方法不同的是,这里是从别的view到receiver的点的转换。

    方法实现

    下面我们就看一下方法的实现。

    - (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;
    }
    

    10. - (CGRect)convertRect:(CGRect)rect toViewOrWindow:(nullable UIView *)view;

    该方法的作用就是在两个视图上的rect的转换。

    方法实现

    下面我们就看一下方法的实现。

    - (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;
    }
    

    11. - (CGRect)convertRect:(CGRect)rect fromViewOrWindow:(nullable UIView *)view;

    该方法的作用就是在两个视图上的rect的转换,与上面那个方法不同的是,这里是从别的view到receiver的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;
    }
    

    12. 其他几个相关属性

    下面看一下其他几个相关属性,包括left、bottom、right和top等。

    这里直接给出实现了,很简单就不多说了。

    - (CGFloat)left {
        return self.frame.origin.x;
    }
    
    - (void)setLeft:(CGFloat)x {
        CGRect frame = self.frame;
        frame.origin.x = x;
        self.frame = frame;
    }
    
    - (CGFloat)top {
        return self.frame.origin.y;
    }
    
    - (void)setTop:(CGFloat)y {
        CGRect frame = self.frame;
        frame.origin.y = y;
        self.frame = frame;
    }
    
    - (CGFloat)right {
        return self.frame.origin.x + self.frame.size.width;
    }
    
    - (void)setRight:(CGFloat)right {
        CGRect frame = self.frame;
        frame.origin.x = right - frame.size.width;
        self.frame = frame;
    }
    
    - (CGFloat)bottom {
        return self.frame.origin.y + self.frame.size.height;
    }
    
    - (void)setBottom:(CGFloat)bottom {
        CGRect frame = self.frame;
        frame.origin.y = bottom - frame.size.height;
        self.frame = frame;
    }
    
    - (CGFloat)width {
        return self.frame.size.width;
    }
    
    - (void)setWidth:(CGFloat)width {
        CGRect frame = self.frame;
        frame.size.width = width;
        self.frame = frame;
    }
    
    - (CGFloat)height {
        return self.frame.size.height;
    }
    
    - (void)setHeight:(CGFloat)height {
        CGRect frame = self.frame;
        frame.size.height = height;
        self.frame = frame;
    }
    
    - (CGFloat)centerX {
        return self.center.x;
    }
    
    - (void)setCenterX:(CGFloat)centerX {
        self.center = CGPointMake(centerX, self.center.y);
    }
    
    - (CGFloat)centerY {
        return self.center.y;
    }
    
    - (void)setCenterY:(CGFloat)centerY {
        self.center = CGPointMake(self.center.x, centerY);
    }
    
    - (CGPoint)origin {
        return self.frame.origin;
    }
    
    - (void)setOrigin:(CGPoint)origin {
        CGRect frame = self.frame;
        frame.origin = origin;
        self.frame = frame;
    }
    
    - (CGSize)size {
        return self.frame.size;
    }
    
    - (void)setSize:(CGSize)size {
        CGRect frame = self.frame;
        frame.size = size;
        self.frame = frame;
    }
    

    后记

    本篇主要介绍的是UIView的一个分类,感兴趣的给个赞或者关注,谢谢~~~~

    相关文章

      网友评论

        本文标题:YYKit源码探究(二十三) —— UIView分类(一)

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