swift04基础训练

1.Dao 层

DAO(Data Access Object)层:

对数据库进行数据持久化操作,它的方法语句是直接针对数据库操作的,主要实现一些数据的增删改查。

2.for ···in···使用

1.in 后面是范围

import Cocoa
for index in 1...5 {
    print("\(index) 乘于 5 为:\(index * 5)")
}

输出结果为:

1 乘于 5 为:5
2 乘于 5 为:10
3 乘于 5 为:15
4 乘于 5 为:20
5 乘于 5 为:25

2.in 后面是数组

import Cocoa

var someInts:[Int] = [10, 20, 30]

for index in someInts {
   print( "index 的值为 \(index)")
}

输出结果为:

index 的值为 10
index 的值为 20
index 的值为 30

3 firstIndex(of:)

var students = ["Ben", "Ivy", "Jordell", "Maxime"]
if let i = students.firstIndex(of: "Maxime") {
    students[i] = "Max"
}
print(students)
// Prints "["Ben", "Ivy", "Jordell", "Max"]"

4 projiet5

//
//  ContentView.swift
//  Animation
//
//  Created by 朱光俊 on 2022/2/16.
//

import SwiftUI

struct ContentView: View {
    
    @State private var Usedword=[String]()
    
    @State private var newWord=""
    
    @State private var rootWord=""
    
    @State private var errorTitle = ""
    @State private var errorMessage = ""
    @State private var showingError = false

    
    
    
    
    
    var body: some View {
        
        NavigationView{
            
            List{
                

                            
                Section{
                    
                    
                    TextField("newWord", text: self.$newWord)
                        //去除首字母自动大写
                        .autocapitalization(.none)
                    
                        
                        
                    
                }
                
                
                
                Section{
                    
                    ForEach(Usedword,id: \.self){word in
                        
                        
                        HStack{
                            
                            Image(systemName: "\(word.count).circle")
                        
                        
                            Text(word)
                        }
                        
                    }
                    
                    
                    
                }
            
                
                
                
            }
            
            .navigationTitle(rootWord)
            
            //提交文本信息
            .onSubmit(addNewWord)
            .onAppear(perform: loadGameword)
        
            .alert(errorTitle, isPresented: self.$showingError){
                
                Button("ok",role: .cancel){}
                
                
            }message: {
                Text(errorMessage)
            }
            
        }
        
        
        
        
            
    
        
    }
    
    
    
    func addNewWord(){
        
        //小写,去除空白
        
        
        let answer  = newWord.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)
        
        //必须有输入才去增加
        
        
        guard answer.count>0 else{ return }
        
        
        
        //对usedword进行第一位的插入
        
        withAnimation{
        Usedword.insert(answer, at: 0)
        }
        
        //最后对输入textfield的内容进行清除
        newWord=""
        
        guard isOraginal(word: answer) else {
            wordError(title: "Word used already", message: "Be more original")
            return
        }

        guard isPossible(word: answer) else {
            wordError(title: "Word not possible", message: "You can't spell that word from '\(rootWord)'!")
            return
        }

        guard isReal(word: answer) else {
            wordError(title: "Word not recognized", message: "You can't just make them up, you know!")
            return
        }
        
      
        
    }
    
    
    func loadGameword(){
        
        if let loadURL = Bundle.main.url(forResource: "start", withExtension: "txt"){
            
            if let loadContent = try? String(contentsOf: loadURL){
                
                let allwords=loadContent.components(separatedBy: "\n")
                
                rootWord = allwords.randomElement() ?? "nil"

                
                
                return
            }
            
            
        }
        
        
        fatalError("Could not load txt from boundle")
            
            
        
        
        
        
        
    }
    
    //检查单词是否被用过
    func isOraginal(word:String) -> Bool{
        
        !Usedword.contains(word)
        //包含就返回false,不包含就返回true
        
    }
    func isPossible(word:String) ->Bool {
        
        var tempWord=rootWord
        
        for letter in word{
            if let pos = tempWord.firstIndex(of: letter){
                
                tempWord.remove(at: pos)
                
                
                
            }else{
                
                
                return false
                
                
            }
            
            
        }
        
        return true
        
        
        
        
    }
    
    
    
    func isReal(word:String) ->Bool{
        //检查器
        let checker = UITextChecker()
        
        //检查的长度
        let range=NSRange(location: 0, length: word.utf16.count)
        
        let misspelledRange = checker.rangeOfMisspelledWord(in: word, range: range, startingAt: 0, wrap: false, language: "en")
        
        return misspelledRange.location == NSNotFound
        
        

        
    }
    
    func wordError(title: String,message:String){
        
        
        
        errorTitle = title
        
        errorMessage = message
        
        showingError = true
    }
    
    
    
    
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

5让toggle有一个渐变的过程

猜想:从true变成false的 就是从1变成0的过程,但是这个过程是逐渐变换的过程

比如:0.9,0.8,0.7,0.6-----0.0

  Button("Tap Me"){
            
            changeBackgroud.toggle()
            
        }
        .font(.title)
       
        .frame(width: 200, height: 200)
        .background(changeBackgroud ? .blue:.red)
        .foregroundColor(.white)
        .animation(.default, value: changeBackgroud)
        //0~1(0.01,0.02....0.1.....0.88,0.89...0.90....0.97,.0.98,0.9)
        

Project6 -two

6.animation动画

它作为修饰符可以添加多个animation,而且它是分段修饰的

循环这些animation可以使用repeatCount()或repeatForever()

ease in animations start slow and end fast.

 var body: some View {
        
        VStack{
            Button("Tap Me"){
                
                changeBackgroud.toggle()
                
            }
            .font(.title)
           
            .frame(width: 200, height: 200)
            .background(changeBackgroud ? .blue:.red)
            .foregroundColor(.white)
            .animation(nil, value: changeBackgroud)
            //0~1(0.01,0.02....0.1.....0.88,0.89...0.90....0.97,.0.98,0.9)
            .clipShape(RoundedRectangle(cornerRadius: changeBackgroud ? 60 : 0))
            
            .animation(.interpolatingSpring(stiffness: 10, damping: 1), value: changeBackgroud)
        
        
        //stiffness弹簧的刚度
        //damping弹簧的阻尼值
        //interpolating插入
            Text("Offset by passing horizontal & vertical distance")
                .border(Color.green)
                .offset(x: 20, y: 50)
                .border(Color.gray)
        
        }
        
        
        
        
        
        
    }

7.offset(x:,y:)

Text("Offset by passing horizontal & vertical distance")
    .border(Color.green)
    .offset(x: 20, y: 50)
    .border(Color.gray)

修改文本对灰色边框的偏移量

8.rotation3DEffect

.rotation3DEffect(.degrees(animationAmount), axis: (x: 0, y: 1, z: 0))

9.使图片旋转起来

import SwiftUI
struct ContentView: View {
    @State private var changeBackgroud = false
    @State private var animationAmount = 0.0
    var body: some View {
        ZStack{
            Color.blue
                .ignoresSafeArea()
            Button(action:{
                withAnimation{
                animationAmount+=360
                }
            },label:{
                Image("Russia")   
            }
            )
                .rotation3DEffect(.degrees(animationAmount), axis:(x: 0, y: 1, z: 0))   
        }
            
    }
  
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值