2つの* .swiftファイルがあります。 1つは、スライダ付きのViewControllerです。 2つ目は、線を描く「引き出し」クラスです。prepareForSegueなしの2つの* .swiftファイル間でデータを渡す
目標は、スライダーの値から変数を取得し、次にそれを「引き出し」に渡して線を描画し、次に(3)スライダーに新しい値を渡してライン。
デリゲートを使用してデータを渡す必要がありますか、それとも複雑すぎるのでしょうか? 残念ながら私はprepareForSeague
のないDelegateメソッドを見つけることができませんでした。
コードは次のとおりです。
のViewController:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBAction func slider(_ sender: UISlider) {
label.text = String (sender.value)
}
drawer.swift:
import UIKit
// var position = 300 - Here I want to get a variable from the ViewController
class drawer: UIView {
override func draw(_ rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
context?.setLineWidth(2.0)
let colorSpace = CGColorSpaceCreateDeviceRGB()
let components: [CGFloat] = [0.0, 0.0, 1.0, 1.0]
let color = CGColor(colorSpace: colorSpace, components: components)
context?.setStrokeColor(color!)
context?.move(to: CGPoint(x: 30, y: 30))
context?.addLine(to: CGPoint(x: position, y: 400))
context?.strokePath()
}
}
https://stackoverflow.com/a/41560949/2303865 –
で始まるすべてのクラスに名前を付けます。迅速? –
スライダの値が変更されたら、ラインを再描画するためにスライダの値をドロワに送信する必要があります。 – user3820453