Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在哪里配置base 375 #3

Open
manondidi opened this issue Dec 27, 2018 · 5 comments
Open

在哪里配置base 375 #3

manondidi opened this issue Dec 27, 2018 · 5 comments

Comments

@manondidi
Copy link

怎么修改成其他的呢

@lixiang1994
Copy link
Owner

这一点是可以的, 只不过我没有在文档中写出

通过源码可以看到我是通过两个静态变量的闭包来进行计算处理的, 默认是基于375按照比例计算的

public enum Auto {
    
    /// 转换 用于数值的等比例计算 如需自定义可重新赋值
    public static var conversion: ((Double) -> Double) = { (origin) in
        guard UIDevice.current.userInterfaceIdiom == .phone else {
            return origin
        }
        
        let base = 375.0 
        let screenWidth = Double(UIScreen.main.bounds.width)
        let screenHeight = Double(UIScreen.main.bounds.height)
        let width = min(screenWidth, screenHeight)
        return origin * (width / base)
    }
    
    /// 适配 用于可视化等比例计算 如需自定义可重新赋值
    public static var adaptation: ((CGFloat) -> CGFloat) = { (origin) in
        return origin.auto()
    }
}

可以在启动时 为这个静态变量赋值一个新的闭包.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        Auto.conversion = { (origin) in
            guard UIDevice.current.userInterfaceIdiom == .phone else {
                return origin
            }
            
            let base = 414.0
            let screenWidth = Double(UIScreen.main.bounds.width)
            let screenHeight = Double(UIScreen.main.bounds.height)
            let width = min(screenWidth, screenHeight)
            return origin * (width / base)
        }
        
        return true
    }

@manondidi
Copy link
Author

能否简便点,不要让业务层写这么多代码

@manondidi
Copy link
Author

我认为最好不要让用户配置这种数字,而是初始化的时候传入一个枚举,告诉你UI给的设计稿是基于哪个设备 而不是传入数字或者传一个闭包,要简化业务层的代码。

@lixiang1994
Copy link
Owner

我将整个计算部分暴露出来可以让使用者重写, 就是考虑到了每个人的情景不同, 有些人是根据宽度方式计算, 但有些人确不是. 我不能去限制他们的计算规则, 我觉得我应该提供最通用的API, 你说的枚举类型应该是在我这个的上一层的封装, 我也在考虑是我来封装这一层还是由使用者自己按照需要封装.

其实目前这种计算处理只需要设置一次即可, 如果你觉得代码过多 可以考虑将他们放入一个func中, 很高兴能收到你的建议, 容我再考虑一下. 😄

@manondidi
Copy link
Author

这块改善了吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants