【问题标题】:Tap To Play Button Flashing Stops点击播放按钮闪烁停止
【发布时间】:2017-11-22 19:05:15
【问题描述】:

我有一个游戏,对于主页,我希望“点击播放”按钮淡入淡出。这是我的代码:

import UIKit

class HomePageViewController: UIViewController {

@IBOutlet weak var highscoreLabel: UILabel!
@IBOutlet weak var taptoplay: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    flashing(buttonName: taptoplay)

    let highscore = UserDefaults.standard.integer(forKey: "highscore")
    highscoreLabel.text = "Highscore: \(highscore)"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func flashing(buttonName:UIButton) {
    UIView.animate(withDuration: 1, delay: 0, options: UIViewAnimationOptions.autoreverse, animations: {
        buttonName.alpha = 1
        buttonName.alpha = 0
    }, completion: nil)

}

}

我的问题是文本完全按照我想要的方式闪烁了大约两次,但随后它就消失了,我什至无法单击按钮。事实上,即使它正确闪烁,我也无法单击该按钮。

在我添加此代码之前,该按钮工作正常,并且所有链接仍然正确。感谢您的帮助。

【问题讨论】:

    标签: ios swift uiview uibutton


    【解决方案1】:

    当您为按钮设置动画时,您对它所做的最后一件事是将其 alpha 设置为 0。不知何故,您需要在按钮完成动画后将 alpha 设置回 1,或者更改顺序,并设置首先将 alpha 设为 0。

    编辑:另外,用户交互的默认设置是nil,所以你需要添加另一个选项:allowUserInteraction

    希望这会有所帮助。

    【讨论】:

    • 您好,感谢您的快速回复。当我更改顺序时,它的动画效果很完美,但是我仍然不能在它动画的时候点击按钮,然后它突然又变成 1 alpha,然后我可以按下按钮,但它仍然不会动画。
    • 我认为添加两个选项的方法是这样做:options: [UIViewAnimationOptions.autoreverse, .allowUserInteraction]
    • 我添加了这个,我还添加了 .repeat,现在它运行完美!
    • 很高兴我能帮上忙 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-09
    • 2023-04-09
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多