怎么在iOS中利用SwiftUI实现颜色渐变填充效果

这篇文章给大家介绍怎么在iOS中利用SwiftUI实现颜色渐变填充效果,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

丽水网站建设公司成都创新互联公司,丽水网站设计制作,有大型网站制作公司丰富经验。已为丽水近1000家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的丽水做网站的公司定做!

Gradient 渐变器

A color gradient represented as an array of color stops, each having a parametric location value.

gradient是一组颜色的合集,每个颜色都忽略位置参数

LinearGradient 线性渐变器

线性渐变器拥有沿轴进行渐变函数,我们可以自定义设置颜色空间、起点和终点。

import SwiftUI

struct LineView: View {
   var gradient: Gradient {
      let stops: [Gradient.Stop] = [
        .init(color: .red, location: 0.5),
        .init(color: .yellow, location: 0.5)
      ]
      return Gradient(stops: stops)
    }
    
    var body: some View {
    
        ZStack {
          LinearGradient(gradient: gradient,
                 startPoint: .top,
                 endPoint: .trailing)
            .edgesIgnoringSafeArea(.all)
          
          Image("1")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .clipShape(Circle())
            .overlay(Circle()
              .stroke(lineWidth: 8)
              .foregroundColor(.white))
            .frame(width: 250)
          
        Text("洛神赋图")
              .padding()
              .foregroundColor(.white)
          .cornerRadius(8)
              .background(LinearGradient(gradient: Gradient(colors: [.white, .black]), startPoint: .top, endPoint: .bottom))
          .offset(y:-280)
        }

    }
}
import SwiftUI

struct LineGradient3Color: View {
  
  var body: some View {
    ZStack {
      LinearGradient(gradient:
        Gradient(
          colors: [.blue, .white, .pink]),
              startPoint: .topLeading,
              endPoint: .bottomTrailing)
        .edgesIgnoringSafeArea(.all)
      
      Image("2")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .clipShape(Circle())
        .overlay(Circle()
          .stroke(lineWidth: 8)
          .foregroundColor(.white))
        .frame(width: 250)
      
      Text("清明上河图")
        .padding()
        .foregroundColor(.white)
        .cornerRadius(8)
        .background(LinearGradient(gradient: Gradient(
          colors: [.yellow, .red]),
                      startPoint: .topLeading,
          endPoint: .bottomTrailing))
        .offset(y:-180)
    }
  }
}

Radial Gradient 径向渐变

在径向渐变中,我们必须指定起始半径点,端半径点与中心点,从径向渐变开变.

import SwiftUI

struct RadialView: View {
   var body: some View {
     ZStack {
       RadialGradient(gradient: Gradient(
        colors: [.blue, .black]),
        center: .center,
        startRadius: 2,
        endRadius: 650)
         .edgesIgnoringSafeArea(.all)
       
       Image("3")
         .resizable()
         .aspectRatio(contentMode: .fit)
         .clipShape(Circle())
         .overlay(Circle()
           .stroke(lineWidth: 8)
           .foregroundColor(.white))
         .frame(width: 250)
       
       Text("富春山居图")
         .padding()
         .foregroundColor(.white)
         .cornerRadius(8)
         .background(
          RadialGradient(gradient: Gradient(
           colors: [.yellow, .red]),
                  center: .center,
                   startRadius: 2,
               endRadius: 60))
         .offset(y:-180)
     }
   }
}

Angular Gradient

在角渐变中,我们只需要通过中心点。

import SwiftUI

struct AngularView: View {
    var body: some View {
     ZStack {
      AngularGradient(gradient: Gradient(
        colors: [.green, .blue, .black, .green, .blue, .black, .green]),
        center: .center)
         .edgesIgnoringSafeArea(.all)
       
       Image("4")
         .resizable()
         .aspectRatio(contentMode: .fit)
         .clipShape(Circle())
         .overlay(Circle()
           .stroke(lineWidth: 8)
           .foregroundColor(.white))
         .frame(width: 250)
       
       Text("汉宫春晓图")
         .padding()
         .foregroundColor(.white)
         .cornerRadius(8)
         .background(
          RadialGradient(gradient: Gradient(
           colors: [.yellow, .red]),
                 center: .center,
                   startRadius: 2,
              endRadius: 60))
         .offset(y:-180)
     }
   }

关于怎么在iOS中利用SwiftUI实现颜色渐变填充效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

分享名称:怎么在iOS中利用SwiftUI实现颜色渐变填充效果
分享地址:https://www.cdcxhl.com/article42/ppihec.html

成都网站建设公司_创新互联,为您提供网站改版网站设计公司云服务器服务器托管静态网站网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都定制网站建设