第一章:创建项目
首先,在Xcode中创建新项目。可以通过菜单栏的 File 选项选择 Project... 来启动创建流程,然后选择 iOS-App 作为项目类型。创建完成后,即可开始编写代码。但在编写代码之前,需要为项目添加相关素材。
第二章:为项目添加素材
在Xcode开发流程中,可以同时添加多种规格的素材来适应不同的设备和分辨率。导入对应素材后,就无需过多考虑尺寸适配问题。Xcode界面设计良好,从左到右依次是导航栏、开发界面和细节界面。导航栏用于管理整个项目。要为项目添加素材,在左边的导航栏中双击 Assets(资源),然后导入应用图标,这里可以根据需要调整图标。
第三章:创建预定义颜色
预定义颜色可用于暗黑模式,非常方便。设置一个Dark Appearance后,系统会自动适应iOS操作系统中的暗黑模式API。点击 Assets 中的 “+” 号,然后选择 Color Set。将其命名为 ColorShadow。创建好后,里面有两种模式:任意模式和暗黑模式。将两者都设置为黑色,透明度为60。然后可以添加其他颜色,直接拖入即可。再点击 “+” 号创建一个单独的文件夹来整理颜色资源。
第四章:导入图片
在iOS程序中,可以直接使用矢量图(.svg)来避免图像素材在不同分辨率下的失真问题。可以随便找一个矢量图片或普通图片进行测试。导入时,勾选相应选项以确保矢量图不失真。
第五章:创建启动动画
苹果在2020年开发者大会上推出了一套制作启动画面的便捷流程。在最左边点击最上面的图标,然后点击 info,找到 Launch Screen 键。点击Launch Screen的 “+” 号,在 value 中填入图片名字即可设置启动动画。
第六章:创建卡片视图
在左边导航栏右键 MyFirstAppApp,选择 New File...,然后选择用户界面的SwiftUI View,命名为 CardView。以下是一个基本的CardView代码结构:
import SwiftUI
struct CardView: View {
var body: some View {
Text("Hello, World!")
}
}
struct CardView_Previews: PreviewProvider {
static var previews: some View {
CardView()
}
}
在代码中添加注释后,点击 Resume 可以进行预览。在主体部分添加一个ZStack容器,ZStack用于中心对齐,而HStack和VStack分别用于横向和纵向布局。例如:
ZStack {
Text("卡片")
}.frame(width: 335, height: 545)
.background(Color.green)
.cornerRadius(16)
.shadow(radius: 8)
创建好卡片视图后,需要在 ContentView 中调用它。修改ContentView的代码如下:
struct ContentView: View {
var body: some View {
CardView()
}
}
第七章:为卡片视图添加渐变颜色
首先,在CardView的属性声明区域创建一个储存渐变颜色信息的变量:
var gradient: [Color] = [Color("Color01"), Color("Color02")]
然后,在CardView的background修改为使用LinearGradient:
.background(
LinearGradient(
gradient: Gradient(colors: gradient),
startPoint: .top, endPoint: .bottom
)
)
第八章:为卡片添加SVG图片、文字等
在ZStack中添加Image组件和VStack容器来显示文字。例如:
ZStack {
Image("woman-6787784")
VStack {
Text("Hi,我是标题")
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.green)
.multilineTextAlignment(.center)
Text("小蚊子")
.fontWeight(.light)
.foregroundColor(.blue)
.italic()
}.offset(y: -238)
}
第九章:为卡片添加按钮
添加一个按钮,使用HStack容器封装,并应用渐变色和阴影。按钮代码示例如下:
Button(action: {
print("用户点击了按钮")
}) {
HStack {
Text("小蚊子")
.fontWeight(.heavy)
.foregroundColor(.white)
.accentColor(.white)
}
.padding(.vertical)
.padding(.horizontal, 24)
.background(
LinearGradient(
gradient: Gradient(colors: gradient),
startPoint: .leading, endPoint: .trailing
)
)
.clipShape(Capsule())
.shadow(color: Color("ColorShadow"), radius: 6, x: 0, y: 3)
}.offset(y: 200)
第十章:循环生成多张卡片
在ContentView中创建一个横向的ScrollView,使用ForEach循环生成多张卡片。例如:
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: 20) {
ForEach(cards) { item in
CardView(card: item)
}
}.padding(20)
}
第十一章:创建一个数据模型
新建一个文件CardModel,定义一个符合Identifiable协议的结构体Card:
import SwiftUI
struct Card: Identifiable {
var id = UUID()
var title: String
var headline: String
var imageName: String
var callToAction: String
var message: String
var gradientColors: [Color]
}
第十二章:为静态数据创建数组
新建文件CardData,创建一个Card数组来存储静态数据:
import SwiftUI
let cardData: [Card] = [
Card(title: "我是remoo",
headline: "嘻嘻",
imageName: "Image001",
callToAction: "我是开发者",
message: "这是一条信息",
gradientColors: [Color("Color04"), Color("Color06")]
),
Card(title: "我是小蚊子",
headline: "开心~",
imageName: "Image002",
callToAction: "小蚊子",
message: "嗡嗡嗡",
gradientColors: [Color("Color02"), Color("Color06")]
)
]
第十三章:显示数据
修改CardView以接受card参数,并使用传入的数据更新界面。同时,更新ContentView以使用cardData数组。例如,CardView的调整后代码:
struct CardView: View {
var card: Card
var gradient: [Color] = [Color("Color03"), Color("Color04")]
var body: some View {
ZStack {
Image(card.imageName)
VStack {
Text(card.title)
.font(.largeTitle)
.fontWeight(.heavy)
.foregroundColor(.green)
.multilineTextAlignment(.center)
Text(card.headline)
.fontWeight(.light)
.foregroundColor(.blue)
.italic()
}.offset(y: -238)
Button(action: {
print("用户点击了按钮")
}) {
HStack {
Text(card.callToAction)
.fontWeight(.heavy)
.foregroundColor(.white)
.accentColor(.white)
}
.padding(.vertical)
.padding(.horizontal, 24)
.background(
LinearGradient(
gradient: Gradient(colors: card.gradientColors),
startPoint: .leading, endPoint: .trailing
)
)
.clipShape(Capsule())
.shadow(color: Color("ColorShadow"), radius: 6, x: 0, y: 3)
}.offset(y: 200)
}
.frame(width: 335, height: 545)
.background(
LinearGradient(
gradient: Gradient(colors: card.gradientColors),
startPoint: .top, endPoint: .bottom
)
)
.cornerRadius(16)
.shadow(radius: 8)
}
}
ContentView的更新代码:
import SwiftUI
let cards: [Card] = cardData
struct ContentView: View {
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .center, spacing: 20) {
ForEach(cards) { item in
CardView(card: item)
}
}.padding(20)
}
}
}
第十四章:播放提示声音
首先导入音效文件,然后新建文件PlaySound,编写音频播放代码以提升用户体验。音频播放函数示例如下:
import Foundation
import AVFoundation
var audioPlayer: AVAudioPlayer?
func playSound(sound: String, type: String) {
if let path = Bundle.main.path(forResource: sound, ofType: type) {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path))
audioPlayer?.play()
} catch {
print("播放失败。。")
}
}
}
在CardView的按钮动作中调用此函数,例如:playSound(sound: "sound-transitions", type: "mp3")。通过以上步骤,你可以创建一个基本的iOS应用,并使用SwiftUI构建交互式界面。