YYKit源碼探究(十九) —— UIScreen分類(一)

版本記錄

版本號 時間
V1.0 2018.03.25

前言

iOS圈內(nèi)有幾個人大家基本都知道,比如說王巍、唐巧,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - 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分類之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類之highlight image部分(二)

回顧

上一篇主要介紹了UIImageView分類之架構(gòu)和highlight image部分,這一篇主要看一下UIScreen部分。


API

下面我們看一下API。


/**
 Main screen's scale
 
 @return screen's scale
 */
+ (CGFloat)screenScale;

/**
 Returns the bounds of the screen for the current device orientation.
 
 @return A rect indicating the bounds of the screen.
 @see    boundsForOrientation:
 */
- (CGRect)currentBounds NS_EXTENSION_UNAVAILABLE_IOS("");

/**
 Returns the bounds of the screen for a given device orientation.
 `UIScreen`'s `bounds` method always returns the bounds of the
 screen of it in the portrait orientation.
 
 @param orientation  The orientation to get the screen's bounds.
 @return A rect indicating the bounds of the screen.
 @see  currentBounds
 */
- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation;

/**
 The screen's real size in pixel (width is always smaller than height).
 This value may not be very accurate in an unknown device, or simulator.
 e.g. (768,1024)
 */
@property (nonatomic, readonly) CGSize sizeInPixel;

/**
 The screen's PPI.
 This value may not be very accurate in an unknown device, or simulator.
 Default value is 96.
 */
@property (nonatomic, readonly) CGFloat pixelsPerInch;

1. + (CGFloat)screenScale;

該方法的作用就是返回屏幕的尺寸比例。

實例調(diào)用

先看一下實例調(diào)用。

CGFloat value = [UIScreen screenScale];
NSLog(@"scale = %lf", value);

看一下輸出結(jié)果

2018-03-25 17:33:01.593227+0800 JJWebImage[2144:93492] scale = 3.000000

這里我用的是模擬器8plus,下面我用5s模擬器,看一下輸出結(jié)果。

2018-03-25 17:35:56.767142+0800 JJWebImage[2450:98845] scale = 2.000000

方法實現(xiàn)

+ (CGFloat)screenScale {
    static CGFloat screenScale = 0.0;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if ([NSThread isMainThread]) {
            screenScale = [[UIScreen mainScreen] scale];
        } else {
            dispatch_sync(dispatch_get_main_queue(), ^{
                screenScale = [[UIScreen mainScreen] scale];
            });
        }
    });
    return screenScale;
}

2. - (CGRect)currentBounds

該方法的作用就是返回當(dāng)前屏幕的大小。

示例調(diào)用

CGRect rect = [[UIScreen mainScreen] currentBounds];
NSLog(@"scale = %@", NSStringFromCGRect(rect));

這里是5s模擬器,下面看一下輸出結(jié)果。

2018-03-25 17:41:06.893232+0800 JJWebImage[2549:102843] scale = {{0, 0}, {320, 568}}

方法實現(xiàn)

- (CGRect)currentBounds {
    return [self boundsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}

- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation {
    CGRect bounds = [self bounds];
    
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        CGFloat buffer = bounds.size.width;
        bounds.size.width = bounds.size.height;
        bounds.size.height = buffer;
    }
    return bounds;
}

3. - (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation;

該方法的作用就是返回給定設(shè)備方向的屏幕邊界尺寸。 UIScreenbounds方法總是以縱向方向返回它的屏幕邊界。

方法實現(xiàn)

下面看一下方法實現(xiàn)。

- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation {
    CGRect bounds = [self bounds];
    
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        CGFloat buffer = bounds.size.width;
        bounds.size.width = bounds.size.height;
        bounds.size.height = buffer;
    }
    return bounds;
}

static inline BOOL UIInterfaceOrientationIsLandscape(UIInterfaceOrientation orientation) __TVOS_PROHIBITED {
    return ((orientation) == UIInterfaceOrientationLandscapeLeft || (orientation) == UIInterfaceOrientationLandscapeRight);
}

4. @property (nonatomic, readonly) CGSize sizeInPixel;

該屬性的意義就是屏幕的像素實際尺寸(寬度始終小于高度)。 該值在未知設(shè)備或模擬器中可能不太準(zhǔn)確。 例如(768,1024)。

示例調(diào)用

下面看一下示例調(diào)用。

CGSize size = [[UIScreen mainScreen] sizeInPixel];
NSLog(@"size = %@", NSStringFromCGSize(size));

這里用的是5s模擬器,下面看輸出結(jié)果

2018-03-25 17:52:13.767755+0800 JJWebImage[2766:111102] size = {640, 1136}

方法實現(xiàn)

下面看一下Getter方法實現(xiàn)

- (CGSize)sizeInPixel {
    CGSize size = CGSizeZero;
    
    if ([[UIScreen mainScreen] isEqual:self]) {
        NSString *model = [UIDevice currentDevice].machineModel;
        
        if ([model hasPrefix:@"iPhone"]) {
            if ([model isEqualToString:@"iPhone7,1"]) return CGSizeMake(1080, 1920);
            if ([model isEqualToString:@"iPhone8,2"]) return CGSizeMake(1080, 1920);
            if ([model isEqualToString:@"iPhone9,2"]) return CGSizeMake(1080, 1920);
            if ([model isEqualToString:@"iPhone9,4"]) return CGSizeMake(1080, 1920);
        }
        if ([model hasPrefix:@"iPad"]) {
            if ([model hasPrefix:@"iPad6,7"]) size = CGSizeMake(2048, 2732);
            if ([model hasPrefix:@"iPad6,8"]) size = CGSizeMake(2048, 2732);
        }
    }
    
    if (CGSizeEqualToSize(size, CGSizeZero)) {
        if ([self respondsToSelector:@selector(nativeBounds)]) {
            size = self.nativeBounds.size;
        } else {
            size = self.bounds.size;
            size.width *= self.scale;
            size.height *= self.scale;
        }
        if (size.height < size.width) {
            CGFloat tmp = size.height;
            size.height = size.width;
            size.width = tmp;
        }
    }
    return size;
}

5. @property (nonatomic, readonly) CGFloat pixelsPerInch;

該屬性的作用就是獲取每英寸的像素數(shù)目,也就是屏幕的PPI,這個值在未知設(shè)備和模擬器中可能不是很準(zhǔn)確,默認(rèn)值就是96。

實例調(diào)用

CGFloat pixels = [[UIScreen mainScreen] pixelsPerInch];
NSLog(@"pixels = %lf", pixels);

下面看一下輸出結(jié)果

2018-03-25 18:03:18.085910+0800 JJWebImage[2858:116577] pixels = 326.000000

方法實現(xiàn)

下面看一下方法的實現(xiàn)。

- (CGFloat)pixelsPerInch {
    if (![[UIScreen mainScreen] isEqual:self]) {
        return 326;
    }
    
    static CGFloat ppi = 0;
    static dispatch_once_t one;
    static NSString *name;
    dispatch_once(&one, ^{
        NSDictionary<NSString*, NSNumber *> *dic = @{
            @"Watch1,1" : @326, //@"Apple Watch 38mm",
            @"Watch1,2" : @326, //@"Apple Watch 43mm",
            @"Watch2,3" : @326, //@"Apple Watch Series 2 38mm",
            @"Watch2,4" : @326, //@"Apple Watch Series 2 42mm",
            @"Watch2,6" : @326, //@"Apple Watch Series 1 38mm",
            @"Watch1,7" : @326, //@"Apple Watch Series 1 42mm",

            @"iPod1,1" : @163, //@"iPod touch 1",
            @"iPod2,1" : @163, //@"iPod touch 2",
            @"iPod3,1" : @163, //@"iPod touch 3",
            @"iPod4,1" : @326, //@"iPod touch 4",
            @"iPod5,1" : @326, //@"iPod touch 5",
            @"iPod7,1" : @326, //@"iPod touch 6",

            @"iPhone1,1" : @163, //@"iPhone 1G",
            @"iPhone1,2" : @163, //@"iPhone 3G",
            @"iPhone2,1" : @163, //@"iPhone 3GS",
            @"iPhone3,1" : @326, //@"iPhone 4 (GSM)",
            @"iPhone3,2" : @326, //@"iPhone 4",
            @"iPhone3,3" : @326, //@"iPhone 4 (CDMA)",
            @"iPhone4,1" : @326, //@"iPhone 4S",
            @"iPhone5,1" : @326, //@"iPhone 5",
            @"iPhone5,2" : @326, //@"iPhone 5",
            @"iPhone5,3" : @326, //@"iPhone 5c",
            @"iPhone5,4" : @326, //@"iPhone 5c",
            @"iPhone6,1" : @326, //@"iPhone 5s",
            @"iPhone6,2" : @326, //@"iPhone 5s",
            @"iPhone7,1" : @401, //@"iPhone 6 Plus",
            @"iPhone7,2" : @326, //@"iPhone 6",
            @"iPhone8,1" : @326, //@"iPhone 6s",
            @"iPhone8,2" : @401, //@"iPhone 6s Plus",
            @"iPhone8,4" : @326, //@"iPhone SE",
            @"iPhone9,1" : @326, //@"iPhone 7",
            @"iPhone9,2" : @401, //@"iPhone 7 Plus",
            @"iPhone9,3" : @326, //@"iPhone 7",
            @"iPhone9,4" : @401, //@"iPhone 7 Plus",

            @"iPad1,1" : @132, //@"iPad 1",
            @"iPad2,1" : @132, //@"iPad 2 (WiFi)",
            @"iPad2,2" : @132, //@"iPad 2 (GSM)",
            @"iPad2,3" : @132, //@"iPad 2 (CDMA)",
            @"iPad2,4" : @132, //@"iPad 2",
            @"iPad2,5" : @264, //@"iPad mini 1",
            @"iPad2,6" : @264, //@"iPad mini 1",
            @"iPad2,7" : @264, //@"iPad mini 1",
            @"iPad3,1" : @324, //@"iPad 3 (WiFi)",
            @"iPad3,2" : @324, //@"iPad 3 (4G)",
            @"iPad3,3" : @324, //@"iPad 3 (4G)",
            @"iPad3,4" : @324, //@"iPad 4",
            @"iPad3,5" : @324, //@"iPad 4",
            @"iPad3,6" : @324, //@"iPad 4",
            @"iPad4,1" : @324, //@"iPad Air",
            @"iPad4,2" : @324, //@"iPad Air",
            @"iPad4,3" : @324, //@"iPad Air",
            @"iPad4,4" : @264, //@"iPad mini 2",
            @"iPad4,5" : @264, //@"iPad mini 2",
            @"iPad4,6" : @264, //@"iPad mini 2",
            @"iPad4,7" : @264, //@"iPad mini 3",
            @"iPad4,8" : @264, //@"iPad mini 3",
            @"iPad4,9" : @264, //@"iPad mini 3",
            @"iPad5,1" : @264, //@"iPad mini 4",
            @"iPad5,2" : @264, //@"iPad mini 4",
            @"iPad5,3" : @324, //@"iPad Air 2",
            @"iPad5,4" : @324, //@"iPad Air 2",
            @"iPad6,3" : @324, //@"iPad Pro (9.7 inch)",
            @"iPad6,4" : @324, //@"iPad Pro (9.7 inch)",
            @"iPad6,7" : @264, //@"iPad Pro (12.9 inch)",
            @"iPad6,8" : @264, //@"iPad Pro (12.9 inch)",
            };
        NSString *model = [UIDevice currentDevice].machineModel;
        if (model) {
            ppi = dic[name].doubleValue;
        }
        if (ppi == 0) ppi = 326;
    });
    return ppi;
}

后記

本篇主要講述的方法就是UIScreen分類,喜歡的給個贊或者關(guān)注~~~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容