100 Days of SwiftUI - Day 24 項目3 Part2

挑戰(zhàn)

嘗試以下方式擴(kuò)展之前的程序:

  1. 創(chuàng)建一個自定義ViewModifier(和附帶的View擴(kuò)展名),使視圖具有適合于視圖的藍(lán)色大標(biāo)題。
  2. 返回項目1,如果用戶選擇了0%小費,使用條件修飾符將文本總數(shù)視圖更改為紅色。
  3. 返回猜旗項目2,并創(chuàng)建一個FlagImage()視圖,該視圖使用已經(jīng)有的修飾符來渲染一個旗幟。

代碼如下

struct TopTitle: ViewModifier {
    
    var title: String
    func body(content: Content) -> some View {
        
        VStack(spacing: 10) {
            Text(title)
                .font(.largeTitle)
                .foregroundColor(.blue)
            content
        }
    }
}

extension View {
    
    func topTitleStyle(with text: String) -> some View {
        self.modifier(TopTitle(title: text))
    }
}
.topTitleStyle(with: "My title")
    var is0Percentage: Bool {
        let tipSelection = Double(tipPercentages[tipPercentage])
        if tipSelection == 0 {
            return true
        } else {
            return false
        }
    }
Text("$\(totalAmount, specifier: "%.2f")")
        .foregroundColor(is0Percentage ? .red : .primary)
struct FlagImage: View {
    
    var name: String
    var body: some View {
        Image(name)
            .renderingMode(.original)
            .clipShape(Capsule())
            .overlay(Capsule().stroke(Color.black, lineWidth: 1.0))
            .shadow(color: .black, radius: 2)
    }
}
FlagImage(name: countries[number])
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容