1

所以我的设计师朋友给我做了一个密码生成器的模型,我要制作https://imgur.com/gallery/LZNNrWj,我不知道如何使用 SwiftUI 在该渐变中编码,渐变按钮是#41A5ED -> #3099D8 并朝这个方向前进(见图)https://imgur.com/gallery/2rd7Iix左上角是#41A5ED,右下角是#3099D8。我还想给这个按钮一个像 https://imgur.com/gallery/gEmBJ1w这样的底部阴影,与按钮具有相同的渐变。如果您对我将如何做这件事有任何想法,请告诉我,因为我完全迷路了。

4

3 回答 3

2

你可以这样做:

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中声明

于 2019-06-10T07:13:50.960 回答
2

有点晚了,但以防万一它帮助别人。

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)

在此处输入图像描述

于 2021-08-08T03:27:38.330 回答
-1

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

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
于 2019-06-10T08:06:19.437 回答