//
// ContentView.swift
// ButtonControl
//
// Created by lujun on 2021/12/19.
//
import SwiftUI
struct ContentView: View {
var body: some View {
// basiceButton
//ScrollView(.vertical, showsIndicators: true) {
// addToContainers
// styleButtonIOS
customButtonStyle
//}
}
//MARK: - button的基本使用
var basiceButton : some View {
VStack(spacing:20) {
HStack {
Button(action: {
singIn()
} ,label: {
Text("Sign In 1")
})
Button(action: singIn) {
Text("Sign In 2")
}
}
HStack {
Button("Register 1")
{
register()
}
Button("Register 1",action: register)
}
}
}
private func singIn(){
debugPrint("singIn")
}
private func register(){
debugPrint("register")
}
//MARK: - 添加容器
var addToContainers: some View {
List {
ForEach(items) { item in
Text(item.title) + Text("\t\(item.desc)")
}
Button("Add Item",action: addItem)
}
}
struct Item: Identifiable {
let id = UUID()
let title: String
let desc: String
}
@State private var items: [Item] = []
private func addItem() -> Void {
let newItem = Item(title: "new items title", desc: "new Item Desc")
items.append(newItem)
}
//MARK: - styleButtonIOS button自带的样式
var styleButtonIOS: some View {
VStack(spacing: 20) {
Button("Sign In",action: singIn)
.buttonStyle(PlainButtonStyle())
Button("Sign In",action: singIn)
.buttonStyle(BorderlessButtonStyle())
Button("Sign In",action: singIn)
.buttonStyle(DefaultButtonStyle())
#if os(macOS)
Button("Sign In",action: singIn).buttonStyle(.bordered)
Button("Sign In",action: singIn).buttonStyle(BorderlessButtonStyle())
#endif
}
}
var customButtonStyle: some View {
VStack(spacing: 20) {
Button("Scale Effect",action: singIn)
.buttonStyle(ScaleEffectButtonStyle())
Button("Red Bordered",action: singIn)
.buttonStyle(RedBorderedButtonStyle())
Button("Scale Effect + Bordered",action: singIn)
.buttonStyle(RedBorderedButtonStyle())
.buttonStyle(ScaleEffectButtonStyle())
Button("Scale Effect + Bordered",action: singIn)
.buttonStyle(ScaleEffectButtonStyle())
.buttonStyle(RedBorderedButtonStyle())
Button("Long Press Tap",action: singIn)
.buttonStyle(LongPressButtonStyle())
Button("ComposeButtonStyle",action: singIn)
.buttonStyle(ComposeButtonStyle())
}
}
struct ScaleEffectButtonStyle : ButtonStyle {
func makeBody(configuration: Configuration) -> some View {
HStack {
Spacer()
configuration.label
.padding()
.foregroundColor(configuration.isPressed ? .red: .white)
Spacer()
}
.frame(width: 300)
.background(Color.blue.cornerRadius(10.0))
.scaleEffect(configuration.isPressed ? 0.9 : 1.0)
}
}
struct RedBorderedButtonStyle : PrimitiveButtonStyle {
func makeBody(configuration: Configuration) -> some View {
Button(configuration)
.border(Color.red,width: 2)
}
}
struct LongPressButtonStyle: PrimitiveButtonStyle {
func makeBody(configuration: Configuration) -> some View {
Button(configuration)
.gesture(
LongPressGesture()
.onEnded({ _ in
configuration.trigger()
})
)
}
}
struct ComposeButtonStyle: PrimitiveButtonStyle {
func makeBody(configuration: Configuration) -> some View {
Button(configuration)
.buttonStyle(LongPressButtonStyle())
.buttonStyle(RedBorderedButtonStyle())
.buttonStyle(ScaleEffectButtonStyle())
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}