简体   繁体   English

3秒钟不触摸时淡出按钮,用户触摸屏幕时淡入按钮

[英]Fade out button when not touching for 3 seconds and fade in when user touches the screen

Problem: 问题:

I have a UIButton that I'd like to fade out for a few seconds when the user doesn't touch the screen and fade them in when the user touches the screen. 我有一个UIButton,我想在用户不触摸屏幕时淡出几秒钟,并在用户触摸屏幕时淡入几秒钟。 I think I might need to use a timer and some animation in the viewdidload part 我想我可能需要在viewdidload部分中使用计时器和一些动画

@IBOutlet var startStopButton:  UIButton!

@IBAction func startStopButtonTapped(_ sender: UIButton) {

}


override func viewDidLoad() {
    super.viewDidLoad() }

I can help you here. 我可以在这里帮助您。 Let me preface this answer with a usability concern. 让我以可用性问题作为开头。 I'm not sure it makes sense to tap anywhere on the screen to reset (fade in) the button after X seconds (will the user know to do that?). 我不确定在X秒钟后点击屏幕上的任何位置以重置(淡入)按钮是否有意义(用户会知道这样做吗?)。 You might want to add a button or some kind of text indicator to "tap here to reset the button(s)". 您可能想添加一个按钮或某种文本指示器以“在此处轻按以重置按钮”。 This all ultimately depends on your application, but just throwing that out there :) 最终,这一切都取决于您的应用程序,只是将其扔掉了:)

On with the answer! 关于答案!

The general idea here is you want to have a timer that runs a method every 1 second. 这里的一般想法是您想要一个计时器,该计时器每1秒运行一次方法。 If the timer hits zero, then you fade out and disable the startStop button. 如果计时器为零,则淡出并禁用startStop按钮。 If the user taps anywhere on the screen (via gesture recognizer on the whole view), then fade the button in and enable it. 如果用户点击屏幕上的任意位置(通过整个视图上的手势识别器),则淡入按钮并启用它。

Now for the code part! 现在是代码部分!

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")
}
}

Hope that makes sense, but do let me know if you have any more questions! 希望这是有道理的,但是如果您还有其他问题,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM