设置连续轮选择器 - SwiftUI [英] Set continuous wheel picker - SwiftUI

查看:81
本文介绍了设置连续轮选择器 - SwiftUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何显示可以在选定范围内无限向上或向下滚动的选择器?就像在 ios 设备中设置闹钟一样.现在,如果我的范围在 0 到 60 之间,我只能向上滚动直到达到 60,然后向下滚动返回到 0.我希望它是连续的,当我达到 60 时,我让循环重新开始.

How can I display a picker that can scroll either up or down infinitely inside a chosen range? Just like when you set an alarm in ios devices. Right now if I have a range between 0 and 60, I can only scroll up until I reach 60 and then scroll down to go back to 0. I want it to be continuous that when I reach 60 I have the loop starting over again.

推荐答案

您可以简单地添加大量作为范围的上限并将初始值设置为它的一半并获得截断提醒除法的结果值它乘以 60 value % 60.要在选择器值更改时执行方法,您可以使用 [post][1] 中显示的方法:

You can simply add a large amount as the upper bound of your range and set the initial value to be half of it and get the resulting value truncating the reminder dividing it by 60 value % 60. To execute a method when the picker value changes you can use the approach shown in this [post][1]:

import SwiftUI

struct ContentView: View {
    @State private var selection = 300
    var minutes: Int { selection % 60 }
    var body: some View {
        Picker(selection: $selection, label: Text("Minutes:")) {
            ForEach(0 ..< 600) {
                Text(String(format: "%02d", $0 % 60))
            }
        }.onChange(of: selection, perform: valueChanged)
    }
    func valueChanged(_ value: Int) {
        selection = 300 + value % 60
        print("Minutes: \(minutes)")
    }
}

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

这篇关于设置连续轮选择器 - SwiftUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆