###分からないこと
超初心者ですが、回答いただけると嬉しいです。
Timerを使って画像を一定時間ごとに表示して行った後、
次のViewControllerに遷移をしたいのですが、
どこにどのように入れれば良いのか教えていただけないでしょうか。
同じようにTiemrを使って行えば良いのでしょうか。
###対象ソースコード
swift
1 2 var timer:Timer! 3 4 var count:Int = 0 5 6 var dispImageNo = 0 7 8 var imageView = UIImageView() 9 10 11 override func loadView() { 12 let view07A = UIView() 13 view07A.frame = CGRect(x:0, y:0, width:375, height:669) 14 view07A.backgroundColor = .white 15 self.view = view07A 16 17 18 } 19 20 @objc func timerAction(sender:Timer){ 21 22 count = count + 1 23 24 dispImageNo = count % 7 25 26 print("カウントダウン") 27 28 pictureView() 29 30 31 } 32 override func viewDidLoad() { 33 super.viewDidLoad() 34 35 createTimer() 36 37 } 38 39 func createTimer () { 40 41 timer = Timer.scheduledTimer(timeInterval:5.0, 42 target:self, 43 selector: #selector(ViewController_7A.timerAction(sender:)), 44 userInfo:nil, 45 repeats:true) 46 47 48 DispatchQueue.main.asyncAfter(deadline: .now() + 40.0) { 49 print("終わり") 50 51 } 52 53 } 54 func pictureView() { 55 56 let pictureArray = [ 57 //画像 58 ] 59 60 61 let name = pictureArray[dispImageNo] 62 63 let image = UIImage(named:name) 64 65 self.imageView.image = image 66 67 68 imageView.frame = CGRect(x:0, y:0, width:200, height:300) 69 70 let screenWidth:CGFloat = view.frame.size.width 71 let screenHeight:CGFloat = view.frame.size.height 72 73 74 self.imageView.center = CGPoint(x:screenWidth/2, y:screenHeight/2) 75 76 view.addSubview(self.imageView) 77 78 } 79 80 @objc func tapp(_ sender:UIButton) { 81 82 let nextvc = ViewController_7A() 83 84 self.present(nextvc, animated: true, completion: nil) 85 } 86 87 88} 89 90 91 92 93 94 95class ViewController_8:UIViewController { 96 97 override func loadView() { 98 let view = UIView() 99 view.frame = CGRect(x:0, y:0, width:375, height:669) 100 view.backgroundColor = .white 101 self.view = view 102 103 let lbl1 = UILabel() 104 lbl1.frame = CGRect(x:40, y:100, width:130, height:50) 105 lbl1.text = "言葉" 106 lbl1.textColor = .black 107 view.addSubview(lbl1) 108 109 let tt_answer = UITextView() 110 tt_answer.frame = CGRect(x:40, y:150, width:180, height:100) 111 tt_answer.textColor = .black 112 tt_answer.text = "言葉" 113 view.addSubview(tt_answer) 114 115 116 117 118 } 119 120} 121
そもそもViewControllerの遷移を繰り返すとどんどんメモリに積み上げられるので、メモリ不足になりますよ。同じコントローラ内で画像を差し替えるようにすることを考えた方がよろしいかと思います。
修正依頼ありがとうございます。メモリのこと考えていませんでした。差し替える方法で作成してみます。
回答1件
あなたの回答
tips
プレビュー