Button โŸฉ custom styles

SwiftUI โŸฉ Controls โŸฉ Button โŸฉ

ๆณจๆ„๏ผšๆœฌไพ‹ไฝฟ็”จๅ…ฉ็จฎไธๅŒๆ–นๆณ•ไพ†ๆŽงๅˆถ Button ็š„ๅค–่ง€ใ€‚

  • btn1 ไฝฟ็”จ view modifier๏ผŒbtn2 ไฝฟ็”จ custom button styleใ€‚

  • ็”จ view modifier ๅฟ…้ ˆ่‡ช่กŒ่จญๅฎš State ่ฎŠๆ•ธ isPressed๏ผŒ็„ถ่€Œ custom button style ๅ‰‡็”ฑ configuration.isPressed ่‡ชๅ‹•็›ฃๆŽง๏ผŒไธ้œ€ๅฆ่กŒ่จญๅฎš State ่ฎŠๆ•ธใ€‚

  • ็”ฑๆ–ผ btn1 ๆ˜ฏ็”ฑ State ่ฎŠๆ•ธไพ†ใ€Œๅˆ‡ๆ›ใ€isPressed ็š„็‹€ๆ…‹๏ผŒๆ‰€ไปฅๅช่ฆๆŒ‰ไธ€ไธ‹ btn1๏ผŒๆŒ‰้ˆ•ๅฐฑๆœƒ้™ทไธ‹ๅŽป๏ผŒ่€Œไธ”ไธๆœƒๅฝˆๅ›žไพ†ใ€‚btn2 ๅ‰‡็”ฑ configuration.isPressed ่‡ชๅ‹•็›ฃๆŽง๏ผŒๆ‰€ไปฅๅชๆœ‰ๆŒ็บŒๆŒ‰ไฝ๏ผŒๆ‰ๆœƒไฟๆŒใ€ŒๆŒ‰ไธ‹็š„็‹€ๆ…‹ใ€ใ€‚

โฌ†๏ธ ้œ€่ฆ๏ผš .neumorphic() (modifier)

struct ContentView: View {
    
    @State private var isPressed: Bool = false
    let color = Color(white: 0.9)
    
    var body: some View {
        HStack {
            VStack(alignment: .trailing, spacing: 40) {
                btn1        // โญ๏ธ 1. use view modifier
                btn2        // โญ๏ธ 2. use custom button style
            }
            .offset(x: -80)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(color)
        
    }
    
    var btn1: some View {
        Button{
            self.isPressed.toggle()
        } label: {
            Label("Hello", systemImage: "person")
                // โญ๏ธ 1. view modifier
                .neumorphic(isPressed: $isPressed, color: color)
        }
        .background(color)
        .overlay { 
            Text("้ปžไธ€ไธ‹ๅฐฑๆœƒไธ‹ๆฒ‰")
                .foregroundColor(.gray)
                .offset(x: 150)
        }
    }
    
    var btn2: some View {
        Button{} label: {
            Label("World", systemImage: "globe")
        }
        // โญ๏ธ 2. custom button style
        .buttonStyle(.neumorphic(color: color))   // ๐ŸŒ€ .neumorphic() 
        .overlay { 
            Text("่ฆๆŒ‰ไฝๆ‰ๆœƒไธ‹ๆฒ‰")
                .foregroundColor(.gray)
                .offset(x: 150)
        }
    }
}

Last updated