탭 제스처 인식기를 사용하고 있기 때문에 사용자가 화면을 탭하면 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")
}
}
왜 페이드 인 할 때 버튼을 비활성화 할 수 없습니까? (상대적으로 많은 양의 코드를 보면 필요한 것보다 복잡한 것을 만드는 느낌이 들게됩니다.) –
왜 애니메이션으로 버튼을 숨기고 대신에 알파 속성으로 재생할 수 있습니까? –
위의 (^^^) 주석을 따라 가려면'isHidden' 속성에 애니메이션을 적용하면 활성화/비활성화 할 필요가 없다는 것을 의미합니다. – dfd