应用场景:
前段时间公司开发一个新项目,要求在各个设备上均显示一致,老大要求纯代码开发,尝试使用第三方库masonry开发,但效果是不是很好,无法满足需求。后来通过查找资料得到可以通过如下的图建立联系找到解决方法
设备逻辑尺寸和像素关系示意图.png
代码如下:
/**
* 1080*1920转6的系数
*/
static CGFloat const parameterOne =0.96;
/**
* 6转5的系数
*/
static CGFloat const parametertwo =1.171875;
static CGFloat const parameterThree =1.15;
+ (CGFloat)adaptionFrom:(CGFloat)pixel{
CGFloat chanagePixel;
if ([UIScreen mainScreen].bounds.size.width ==320) {
if ([UIScreen mainScreen].bounds.size.height ==480) {
}else if ([UIScreen mainScreen].bounds.size.height ==568){
return chanagePixel = pixel/parameterOne/3*2/parametertwo/2;
}
}else if ([UIScreen mainScreen].bounds.size.width ==375){
return chanagePixel = pixel/parameterOne/3;
}else if ([UIScreen mainScreen].bounds.size.width ==414){
return chanagePixel = pixel*parameterThree/3;
}
return 0 ;
}