・説明
元々step1という画像が非表示になっていて、ボタンを1回押下するとstep1という画像が表示され、二回押下するとstep1という画像が非表示なる。
ループ処理を行いたいのですが、二回目の処理が行われません。どこを訂正すればよろしいのでしょうか。
(if文のみで実行した時はボタンの押下した回数によって表示・非表示を行うことはできました。)
また、repeatを使用したところ、3回目にアプリが固まってしまいました。
・コード
//
// ViewController_ken1.swift
// testc
//
// Created by Apple on 2017/10/28.
// Copyright © 2017年 Apple. All rights reserved.
//
import UIKit
class ViewController_ken1: UIViewController {
@IBOutlet weak var step1: UIImageView! override func viewDidLoad() { step1.isHidden = true super.viewDidLoad() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }
/////step1表示・非表示コード(ループ処理)
var tapCount = 0
@IBAction func step11(_ sender: Any) { tapCount += 1 for tapCount in 0..<2{ if tapCount == 1 { step1.isHidden = false }else if tapCount == 2 { step1.isHidden = true } } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destinationViewController. // Pass the selected object to the new view controller. } */
・repeat使用したコード
/////repeatを使用してstep1表示・非表示コード
省略
var tapCount = 0 @IBAction func step11(_ sender: Any) { tapCount += 1 repeat{ if tapCount == 1 { step1.isHidden = false }else if tapCount == 2 { step1.isHidden = true } }while tapCount == 3 }
}
回答1件
あなたの回答
tips
プレビュー