ios - 使用 SwiftUI Shape 进行阴影偏移

标签 ios graphics swiftui

我正在尝试使用 Shape、Geometry reader 和 Path 在 SwiftUI 中绘制一个圆圈,我希望阴影位于圆圈的正下方,但阴影出现偏移,我似乎无法将其绘制到哪里它应该:

shadow offset problem


struct ContentView: View {

    var body: some View {

         return  GeometryReader { geometry in

            VStack(alignment: .center) {
            BackgroundRing()
                .stroke(Color.red, style:  StrokeStyle(lineWidth: geometry.size.width < geometry.size.height ? geometry.size.width / 12.0 :  geometry.size.height / 12))
                .padding()
                .shadow(color: .gray, radius: 1.0, x: 0.0, y: 0.0)
            }
         }
    }
}

struct BackgroundRing : Shape {
     func path(in rect: CGRect) -> Path {
        var path: Path = Path()

                let radiusOfRing: CGFloat = (rect.width < rect.height ? rect.width/2 - rect.width / 12 : rect.height/2 - rect.height / 12)
        path.addRelativeArc(center: CGPoint(x: rect.width/2, y: rect.height/2),
                                radius: radiusOfRing,
                                startAngle: Angle(radians: 0.0),
                                delta: Angle(radians: Double.pi * 2.0 ))

        return path
    }
}




最佳答案

好的 所以我似乎已经设法解决了这个问题。宽度/高度与代码交互以计算阴影位置 - 阴影位置似乎来自框架尺寸而不是形状。

添加

.aspectRatio(contentMode: .fit)

解决问题

此外,.shadow 似乎自动将默认偏移设置为与半径相同的值,因此要获得 0.0 的实际偏移,您必须相对于半径设置它,如下所示:

.shadow(radius: 10.0, x: -10.0, y: -10.0)

在我看来像是一个错误,但这个解决方法解决了它:

enter image description here

import SwiftUI

struct ContentView: View {

    var body: some View {

         return  GeometryReader { geometry in

            VStack(alignment: .center) {
            BackgroundRing()
               .stroke(Color.red,
                        style:  StrokeStyle(lineWidth: geometry.size.width < geometry.size.height ? geometry.size.width / 12.0 :  geometry.size.height / 12))
                .shadow(radius: 30.0, x: -30.0, y: -30.0)
                .aspectRatio(contentMode: .fit)
            }
         }

    }
}

struct BackgroundRing : Shape {
     func path(in rect: CGRect) -> Path {
        var path: Path = Path()

                let radiusOfRing: CGFloat = (rect.width < rect.height ? rect.width/2 - rect.width / 12 : rect.height/2 - rect.height / 12)
        path.addRelativeArc(center: CGPoint(x: rect.width/2, y: rect.height/2), // <- this change solved the problem
                                radius: radiusOfRing,
                                startAngle: Angle(radians: 0.0),
                                delta: Angle(radians: Double.pi * 2.0 ))

        return path
    }
}

关于ios - 使用 SwiftUI Shape 进行阴影偏移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58470620/

相关文章:

ios - 使用自定义单元格创建多个部分

ios - 在 iOS 中修改 CNContact 的属性

iphone - 在 iOS 上使用多重采样抗锯齿会对性能产生什么影响?

c# - 用c#裁剪矩形

xcode - 为什么 resizeable() 需要成为 View 中的第一个方法?

swift - 缩短 swiftUI 代码 : compiler is unable to type-check this expression in reasonable time

ios - Swift - 受密码保护的 View Controller

ios - ScrollView 中的 TableView

java - 简单的线条画

swiftui - 将 UIWindow.rootView 转换为具有环境变量的 UIHostingController<View>