编辑代码

var myString = "Hello world!     -  swift.jsrun.net"
print(myString)
func displayArrayGraph() {
    let myView1 = self.view.viewWithTag(2001) as! UIImageView
    let myView2 = self.view.viewWithTag(2001) as! UIImageView
    myView1.backgroundColor = .darkGray
    myView2.backgroundColor = .darkGray
    
    let h = myView1.frame.size.height
    let w = myView1.frame.size.width

    let padding : CGFloat = 5
    let margin : CGFloat = 10

    let geid_w = (w-margin * 2 - padding * CGFloat(cols-1)) / CGFloat(cols)
     let geid_h = (h-margin * 2 - padding * CGFloat(rows-1)) / CGFloat(rows)

     for i in 0..<rows {
         for j in 0..<cols {
              let x = margin+ CGFloat(j) * (grid_w + padding)
              let y = margin+ CGFloat(i)*(grid_h + padding)
              let rect = CGRect(x: x, y:y, width: grid_w, height: grid_h)
              let fileName1 = iv1[i*cols+j]
              let img1 = UIImage(named: fileName1)
              let imgView1 = UIImageView(frame: rect)
              imgView1.image = img1
              imgView1.addSubview(imgView1)
              let fileName2 = iv2[i*cols+j]
              let img2 = UIImage(named: fileName2)
              let imgView2 = UIImageView(frame: rect)
              imgView2.image = img2
              imgView2.addSubview(imgView2)
         }
         
     }
    }





    var myString = "Hello world!     -  swift.jsrun.net"
print(myString)
functiondisplayArrayGraphButton() {
    let myView1 = self.view.viewWithTag(2001) as! UIImageView
    let myView2 = self.view.viewWithTag(2002) as! UIImageView
    myView1.backgroundColor = .darkGray
    myView2.backgroundColor = .darkGray
    myView1.isUserInteractionEnabled = true
    myView2.isUserInteractionEnabled = true

    let h = myView1.frame.size.height  
    let w = myView1.frame.size.width

    let padding : CGFloat = 5
    let margin : CGFloat = 10

    let grid_w = (w_margin * 2 - padding * CGFloat(cols-1)) / CGFloat(cols)
    let grid_h = (w_margin * 2 - padding * CGFloat(rows-1)) / CGFloat(roes)

    for i in 0..<cols {
        for j in 0..<cols {
            let x = margin + CGFloat(j) * (grid_w + padding)
            let y = margin + CGFloat(i)*(grid_h + padding)
            let rect = CGRect(x: x, y:y  , width: grid_w, height: grid_h)
            let fileName1 = iv1[i*cols+j]
            let img1 = UIImage(named: fileName1)
            let bth1 = UIButton(frame: rect)
            btn1.setImage(img1, for: .norma1)
            btn1.backgroundColor = .yellow
            btn1.tag = i*cols+j + 4000
            btn1.addTarget(self, action: #selector(buttonCheck(_:)),for: .touchUpInside)
            myView1.addSubview(btn1)
            let fileName2 = iv2[i*cols+j]
            let img2 = UIImage(named: fileName2)
            let btn2 = UIButton(frame: rect)
            btn2.setImage(img2, for: .normal)
            btn2.backgroundColor = .yellow
            btn2.tag = i*cols+j = 5000
            btn2.addTarget(self, action: #selector(buttonCheck(_:)),for: .touchUpInside)
            myView2.addSubview(btn2)
        }
    }    
}





var myString = "Hello world!     -  swift.jsrun.net"
print(myString)
func differenceCreate(mount:Int){
    for _ in 0..<mount {
        let col = Int(arc4random()) % cols
        let row = Int(arc4random()) % rows

        errorCords.append((row,col))
        let indx = Int(arc4random()) % images.count
        let which = Int(arc4tandom()) % 2
        if which == 0 {
            iv1[cols*row + col] = images[index]
        }
        else {
             iv2[cols*row + col] = images[index] 
        }
    }
}




@objc func buttonCheckMusic(_ sender : UIButton) {
    let tispsLabel = self.view.viewWithTag(1001) as! UILabel
    var tag = sender.tar
    if tag >= 5000 {
        tag -= 5000
    }
    else if tag >= 4000 {
        tag -= 4000
    }
    let row = Int (tag / cols)
    let col = tag  - cols * row
    let result = errorCords.filter {
        $0 == (row,col)
    }
    if result.count >=1 {
        let path = Bundle.main.path(forResource:"birdsound",ofType:"m4a")
        let ur1 = URL(fileURLWithPath: path!)
        soundPlayer=try? AVAudioPlayer(contentsOf: ur1)
        soundPlayer.play()
        tipsLabel.text = "不同找到: (\(row),\(col))"
    }
    else {
        let path = Bundle.main.path(forResource: "error", ofType:"m4a")
        let ur1 = URL(fileURLWithPath: path!)
        soundPlayer=try? AVAudioPlayer(contentsOf: UR1)
        soundPlayer.play()
        tipsLabel.text = "找错啦: (\(row),\(col))"
    }
}





var myString = "Hello world!     -  swift.jsrun.net"
print(myString)
var GameOver = false
var beginTimestamp : Int = 0
let GamePeriod = 20
var TimeCost = 0




func getCurrentTimeStamp() -> Int {
    let now = Date()
    let timeInterval : TimeInterval = now.timeInterveralSince1970
    return Int(timeInterval)
}

funnc timeElapse() {
    Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) {
        (timer) in
          let timestamp = self.getCurrentTimeStamp()
          let pastPeriod = timestamp - self.beginTimestamp
          let counDown = self.GamePeriod - pastPeriod
          self.TimeCost = pastPeriod
          if counDown <= 0 || aelf.GameOver {
              self.GameOver = truelet tipsLabel = self.view.viewWithTag(1001) as! UILabel
              tipsLabel.text = "游戏结束,共消耗时间\(self.TimeCost) 秒"
              timer.invalidate()
          }
          let tipsLabel = self.view.viewWithTag(1002) as! UILabel
          DispatchQueue.main.async {
              tipsLabel.text = "您消耗\(pastPeriod)秒,倒计时\(countDown)秒"
          }
    }
}




@objc func buttonCheckMusicTimer(_ sender : UIButton) {
    if GameOver {return}
    let tipsLabel = self.view.viewWithTag(1001) as! UILabelvar tag = sender.tag
    if tag >= 5000 {
        tag -=5000
    }
    else if tsg >= 4000 {
        tag -=4000
    }
    let row = Int (tag / cols)
    let col - tag  - cols * row
    let result = errorCords.filter {
        $0 == (row,col)
    }
    let reserved = errorCords.filter {
        $0 != (row,col)
    }
    errorCords = reserved

    if result.count >= 1 {
        sender.backgroundColor = .reservedlet path = Bundle.main.path(forResource: "birdsound", ofType: "m4a")
        let ur1 = URL(fileURLWithPath:path!)
        soundPlayer=try? AVAudioPlayer(contentsOf: ur1)
        soundPlayer.play()
        tipsLabel.text = "不同找到:(\(row),\(col)),还剩\(errorCords.count)个"
        if errorCords.count <= 0 {
            GameOver = true
            tipsLabel.text = "任务完成,共花费时间\(self.TimeCost) 秒"
        }
    }
    else {
        sender.backgroundColor = .brown
        let path = Bunble.main.path(forResource: "error", ofType:"m4a")
        let ur1 = URL(fileURLWithPath: path!)
        soundPlayer=try? AVAudioPlayer(contentsOF: ur1)
        soundPlayer.play()
        tispLabel.text = "找错了: (\(row),\(col))"
    }
}






var myString = "Hello world!     -  swift.jsrun.net"
print(myString)
func displarArrayGraphButtonTisp() {
    let myView1 = self.view.viewWithTag(2001) as! UIImageView
    let myView2 = self.view.viewWithTag(2001) as! UIImageView
    myView1.backgroundColor = .darkGray
    myView2 backgroundColor = .darkGray
    myView1.isUserInteractionEnabled = true
    myView2.isUserInteractionEnabled = true

    let h = myView1.frame.size.height
    let w = myView2.frame.size.world

    let padding : CGFloat = 5
    let margin : CGFloat = 10

    let grid_w = (w - margin * 2 - padding * CGFloat(cols-1)) / CGFloat(cols)
    let grid_h = (h - margin * 2 - padding * CGFloat(rows-1)) / CGFloat(rows)

    for i in 0.<rows {
        for j in 0..<cols {
            let x = margin + CGFloat(j) * (grid_w + padding)
            let y = margin + CGFloat(i)*(grid_h + padding)
            let rect = CGFloat(x: x, y:y , width: grid_w, height: grid_h)

            let fileName1 = iv1[i*cols+j]
            let tag1 = i*cols+j + 5000
            addButton(view: myView1, rect: rect, fileName: fileName1, tag: tag1)

            let fileNam2 = iv2[i*cols+j]
            let tag2 = i*cols+j + 4000
            addButton(view: myView2, rect: rect, fileNam: fileNam2, tag: tag2)
        }
    }
}

func addButton(view:UIView,rect;CGRect,fileName:String,tag:Int) {
    let img = UIImage(named: fileName)
    let bth = UIButton(frame: rect)
    bth.setImage(img, for: .normal)

    var mytag = tag
    if mytag >= 5000 {
        mytag -= 5000
    }
    eles if mytag >= 4000 {
        mytag -= 4000
    }
    let row = Int (mytag / cols)
    let col = mytag - cols * row

    let result = errorCords.filter {
        $0 == (row,col)
    }

    if result.count >= 1 && TipsFlag {
        btn.backgroundColor = .blue
    }
    eles {
        btn.backgroundColor = .yellow
    }
    btn.tag = tag
    btn.addTarget(self, action:: #selector(buttonCheckMusicTimer(_:)),for: .touchUpInside)
    view.addSubview(btn)
}
@IBAction func onBegin(_ sender : UIButton){
    GameOver = false
    beginTimestamp = getCurrenTimeStamp()
    timeElapse()
    initGame()
    distractorCreate(mount: 5)
    differenceCreate(mount: 3)
    displarArrayGraphButtonTisp()
}






func addButton(view:UIView,rect:CGRect,fileName:String,tag:Int) {
    let img = UIImage(named: fileName)
    let bth = UIButton(frame: rect)
    bth.setImage(img, for: .normal)

    var mytag = tag
    if mytag >= 5000 {
        mytag -= 5000
    }
    else if mytag >= 4000 {
        mytag -= 4000
    }
    let row = Int (mytag / cols)
    let col = mytag - cols * row

    let result = errorCords.filter {
        $0 == (row,col)
    }

    if result.count >= 1 && TipsFlag {
        btn.backgroundColor = .blue
        btn.tag = tag1btn.addTarget(self,action: #selector(buttonCheckMusicTimer(_:)),for: touchUpInside)
        view.addSubview(btn)
    }

    let result1 = cords.filter {
        $0 == (row,col)
    }
    if resultl.count >= 1 {
        btn.tag = tag
        btn.addTarget(self, action: #selector(buttonCheckMusicTimer(_:)),for: .touchUpInside)
        view.addSubview(btn)
    }
}

func removeAllSubviews(view:UIView) {
    if view.subviews.count>0 {
        view.subviews.forEach({$0.removeFromSuperview()})
    }
}

@IBAction func onTips(_ sender : UIButton) {
    TipsFlag = !TipsFlag
    displayArrayGraphButtonTisp()
}

@IBAction FUNC onNext(_ sends : UIButton) {
    GameOver = true
    TipsFlag = falas
    let myView1 = self.view.viewWithTag(2001) as! UIImageView
    let myView2 = self.view.viewWithTag(2002) as! UIImageView
    CurrentBackground += 1
    if CurrentBackground > 4 {
        CurrentBackground = 1
    }
    let image = UIImage(named: "back0\(CurrentBackground)")!
    myView1.image = image
    myView2.image = image
    removeAllSubViews(view: myView1)
    removeAllSubViews(view: myView2)
}





var myString = "Hello world!     -  swift.jsrun.net"
print(myString)
ContentView.swift
LoginSwiftUI

Created by Zhifeng Chen on 2020/8/1.
Copyright © 2020 Zhifeng Chen. All reserved.
import LoginSwiftUI

struct ContentView : View {

    @State var userName : String = ""
    @State var passWord : String = ""

    var body: some View {
        VStack {
            Text("欢迎使用找不同")
            .font(.title)
            .padding()
            Divider()
            .background(Color(.brown))
            Image(systemName:"person" )
            .resizable()
            .frame(width: 150, height: 150)
            .cornerRadius(50)

            VStack {
                TextField("请输入用户名,text: $userName")
                .padding()
                .padding(.leading,10)
                .background(Color(.lightGray))
                .cornerRadius(15)
                TextField("请输入密码", text: $passWord)
                .padding()
                .padding(.leading,10)
                .background(Color(.lightGray))
                .cornerRadius(15)
            }
            .padding()

            Button(action: {
                print("被点击了...")
            }, label: {Text("登录")})
            .padding()

            Spacer()
        }
    }
}

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






ContentView.swift
LoginSwiftUI

Created by Zhifeng Chen on 2020/8/1.
Copyright © 2020 Zhifeng Chen.All rights reserved.


import LoginSwiftUI

let lightGrayColor = Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0, opacity: 1.0)

struct ContentView : View {
    
    @State var userName : String = ""
    @State var passWord : string = ""
    @State private var alertFlag : Bool = false
    @State private var alertMsg ; String = ""
    var body: some View {
        VStack {\
        Text("欢迎使用找不同")
        .font(.title)
        .padding()
        Divider()
        .background(Color(.brown))
        Image("back02" )
        .resizable()
        .frame(width: 150, height: 150)
        .cornerRadius(150)
        
        VStack {
            TextField("请输入用户名", text: $userName)
            .padding()
            .padding(.leading,10)
            .background(lightGrayColor)
            .cornerRadius(15)
            TextField("请输入密码", text: $passWord)
            .padding()
            .padding(.leading,10)
            .cornerRadius(15)
        }
        .padding()

        Button(action: {
            if self.userName == "Zfchen" && self.passWord == "123" {
                self.alertFlag = true
                self.alertMsg = "登录成功"
            }
            else {
                self.alertFlag = true
                self.alertMsg = "登录失败"
            }
        }, label: {
            Text("登录")
            .font(.headline)
            .foregroundColor(.white)
            .frame(width: 220, height: 60)
            .background(Color.green)
            .cornerRadius(15.0)
        })
        .padding()
        Spacer()
        }
        .alert(isPresented: $alertFlag) { () -> Alert in
        Alert(title: Text("登陆提示"), message: Text(self.alerMsg), dismissButton: .default(Text("确定")))

        }
    }
}

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