始めましてiPhoneアプリ制作しているものです
###前提・実現したいこと
通常時はスクロールをONにして TestDrawクラス内にあるarcとinoパラメーターをタッチした時にスクロールを止めたい
###発生している問題・エラーメッセージ
Iphoneシミュレーターを起動してarcを触ると self.tou.isScrollEnabled = isScrollEnableの所にエラーがでて落ちる (fatal error: unexpectedly found nil while unwrapping an Optional value)
viewDidLoad内の情報を他クラスから変更する方法はないでしょうか?ご回答いただけるとありがたいです
###該当のソースコード
swift
1import UIKit 2import CoreGraphics 3 4class ViewController: UIViewController { 5 6 7 @IBOutlet weak var tou: UIScrollView! 8 var isScrollEnable = true { 9 didSet { 10 viewDidLoad() 11 } 12 } 13 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 18 self.tou.isScrollEnabled = isScrollEnable /*ここを変更したいですがエラーが出ます(fatal error: unexpectedly found nil while unwrapping an Optional value)*/ 19 //スクロール画面の設定 20 self.tou.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height) 21 self.tou.contentSize = CGSize(width: self.view.frame.size.width, height: 900) 22 self.tou.showsVerticalScrollIndicator = false 23 24 //パラメーターボタンの設置 25 let testDraw = TestDraw(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 900 )) 26 testDraw.backgroundColor = UIColor.clear() 27 28 self.tou.addSubview(testDraw) 29 self.view.addSubview(self.tou) 30 31 } 32 33 override func didReceiveMemoryWarning() { 34 super.didReceiveMemoryWarning() 35 // Dispose of any resources that can be recreated. 36 } 37 38 39} 40 41
swift
1import UIKit 2 3class TestDraw: UIView { 4 5 //パラメーターボタン 6 var parameterArc:UIBezierPath! 7 var parameterIno:UIBezierPath! 8 9 10 var radian:CGFloat = -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2) 11 var touchPoint:CGPoint! 12 13 14 //ボタン内をタッチするとパラメーターが動く 15 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 16 17 if let touch = touches.first as UITouch? { 18 self.touchPoint = touch.location(in: self) 19 20 21 22 if parameterArc.contains(touchPoint!) { 23 var viewcon = ViewController() 24 viewcon.isScrollEnable = false //ここでタッチした時にスクロールを止めたいです! 25 26 //タッチした位置に合わせてパラメーターが変化 27 self.radian = -atan2(100 - touchPoint.x, 350 - touchPoint.y) - CGFloat((M_PI)/2) 28 29 if self.radian >= CGFloat((M_PI)*4/5 - (M_PI)/2) { 30 self.radian = CGFloat((M_PI)*4/5 - (M_PI)/2) 31 } else if self.radian <= -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2) { 32 self.radian = -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2) 33 } 34 35 } 36 37 self.setNeedsDisplay() 38 39 } 40 } 41 42 //タッチ後動かしてもパラメーターを変化させる 43 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 44 45 46 if let touch = touches.first as UITouch? { 47 self.touchPoint = touch.location(in: self) 48 49 if parameterArc.contains(touchPoint!) { 50 51 //パラメータの変化 52 self.radian = -atan2(100 - touchPoint.x, 350 - touchPoint.y) - CGFloat((M_PI)/2) 53 54 if self.radian >= CGFloat((M_PI)*4/5 - (M_PI)/2) { 55 self.radian = CGFloat((M_PI)*4/5 - (M_PI)/2) 56 } else if self.radian <= -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2) { 57 self.radian = -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2) 58 } 59 } 60 61 self.setNeedsDisplay() 62 } 63 64 } 65 66 67 //パラメーターの設定、設置 68 override func draw(_ rect: CGRect){ 69 70 self.parameterIno = UIBezierPath(arcCenter: CGPoint(x:120, y:350), radius: 40,startAngle: -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2), endAngle: CGFloat((M_PI)*4/5 - (M_PI)/2), clockwise: true) 71 72 let arcColor = UIColor(red: 0, green: 0.5, blue: 1, alpha: 0.8) 73 arcColor.setStroke() 74 parameterArc.lineWidth = 4 75 parameterArc.lineCapStyle = CGLineCap.round 76 parameterIno.stroke() 77 78 79 self.parameterIno = UIBezierPath(arcCenter: CGPoint(x:120, y:350), radius: 40,startAngle: -1 * CGFloat ((M_PI)*4/5 + (M_PI)/2), endAngle: CGFloat(radian) , clockwise: true) 80 81 let inoColor = UIColor(red: 0, green: 0.2, blue: 0.5, alpha: 1) 82 inoColor.setStroke() 83 parameterIno.lineWidth = 4.4 84 parameterIno.lineCapStyle = CGLineCap.round 85 parameterIno.stroke() 86 87 } 88 89}
##補足情報(言語/FW/ツール等のバージョンなど)
xcode8 beta
swift3.0
回答2件
あなたの回答
tips
プレビュー