Swiftを参考書を見ながら勉強しているのですが、
基本的にグラフ描写などの記述では
コードの中に数字を入れてあるものばかりです。
この例で行くと
let n = 40
のような値です。
このような場合に
storyBoard上に設置したtext11とbutton1を使って
→ let n = 40 の40をtext1に入力された値にする
→ button1を押した時に描画
ということを実現したいのです。
しかし、
class ZLineの中に
textをoutlet接続することもできなければ
Buttonを登録することもできません。
しぶしぶ、配置可能な場所に
配置してみても、
let n = text1.text
という記述は読み込めずエラーになります。
swift
1import UIKit 2 3class ZLine: UIView { 4 override func draw(_ r:CGRect) { 5 let l = UIBezierPath() 6 //線の本数を表すNの値をえ変えてみよう 7 8 9 10 11 let n = 40 12 let xmax = Int(r.width) 13 let ymax = Int(r.height) 14 let xn = xmax / n 15 let yn = ymax / n 16 var y = 0 17 for x in stride(from: 0, through: xmax, by: xn) { 18 l.move(to: CGPoint(x: xmax - x, y: ymax)) 19 l.addLine(to: CGPoint(x: x, y: y)) 20 y += yn 21 } 22 y = ymax 23 for x in stride(from: xmax, through: 0, by: -xn) { 24 l.move(to: CGPoint(x: xmax - x, y: 0)) 25 l.addLine(to: CGPoint(x: x, y: y)) 26 y -= yn 27 UIColor.yellow.set() // UIColor.yellow.set 28 // 29 l.stroke() 30 } 31 32 } 33} 34 35public class tenthViewController: UIViewController { 36 37 38 39 40 41 42 override public func viewDidLoad() { 43 44 view.backgroundColor = .cyan 45 view.addSubview(ZLine(frame: CGRect(x: 15, y: 90, width: 300, height: 400))) 46 super.viewDidLoad() 47 48 // Do any additional setup after loading the view. 49 } 50 51 52 /* 53 // MARK: - Navigation 54 55 // In a storyboard-based application, you will often want to do a little preparation before navigation 56 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 57 // Get the new view controller using segue.destination. 58 // Pass the selected object to the new view controller. 59 } 60 */ 61 62} 63
これはおそらく
return を使いながら記述していくのだと感じているのですが
自分の今の力ではイメージもできず、
外部から入力して値を自由に変更できずに困っております故、
どなたか詳しい方、力をお貸しください。
よろしくお願いいたします。