在 SwiftUI 中,如何为 UIButton 提供渐变并为该按钮提供具有相同渐变的底部阴影?

How do you give a UIButton a gradient and give that button a bottom shadow with that same gradient in SwiftUI?

所以我的设计师朋友为我制作了一个密码生成器模型,我将制作 https://imgur.com/gallery/LZNNrWj and I don't know how to code in that gradient using SwiftUI, the gradient of the button is #41A5ED -> #3099D8 and going in this direction (See picture) https://imgur.com/gallery/2rd7Iix the top left is #41A5ED and the bottom right is #3099D8. I also want to give this button a bottom shadow like this https://imgur.com/gallery/gEmBJ1w 具有与按钮相同的渐变。如果您对我将如何做这件事有任何想法,请告诉我,因为我完全迷路了。

你可以这样做:

Button(action: createTask) {
                   Text(“MyButton“)
                       .color(.white)
                       .font(Font.system(size: 17))
                       .frame(height: 56)
                       .frame(minWidth: 0, maxWidth: .infinity)
                       .background(LinearGradient.actionButton, cornerRadius: 28)
                       .shadow() // configure shadow as you want
                   }

为了提高代码的可读性,我单独创建了渐变:

fileprivate extension LinearGradient {
   static let actionButton = LinearGradient(gradient: Gradient(colors: [Color(“ActionGradientFirst”), Color(“ActionGradientSecond”)]),
                                            startPoint: .topLeading,
                                            endPoint: .bottomTrailing)
}

颜色 ActionGradientFirstActionGradientSecond 我在 Assets.xcasset

中声明

首先使用以下方法创建渐变,

let gradientLayer = CAGradientLayer()
gradientLayer.colors = [UIColor.red.cgColor, UIColor.black.cgColor]
gradientLayer.startPoint = CGPoint(x: 0, y: 0)
gradientLayer.endPoint = CGPoint(x: 1, y: 1)
gradientLayer.frame = view.bounds

然后只需将它应用到您想要的任何视图,

view.layer.addSublayer(gradientLayer)

要添加阴影,

    let shadowPath = UIBezierPath(rect: view.bounds)
    view.layer.masksToBounds = false
    view.layer.shadowColor = UIColor.grey.cgColor
    view.layer.shadowOffset = CGSize(width: 0, height: 0.5)
    view.layer.shadowOpacity = 0.2
    view.layer.shadowPath = shadowPath.cgPath

有点晚了,但以防对其他人有帮助。

ZStack{
    LinearGradient(
        gradient: .init(colors: [Color.white, Color.blue.opacity(0.66)]),
        startPoint: .init(x: 0.0, y: 0.0),
        endPoint: .init(x: 0.75, y: 0.75)
    )
    .mask(
        RoundedRectangle(cornerRadius: 15)
            .frame(width: 120, height: 45, alignment: .center)
            .blur(radius: 10)
    )
    .padding(.top, 20)
    Button(action: {}, label: {
        Text("Siguiente")
            .font(.custom("Avenir-Heavy", size: 20))
            .padding(.top, 10)
            .padding(.bottom, 10)
            .padding(.leading, 16)
            .padding(.trailing, 16)
    })
    .foregroundColor(.white)
    .background(
        LinearGradient(
            gradient: .init(colors: [Color.white, Color.blue.opacity(0.75)]),
            startPoint: .init(x: -0.33, y: -0.33),
            endPoint: .init(x: 0.66, y: 0.66)
        ))
    .clipShape(RoundedRectangle(cornerRadius: 15))
    .buttonStyle(PlainButtonStyle())
}
.frame(height: 100)