【问题标题】:how can I animate a button to fade out and then fade back in with a function using swift 3如何使用swift 3为按钮设置动画以淡出然后淡入
【发布时间】:2017-02-13 00:51:31
【问题描述】:

在使用 swift 3 的 Xcode 8 中,我有 2 个函数。当调用“HideButton”函数时,它会执行适当的淡出动画,但当调用“ShowButton”函数时,不会发生淡入动画。 “ShowButton”动画功能是否有问题,如何解决?

func HideButton() {

    UIView.animate(withDuration: 0.2, delay: 0, animations: {
    self.MainButton.alpha = 0
    }, completion: { finished in
    self.MainButton.isHidden = true
    })

    Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.ShowButton), userInfo: nil, repeats: false)

}

func ShowButton() {

    UIView.animate(withDuration: 0.2, delay: 0, animations: {
    self.MainButton.alpha = 1
    }, completion: { finished in
    self.MainButton.isHidden = false
    })

    Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.HideButton), userInfo: nil, repeats: false)

}

【问题讨论】:

标签: animation swift3 xcode8


【解决方案1】:

在 hideButton 函数中将 isHidden 属性设置为 true。因此,这将限制按钮的功能并阻止您尝试在 showButton 中呈现的视觉变化。因此,您需要使按钮在动画之前不隐藏,而不是在完成处理程序中。

类似这样的:

func hideButton() {

UIView.animate(withDuration: 0.2, delay: 0, animations: {
self.MainButton.alpha = 0
}, completion: { finished in
self.MainButton.isHidden = true
})

Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.ShowButton), userInfo: nil, repeats: false)

}

func showButton() {
self.MainButton.isHidden = false

UIView.animate(withDuration: 0.2, delay: 0, animations: {
self.MainButton.alpha = 1
}, completion: { finished in

})

Timer.scheduledTimer(timeInterval: 1.2, target: self, selector: #selector(GameViewController.HideButton), userInfo: nil, repeats: false)

}

因为在 showButton 动画开始时 alpha 将为零,尽管在动画发生之前 isHidden 属性为 false,您仍然可以获得所需的视觉效果。请注意,我重命名了您的函数以保留约定(funcs 应为小写)!

【讨论】:

  • 没问题!干杯!
猜你喜欢
  • 2017-01-28
  • 2015-03-31
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多