Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

ios - Synchronous start of the event

Is there a way to run the code simultaneously on different devices? Let's say I want that when I click on a button on one of the devices, the function starts simultaneously on both the first and the second device? I tried to use a timer with a time check for 3 seconds ahead, but the function is triggered with a delay of 0.5 seconds on the second device

func getEventTime() -> UInt64{
    let now = Date()
    let interval = now.timeIntervalSince1970
    let result = (UInt64(interval) + (3)) * 1000
    return result
}

func getCurrentTime() -> UInt64{
    let now = Date()
    let interval = now.timeIntervalSince1970
    let result = UInt64(interval * 1000)
    return result
}

    func startTimer(time : UInt64){
       Timer.scheduledTimer(withTimeInterval: 0.0001, repeats: true) { timer in
        
        switch getCurrentTime() {
        
        case time - 1000 :
            DispatchQueue.main.async {
                countdownImageTimer.image = UIImage(named: "Start")
            }
            break
        
        case time :
            DispatchQueue.main.async {
                countdownImageTimer.removeFromSuperview()
            }
            self.setShips()
            timer.invalidate()
            break
        default:
            break
        }
        }
    }
question from:https://stackoverflow.com/questions/65849473/synchronous-start-of-the-event

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...