2017-12-08 4 views
0

私はタップジェスチャー認識機能を使用しています。そのため、ユーザーが画面をタップすると、ボタンは5秒間フェードアウトしてから、画面には、再び彼らは、画面をタップするとボタンがフェードインフェードインボタンで自動的に再びフェードアウトしません

問題がある:それはそう、それは再び自動的にフェードアウトしませんフェードインするとき

私はボタンを無効にすることはできません。私はタイマーを無効にしようとしましたが、それは動作しませんでした。私がやりたいことをより具体的には:

をアプリの負荷に、あなたは有効になって「スタートストップボタン」を参照してください。 - 画面上の任意の場所をタップすると、5秒のタイマーが消えてボタンを消して無効にします。ボタンがフェードアウトして無効にしたら、私は、後ろにボタンをフェードイン、それを有効にして、私が最初にそれをタップする前にそれがあったように、ボタンが現れるので、タイマーを殺すために、画面上の任意の場所をタップすることができます。

class ViewController: UIViewController { 
    // Create these 3 properties in the top of your class 
    var secondToFadeOut = 5 // How many second do you want the view to idle before the button fades. You can change this to whatever you'd like. 
    var timer = Timer() // Create the timer! 
    var isTimerRunning: Bool = false // Need this to prevent multiple timers from running at the same time. 


@IBOutlet weak var startStopButton: UIButton! // The outlet for your button. This is used to fade it in and out, and enable/disable it. 

override func viewDidLoad() { 
    super.viewDidLoad() 
    startStopButton.isEnabled = true 
    runTimer() 

    // Add a tap gesture recognizer to the main view to determine when the screen was tapped (for the purpose of resetting the timer). 
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))) 
    self.view.addGestureRecognizer(tapRecognizer) 

} 

func runTimer() { 
    // Create the timer to run a method (in this case... updateTimer) every 1 second. 
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(ViewController.updateTimer)), userInfo: nil, repeats: true) 
    // Set the isTimerRunning bool to true 
    isTimerRunning = true 
} 

@objc func updateTimer() { 
    // Every 1 second that this method runs, 1 second will be chopped off the secondToFadeOut property. If that hits 0 (< 1), then run the fadeOutButton and invalidate the timer so it stops running. 
    secondToFadeOut -= 1 
    print(secondToFadeOut) 
    if secondToFadeOut < 1 { 
     fadeOutButton() 
     timer.invalidate() 
     isTimerRunning = false 
    } 
} 

@objc func tap(_ gestureRecognizer: UITapGestureRecognizer) { 
    // When the view is tapped (based on the gesture recognizer), reset the secondToFadeOut property, fade in (and enable) the button. 
    //secondToFadeOut = 5 
    fadeInButton() 
    timer.invalidate() 
    //if isTimerRunning == false { 
    // runTimer() 
    //} 
} 

func fadeOutButton() { 
    // Fade out your button! I also disabled it here. But you can do whatever your little heart desires. 
    UIView.animate(withDuration: 0.5) { 
     self.startStopButton.alpha = 0.25 
    } 
    self.startStopButton.isEnabled = false 
} 
func fadeInButton() { 
    // Fade the button back in, and set it back to active (so it's tappable) 
    UIView.animate(withDuration: 0.5) { 
     self.startStopButton.alpha = 1 
    } 
    self.startStopButton.isEnabled = true 
} 


@IBAction func startStopButtonPressed(_ sender: UIButton) { 
    print("Start Stop Button Pressed") 
} 
} 
+0

フェードイン時にボタンを無効にできないのはなぜですか? (このコードの比較的大きな金額を見て、私はあなたが必要以上に物事をより複雑に作っている感覚を得る。) –

+1

あなただけの代わりに、alphaプロパティで遊んで、アニメーションとボタンを非表示にしていないのはなぜ? –

+0

上記の(^^^)コメントに沿って進むには、 'isHidden'プロパティをアニメートすることで、有効/無効をする必要はありません。 – dfd

答えて

0

私の最高の推測では、あなたがあなたの現在のタイマを無効にした後も記憶に残っている不正Timerオブジェクトを持っているということであり、それはそれがフェードインした後、再度ボタンをフェードアウトさせている。

Iあなたのクラスにいくつかの編集を加えました。コードを確認してください:

class ViewController: UIViewController { 
// Create these 3 properties in the top of your class 
var secondToFadeOut = 5 // How many second do you want the view to idle before the button fades. You can change this to whatever you'd like. 
var timer? = nil // Create the timer! 

@IBOutlet weak var startStopButton: UIButton! // The outlet for your button. This is used to fade it in and out, and enable/disable it. 

override func viewDidLoad() { 
    super.viewDidLoad() 
    startStopButton.isEnabled = true 
    runTimer() 

    // Add a tap gesture recognizer to the main view to determine when the screen was tapped (for the purpose of resetting the timer). 
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tap(_:))) 
    self.view.addGestureRecognizer(tapRecognizer) 
} 

func runTimer() { 
    if timer == nil { 
     timer = Timer.scheduledTimer(timeInterval: secondToFadeOut, target: self, selector: (#selector(ViewController.timerFired)), userInfo: nil, repeats: false) 
    } 
} 

@objc func timerFired() { 
    timer = nil 
    if self.startStopButton.isEnabled { 
     fadeOutButton() 
    } else { 
     fadeInButton() 
    } 
} 

@objc func tap(_ gestureRecognizer: UITapGestureRecognizer) { 
    runTimer() 
} 

func fadeOutButton() { 
    UIView.animate(withDuration: 0.5) { 
     self.startStopButton.alpha = 0.25 
    } 
    self.startStopButton.isEnabled = false 
} 

func fadeInButton() { 
    UIView.animate(withDuration: 0.5) { 
     self.startStopButton.alpha = 1 
    } 
    self.startStopButton.isEnabled = true 
} 

@IBAction func startStopButtonPressed(_ sender: UIButton) { 
    print("Start Stop Button Pressed") 
} 
} 
+0

ありがとうございますが、「nil」には文脈型が必要です。「あなたは何を意味するのですか? –

+0

'var timer:Timer? = nil'の場合、タイマーの種類を知る必要があります。 – aksh1t

+0

おかげで、それが消え、再び自動的にフェードアウトが、今それは私が再び画面をタップせないようにタップが無効になっていなくて、今再び表示されます。 –

関連する問題