2017-12-08 3 views
0

탭 제스처 인식기를 사용하고 있기 때문에 사용자가 화면을 탭하면 5 초 동안 페이드 아웃되고 사용자가 화면이 다시 그들은 화면에 누르고 버튼을 페이드페이드 인 버튼을 누르면 다시 자동으로 페이드 아웃되지 않습니다.

문제점은 다음과 같습니다. 그렇게 자동으로 다시 퇴색하지 않습니다 페이드 때

I 버튼을 비활성화 할 수 없습니다. 나는 타이머를 무효화하려고 시도했지만 작동하지 않았다. 더 구체적으로하고 싶은 일 :

앱이로드되면 사용 중지 된 'Start Stop Button'이 표시됩니다. - 화면의 아무 곳이나 누르면 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

왜 애니메이션으로 버튼을 숨기고 대신에 알파 속성으로 재생할 수 있습니까? –

+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'에는 컨텍스트 유형이 필요합니다. 'thids의 의미를 알고 있습니까? –

+0

'var timer : Timer? = nil'이면, 어떤 타입의 timer인지 알 필요가 있습니다. – aksh1t

+0

고마워요, 자동으로 다시 페이드 아웃하지 않고 사라지고 다시 나타납니다. 이제 탭이 비활성화되어 화면에서 다시 탭할 수 없습니다. –

관련 문제