【问题标题】:Fade out button when not touching for 3 seconds and fade in when user touches the screen3秒未触摸时淡出按钮,当用户触摸屏幕时淡入
【发布时间】:2017-09-12 01:52:05
【问题描述】:

问题:

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

@IBOutlet var startStopButton:  UIButton!

@IBAction func startStopButtonTapped(_ sender: UIButton) {

}


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

【问题讨论】:

    标签: ios xcode animation button fadein


    【解决方案1】:

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

    继续回答!

    这里的一般想法是您希望有一个每 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")
    }
    }
    

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

    【讨论】:

    • 非常感谢您的帮助。它工作得很好,但是当 startStopButton 淡入淡出时,我似乎无法禁用计时器,因为它再次打开计时器回到 0,然后自动淡出。如何禁用此功能?
    • 欢迎您的帮助!你是对的,在计时器继续开火,直到按钮淡出。当按钮淡入时,它会自动在另一个 5 秒计时器上,直到点击屏幕。我认为这是你想要的行为。你希望发生什么?
    • 谢谢,我只想让计时器停止并恢复正常,这样当 startStopButton 重新出现时就没有 5 秒计时器。在它自动消失的那一刻,我希望它在淡入时不要消失。
    • 我编辑了我的答案。点击方法(点击手势识别器)所需要做的就是终止计时器。
    • 再次感谢,但是当我这样做时它仍然无法正常工作,现在点击手势识别器不起作用。
    猜你喜欢
    • 1970-01-01
    • 2011-04-10
    • 2020-12-30
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多