【问题标题】:swiftUI moon shapeswiftUI 月亮形状
【发布时间】:2020-10-17 12:49:08
【问题描述】:

我想创建月亮形状。 目前,我通过在顶部放置另一个圆圈然后为其赋予与背景颜色相同的颜色来归档它。

也许使用剪裁的形状或遮罩层。

我想知道是否有更好的方法来做到这一点?

ZStack{

Circle().foregroundColor(isDarkMode ? Color( colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)) : Color.yellow)

.frame(width: 30, height: 30)

.offset(x: isDarkMode ? 20 : -20)

Circle().foregroundColor(isDarkMode ? Color( colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)) : Color.white)

.frame(width: 30, height: 30)

.offset(x: isDarkMode ? 6 : -20).rotationEffect(.degrees( isDarkMode ? -40 : 0))

}

【问题讨论】:

标签: swift swiftui


【解决方案1】:

这是一个可能的方法的演示。使用 Xcode 12 / iOS 14 测试。

struct MoonMask: Shape {
    func path(in rect: CGRect) -> Path {
        var path = Rectangle().path(in: rect)
        path.addPath(Circle().path(in: rect.inset(by: UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 10))
            .offsetBy(dx: 50, dy: -10)))
        return path
    }
}

struct MoonView: View {
    var body: some View {

        Circle()
            .mask(MoonMask().fill(style: FillStyle(eoFill: true)))

            // below is just demo
            .frame(width: 200, height: 200)
            .foregroundColor(Color.yellow)
            .padding().background(Color.black)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多