溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在iOS中利用SwiftUI實(shí)現(xiàn)顏色漸變填充效果

發(fā)布時(shí)間:2021-04-16 17:45:48 來(lái)源:億速云 閱讀:306 作者:Leah 欄目:移動(dòng)開(kāi)發(fā)

這篇文章給大家介紹怎么在iOS中利用SwiftUI實(shí)現(xiàn)顏色漸變填充效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

Gradient 漸變器

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

gradient是一組顏色的合集,每個(gè)顏色都忽略位置參數(shù)

LinearGradient 線性漸變器

線性漸變器擁有沿軸進(jìn)行漸變函數(shù),我們可以自定義設(shè)置顏色空間、起點(diǎn)和終點(diǎn)。

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 徑向漸變

在徑向漸變中,我們必須指定起始半徑點(diǎn),端半徑點(diǎn)與中心點(diǎn),從徑向漸變開(kāi)變.

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

在角漸變中,我們只需要通過(guò)中心點(diǎn)。

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)
     }
   }

關(guān)于怎么在iOS中利用SwiftUI實(shí)現(xiàn)顏色漸變填充效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI