【问题标题】:How to change images in UI according to time duration in Swift如何根据 Swift 中的持续时间更改 UI 中的图像
【发布时间】:2020-01-23 07:25:41
【问题描述】:

我正在做 iWatch 应用程序。其中,用户必须步行 6 分钟。在那,我正在展示计时器。据此,我必须更改行走图像。我更改了 8 张图片。 如image1.png、image2.png等到image8.png

如何根据时长改变图像。

我是 Swift 语言的新手。

    @IBOutlet weak var walkingImage: WKInterfaceImage!
    let walkingImagesArray = ["walking1.png", "walking2.png", "walking3.png","walking4.png", "walking5.png","walking6.png", "walking7.png", "walking8.png"]

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)
        self.startTimer()
}

    func startTimer() {
        self.countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
    }


    @objc func updateTime() {
        count = count + 1
        if(count < 361) {
            timerLabel.setText(String(timeString(time: TimeInterval(count))))
           //here I have to use switch case to change image
        } else {
            print("Workout completed")
            countdownTimer.invalidate()
            session.end()
            builder.endCollection(withEnd: Date()) { (success, error) in
                self.builder.finishWorkout { (workout, error) in
                    DispatchQueue.main.async() {
                        self.session = nil
                        self.builder = nil
                    }
                }
            }
        }
        print("\(count)")
    }

有什么建议吗?

【问题讨论】:

  • 每4分钟,我要更新图像,我已经给了updateTIme代码@Anbu.Karthik
  • @AnilkumariOSReactNative- bro,你能显示你创建的updateTime吗,如果你的时间间隔是
  • @Anbu.Karthik 已更新

标签: ios swift xcode apple-watch progress


【解决方案1】:

在少于 60 秒的情况下生成的图像被命名为“walking0.png”

解决方案:

func image(time: Int) -> String {
    let sekInt = time / 60
    let sek = String(sekInt)
    return "walking\(sek).png"
}

self.walkingImage.setImage(UIImage(contentsOfFile: image(time:count)))

单元测试:

func testExample() {
    XCTAssertEqual(image(time: 360), "walking6.png")
    XCTAssertEqual(image(time: 359), "walking5.png")
    XCTAssertEqual(image(time: 301), "walking5.png")
    XCTAssertEqual(image(time: 300), "walking5.png")
    XCTAssertEqual(image(time: 299), "walking4.png")
    XCTAssertEqual(image(time: 61), "walking1.png")
    XCTAssertEqual(image(time: 60), "walking1.png")
    XCTAssertEqual(image(time: 59), "walking0.png")
    XCTAssertEqual(image(time: 1), "walking0.png")
    XCTAssertEqual(image(time: 0), "walking0.png")
}

【讨论】:

    【解决方案2】:

    您可以启动 6 分钟的计时器并每 6 分钟更改一次图像。

    var timer: Timer?
    var currentIndex: Int = 0
    let walkingImagesArray = ["walking1.png", "walking2.png", "walking3.png","walking4.png", "walking5.png","walking6.png", "walking7.png", "walking8.png"]
    
    func startTimer() {
        if let timer = timer {
            self.timer = timer
    
        } else {
            Timer.scheduledTimer(withTimeInterval: 360, repeats: true) { timer in
                //Change your image here
                self.currentIndex = self.currentIndex + 1
                self. walkingImage.image = self.walkingImagesArray[self.currentIndex]
            }
        }
    }
    
    func stopTimer() {
        self.timer?.invalidate()
        self.timer = nil
    }
    

    【讨论】:

    • 每 4 分钟更换一次图像。总计时器 6 分钟你会怎么做
    • @shraddha11 这里的索引就像计时器值
    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 2021-06-15
    • 2023-03-31
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多