我正试图重现一款流行的游戏:抬头,基本上用户必须在朋友的建议下,把手机放在头上,试着猜出名字……如果他抬起头,他跳过了单词,如果他低下头,这意味着他猜到了名字,他获得了一分.他的时间有限.我需要的是,每次用户抬头/低头时,数组的名称都会更改,并且每个名称必须只出现一次.有什么建议吗?

这是我的代码:

import SwiftUI
import CoreMotion

struct ContentView: View {
    
    let motionManager = CMMotionManager()
    let queue = OperationQueue()
    @State private var roll = Double.zero
    
    @State private var people = ["John", "Marcus", "Steve", "Eric", "Philip"].shuffled()
    @State private var randomPerson = Int.random(in: 0...4)
    
    let timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
    @State private var timeRemaining = 10
    @State private var score = 0
    
    var body: some View {
        NavigationView {
            ZStack {
                //Show a red background and "SKIP" if the user raises head
                if roll < 1 {
                    Color.red
                        .ignoresSafeArea()
                    
                    Text("SKIP")
                        .font(.largeTitle)
                        .bold()
                        .foregroundColor(.white)
                } else if roll > 2.1 {
                //Show a green background and "CORRECT" if user lowers head
                    Color.green
                        .ignoresSafeArea()
                    
                    Text("CORRECT")
                        .font(.largeTitle)
                        .bold()
                        .foregroundColor(.white)
                        .onAppear {
                            score += 1
                        }
                } else {
                //Otherwise show a cyan back with array's name
                    Color.cyan
                        .ignoresSafeArea()
                    
                    Text(people[randomPerson])
                        .font(.largeTitle)
                        .bold()
                        .foregroundColor(.white)
                }
                
                Text("\(timeRemaining)")
                    .font(.system(size: 39))
                    .padding(.bottom, 200)
                    .onReceive(timer) { _ in
                        if timeRemaining > 0 {
                            timeRemaining -= 1
                        }
                    }
                
                Text("Score: \(score)")
                    .font(.largeTitle)
                    .bold()
                    .foregroundColor(.white)
                    .padding(.top, 200)
            }
            .onAppear {
                //Detect device motion
                self.motionManager.startDeviceMotionUpdates(to: self.queue) { (data: CMDeviceMotion?, error: Error?) in
                    guard let data = data else {
                        print("Error: \(error!)")
                        return
                    }
                    let attitude: CMAttitude = data.attitude
                    
                    DispatchQueue.main.async {
                        self.roll = attitude.roll
                    }
                }
            }
        }
        .navigationViewStyle(.stack)
    }
}

推荐答案

您可以这样做:

  1. 当前选定人员的状态变量

@State Private var CurrerntPerson:字符串=""

  1. 一个获取随机人的函数

GetRandomPeople()

  1. 更改TextView显示选定人员姓名:
Text(currerntPerson)
   .font(.largeTitle)
   .bold()
   .foregroundColor(.white)
   .onAppear {
        GetRandomPeople()
   }

= 此处的所有代码:

let motionManager = CMMotionManager()
let queue = OperationQueue()
@State private var roll = Double.zero

@State private var people = ["John", "Marcus", "Steve", "Eric", "Philip"].shuffled()
@State private var randomPerson = Int.random(in: 0...4)

let timer = Timer.publish(every: 1, tolerance: 0.5, on: .main, in: .common).autoconnect()
@State private var timeRemaining = 10
@State private var score = 0
@State Private var CurrerntPerson:字符串=""

var body: some View {
    NavigationView {
        ZStack {
            //Show a red background and "SKIP" if the user raises head
            if roll < 1 {
                Color.red
                    .ignoresSafeArea()
                
                Text("SKIP")
                    .font(.largeTitle)
                    .bold()
                    .foregroundColor(.white)
            } else if roll > 2.1 {
                //Show a green background and "CORRECT" if user lowers head
                Color.green
                    .ignoresSafeArea()
                
                Text("CORRECT")
                    .font(.largeTitle)
                    .bold()
                    .foregroundColor(.white)
                    .onAppear {
                        score += 1
                    }
            } else {
                //Otherwise show a cyan back with array's name
                Color.cyan
                    .ignoresSafeArea()
                
                Text(currerntPerson)
                    .font(.largeTitle)
                    .bold()
                    .foregroundColor(.white)
                    .onAppear {
                        GetRandomPeople()
                    }
            }
            
            Text("\(timeRemaining)")
                .font(.system(size: 39))
                .padding(.bottom, 200)
                .onReceive(timer) { _ in
                    if timeRemaining > 0 {
                        timeRemaining -= 1
                    }
                }
            
            Text("Score: \(score)")
                .font(.largeTitle)
                .bold()
                .foregroundColor(.white)
                .padding(.top, 200)
        }
        .onAppear {
            //Detect device motion
            self.motionManager.startDeviceMotionUpdates(to: self.queue) { (data: CMDeviceMotion?, error: Error?) in
                guard let data = data else {
                    print("Error: \(error!)")
                    return
                }
                let attitude: CMAttitude = data.attitude
                
                DispatchQueue.main.async {
                    self.roll = attitude.roll
                }
            }
        }
    }
    .navigationViewStyle(.stack)
}

func GetRandomPeople() {
    if people.count > 0 {
        let index = Int.random(in: 0..<people.count)
        currerntPerson = people[index]
        people.remove(at: index)
    }
}

Ios相关问答推荐

SWIFT设备方向更新不正确

withTaskGroup不适用于CLGeocoder

带菜单的选取器不适用于Int32,但适用于Int

iOS应用启动时崩溃问题解决方法-NO_CRASH_STACK-DYLD 4个符号丢失

@Query与SwiftData数据列表绑定

为什么 Swift 不在启动的同一线程上恢复异步函数?

占位符文本未显示在 TextEditor 上

视觉扫描无法识别 iOS16 上的 GS1 条码特殊字符

视图之间的 SwiftUI 过渡,没有过渡

使用 CIImage 支持的 UIImage 设置 UIImageView 时发生罕见的崩溃

使用swift将图像转换为A4大小的pdf

在 UICollectionView 上动态设置布局导致莫名其妙的 contentOffset 变化

在 Swift 中执行 while 循环

Xcode 11 向后兼容性:UIWindowScene 仅在 iOS 13 或更高版本中可用

Xcode 8:函数类型不能有参数标签 destruct 我的构建

如何在键盘上方添加工具栏?

NSURLSession:如何增加 URL 请求的超时时间?

Select 器touchesBegan:withEvent:的覆盖方法具有不兼容的类型(NSSet,UIEvent)->()

从故事板导航到其他 Swift 1.2 文件时 Xcode 6.3 崩溃

如果我的分发证书过期会怎样?