【问题标题】:Translate generated code shape by PaintCode to dynamic shape size将 PaintCode 生成的代码形状转换为动态形状大小
【发布时间】:2020-11-30 09:36:05
【问题描述】:

我尝试使用 UIBezietPath 绘制形状,但每次我都遇到“曲线”问题。 为了解决我的问题,我确实使用了 Paint Code,代码生成得很好,但它是一个静态形状,我尝试将此代码转换为动态屏幕大小,但每次我都遇到一半形状的问题。

谁能帮我正确翻译这段代码?

Paint Code 生成的代码:

//// Color Declarations
let fillColor = NSColor(red: 0.118, green: 0.118, blue: 0.149, alpha: 1)

//// Bezier Drawing
let bezierPath = NSBezierPath()
bezierPath.move(to: NSPoint(x: 8.83, y: 89))
bezierPath.line(to: NSPoint(x: 154.29, y: 89))
bezierPath.curve(to: NSPoint(x: 163.01, y: 81.04), controlPoint1: NSPoint(x: 158.71, y: 89), controlPoint2: NSPoint(x: 162.21, y: 85.39))
bezierPath.curve(to: NSPoint(x: 206.45, y: 45.05), controlPoint1: NSPoint(x: 166.77, y: 60.57), controlPoint2: NSPoint(x: 184.79, y: 45.05))
bezierPath.curve(to: NSPoint(x: 249.89, y: 81.04), controlPoint1: NSPoint(x: 228.11, y: 45.05), controlPoint2: NSPoint(x: 246.12, y: 60.57))
bezierPath.curve(to: NSPoint(x: 258.61, y: 89), controlPoint1: NSPoint(x: 250.68, y: 85.39), controlPoint2: NSPoint(x: 254.19, y: 89))
bezierPath.line(to: NSPoint(x: 405.17, y: 89))
bezierPath.curve(to: NSPoint(x: 414, y: 80.21), controlPoint1: NSPoint(x: 410.05, y: 89), controlPoint2: NSPoint(x: 414, y: 85.06))
bezierPath.line(to: NSPoint(x: 414, y: 8.79))
bezierPath.curve(to: NSPoint(x: 405.17, y: 0), controlPoint1: NSPoint(x: 414, y: 3.94), controlPoint2: NSPoint(x: 410.05, y: 0))
bezierPath.line(to: NSPoint(x: 8.83, y: 0))
bezierPath.curve(to: NSPoint(x: 0, y: 8.79), controlPoint1: NSPoint(x: 3.95, y: 0), controlPoint2: NSPoint(x: 0, y: 3.94))
bezierPath.line(to: NSPoint(x: 0, y: 80.21))
bezierPath.curve(to: NSPoint(x: 8.83, y: 89), controlPoint1: NSPoint(x: 0, y: 85.06), controlPoint2: NSPoint(x: 3.95, y: 89))
bezierPath.close()
fillColor.setFill()
bezierPath.fill()

形状:

【问题讨论】:

    标签: swift drawing shapes uibezierpath paintcode


    【解决方案1】:

    首先扩展CGPoint并创建一个scaled(by:)方法:

    extension CGPoint {
        func scaled(by value: CGFloat) -> CGPoint { applying(.init(scaleX: value, y: value)) }
    }
    

    然后得到实际superview的bounds宽度并除以你的路径宽度414

    let value: CGFloat = superview!.bounds.size.width / 414
    

    现在您只需将比例值应用于路径的每个点:

    bezierPath.move(to: NSPoint(x: 8.83, y: 89).scaled(by: value))
    

    【讨论】:

    • 很好的答案!这段代码很酷,我在所有 iPhone 和 iPad 上都进行了测试,运行良好。但是在 iPad 上这个底部菜单似乎有点大,iPad 设备需要改变什么?我了解您的代码,但不明白您为什么要在 414 上划分熨平板宽度?对于 iPad 设备,此值需要更改吗?您是否因为最大 x 位置 414 我是对的而在 414 上进行划分?
    • 414 是路径中的最高 x 值(屏幕宽度)。关于 iPad 的问题,我不确定,但您需要调试并找出一个百分比值来乘以您现在使用的比例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多