Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Last active March 15, 2020 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/5998b7f50cd252149f8cffbcca27f9bb to your computer and use it in GitHub Desktop.
Save DaisukeNagata/5998b7f50cd252149f8cffbcca27f9bb to your computer and use it in GitHub Desktop.
SwiftUI_Timer
//
// ContentView.swift
// SwiftUI_PickerandTimer
//
// Created by 永田大祐 on 2020/03/15.
// Copyright © 2020 永田大祐. All rights reserved.
//
import SwiftUI
struct ContentView: View {
var pic = PicKerView()
@ObservedObject var vm = ViewModel()
@State private var select = 3
init() {
self.vm.setupTimer(0.5)
}
var body: some View {
ZStack {
self.vm.color
VStack {
Picker(selection: $select, label: Text("Luangage")) {
Text("Swift").tag(1)
Text("Kotlin").tag(2)
Text("Python").tag(3)
Text("Java").tag(4)
Text("Ruby").tag(5)
}
.frame(width: 400)
Text("select:\(select)")
// pic
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
final class ViewModel: ObservableObject {
@Published var timer: Timer?
@Published var color = Color.red
func setupTimer(_ time: Double) {
self.timer = Timer(timeInterval: time, target: self, selector: #selector(timerSet), userInfo: nil, repeats: true)
//登録
RunLoop.main.add(self.timer!, forMode: .common)
}
@objc func timerSet() {
if self.color == Color.red {
self.color = Color.blue
} else {
self.color = Color.red
}
}
}
struct PicKerView: UIViewRepresentable {
private let pic = PickerKit()
func makeUIView(context: UIViewRepresentableContext<PicKerView>) -> PickerKit {
pic.delegate = pic
pic.dataSource = pic
return pic
}
func updateUIView(_ uiView: PickerKit, context: UIViewRepresentableContext<PicKerView>) {
}
}
class PickerKit: UIPickerView, UIPickerViewDelegate, UIPickerViewDataSource {
let list: [String] = ["Swift", "Kotlin", "Python", "Java", "Ruby"]
// ドラムロールの列数
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// ドラムロールの行数
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return list.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return list[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// self.textField.text = list[row]
}
}
@DaisukeNagata
Copy link
Author

RunLoop.main.add(self.timer!, forMode: .common) is nonstop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment