두 개의 * .swift 파일이 있습니다. 하나는 슬라이더가있는 ViewController입니다. 두 번째 줄은 선을 그리는 '서랍'클래스입니다.prepareForSegue가없는 두 개의 * .swift 파일간에 데이터 전달
목표는 슬라이더 값에서 변수를 얻은 다음 (2) 선을 그리기 위해 '서랍'으로 전달한 다음 (3) 슬라이더에 새 값을 전달하여 다시 그리기로 전달할 수 있음을 알립니다. 선.
위임자를 사용하여 데이터를 전달해야합니까, 아니면 너무 복잡할까요? 불행히도 prepareForSeague
없이 대리인 메서드를 찾을 수 없습니다.
코드는 다음과 같습니다.
의 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