iOS SwiftUI 顏色漸變填充效果的實(shí)現(xiàn)
SwiftUI 為我們提供了各種梯度選項(xiàng),所有這些選項(xiàng)都可以通過(guò)多種方式使用。
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)。
下面我們看看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 徑向漸變
在徑向漸變中,我們必須指定起始半徑點(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)
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)通過(guò)按鈕添加和刪除控件的方法
這篇文章主要為大家詳細(xì)介紹了iOS通過(guò)按鈕添加和刪除控件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
微信小程序 實(shí)現(xiàn)listview帶字母滑動(dòng)
這篇文章主要介紹了微信小程序 實(shí)現(xiàn)listview帶字母滑動(dòng)的相關(guān)資料,需要的朋友可以參考下2017-05-05
iOS通過(guò)Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼
本篇文章主要介紹了iOS通過(guò)Runtime實(shí)現(xiàn)友盟統(tǒng)計(jì)的實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
IOS開(kāi)發(fā)中加載大量網(wǎng)絡(luò)圖片優(yōu)化方法
這篇文章主要介紹了IOS開(kāi)發(fā)中加載大量網(wǎng)絡(luò)圖片如何優(yōu)化的相關(guān)資料,需要的朋友可以參考下2017-03-03
IOS中快速集成短信SDK驗(yàn)證開(kāi)發(fā)(SMSSDK),IOS開(kāi)發(fā)中如何設(shè)置手機(jī)短信驗(yàn)證碼
這篇文章主要介紹了IOS中快速集成短信SDK驗(yàn)證開(kāi)發(fā)(SMSSDK),IOS開(kāi)發(fā)中如何設(shè)置手機(jī)短信驗(yàn)證碼 的相關(guān)資料,需要的朋友可以參考下2016-01-01

