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

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

问题:

我有一个 UIButton,我想在用户不触摸屏幕时淡出几秒钟,并在用户触摸屏幕时淡入。我想我可能需要在 viewdidload 部分使用计时器和一些动画

@IBOutlet var startStopButton:  UIButton!

@IBAction func startStopButtonTapped(_ sender: UIButton) {

}


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

这边我可以帮你。让我以可用性问题作为这个答案的开头。我不确定在 X 秒后点击屏幕上的任意位置以重置(淡入)按钮是否有意义(用户会知道这样做吗?)。您可能想要向 "tap here to reset the button(s)" 添加按钮或某种文本指示器。这一切最终都取决于您的应用程序,但只是把它扔在那里 :)

开始回答!

这里的一般想法是你想要一个每 1 秒运行一个方法的计时器。如果计时器归零,则您会淡出并禁用 startStop 按钮。如果用户点击屏幕上的任意位置(通过整个视图上的手势识别器),则淡入并启用按钮。

现在是代码部分!

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

希望这是有道理的,但如果您还有任何问题,请告诉我!