版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.03.25 |
前言
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部分(二)
回顾
上一篇主要介绍了UIImageView
分类之架构和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;
该方法的作用就是返回屏幕的尺寸比例。
实例调用
先看一下实例调用。
CGFloat value = [UIScreen screenScale];
NSLog(@"scale = %lf", value);
看一下输出结果
2018-03-25 17:33:01.593227+0800 JJWebImage[2144:93492] scale = 3.000000
这里我用的是模拟器8plus,下面我用5s模拟器,看一下输出结果。
2018-03-25 17:35:56.767142+0800 JJWebImage[2450:98845] scale = 2.000000
方法实现
+ (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
该方法的作用就是返回当前屏幕的大小。
示例调用
CGRect rect = [[UIScreen mainScreen] currentBounds];
NSLog(@"scale = %@", NSStringFromCGRect(rect));
这里是5s模拟器,下面看一下输出结果。
2018-03-25 17:41:06.893232+0800 JJWebImage[2549:102843] scale = {{0, 0}, {320, 568}}
方法实现
- (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;
该方法的作用就是返回给定设备方向的屏幕边界尺寸。 UIScreen
的bounds
方法总是以纵向方向返回它的屏幕边界。
方法实现
下面看一下方法实现。
- (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;
该属性的意义就是屏幕的像素实际尺寸(宽度始终小于高度)。 该值在未知设备或模拟器中可能不太准确。 例如(768,1024)。
示例调用
下面看一下示例调用。
CGSize size = [[UIScreen mainScreen] sizeInPixel];
NSLog(@"size = %@", NSStringFromCGSize(size));
这里用的是5s模拟器,下面看输出结果
2018-03-25 17:52:13.767755+0800 JJWebImage[2766:111102] size = {640, 1136}
方法实现
下面看一下Getter方法实现
- (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;
该属性的作用就是获取每英寸的像素数目,也就是屏幕的PPI,这个值在未知设备和模拟器中可能不是很准确,默认值就是96。
实例调用
CGFloat pixels = [[UIScreen mainScreen] pixelsPerInch];
NSLog(@"pixels = %lf", pixels);
下面看一下输出结果
2018-03-25 18:03:18.085910+0800 JJWebImage[2858:116577] pixels = 326.000000
方法实现
下面看一下方法的实现。
- (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 *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分类,喜欢的给个赞或者关注~~~