Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift中使用计时器更改标签时,防止UIButton标签不闪烁_Ios_Swift_Uibutton - Fatal编程技术网

Ios 在swift中使用计时器更改标签时,防止UIButton标签不闪烁

Ios 在swift中使用计时器更改标签时,防止UIButton标签不闪烁,ios,swift,uibutton,Ios,Swift,Uibutton,我正在尝试开发将计数器值添加到UIButton标签中,代码如下: override func viewDidLoad() { super.viewDidLoad() let counterAttribute = Settings.changeButtonStyle(fullString: String(format: "Continue in %d secs", self.otpTimer) as NSString, boldPartsOfString: "Continue",

我正在尝试开发将计数器值添加到UIButton标签中,代码如下:

override func viewDidLoad() {
    super.viewDidLoad()
    let counterAttribute = Settings.changeButtonStyle(fullString: String(format: "Continue in %d secs", self.otpTimer) as NSString, boldPartsOfString: "Continue", changeFontString: "\(self.otpTimer)")
    self.continueButton.setAttributedTitle(counterAttribute, for: .normal)
    Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(self.updateTimer)), userInfo: nil, repeats: true)
}

func updateTimer() {
    self.otpTimer = self.otpTimer - 1
    let counterAttribute = Settings.changeButtonStyle(fullString: String(format: "Continue in %d secs", self.otpTimer) as NSString, boldPartsOfString: "Continue", changeFontString: "\(self.otpTimer)")
    self.continueButton.setAttributedTitle(counterAttribute, for: .normal)
}
并更改UIButton标签功能

static func changeButtonStyle(fullString: NSString, boldPartsOfString: String, changeFontString: String) -> NSAttributedString {
    let nonBoldFontAttribute = [
        NSFontAttributeName: MyFont.caption2,
        NSForegroundColorAttributeName: MyColor.white
    ]

    /* convert NSString to be added as NSMutableAttributedString */
    let changeThemeString = NSMutableAttributedString(string: fullString as String, attributes: nonBoldFontAttribute)

    if boldPartsOfString.count > 0 {
        let boldFontAttribute = [NSFontAttributeName: MyFont.subheading]
        changeThemeString.addAttributes(boldFontAttribute, range: fullString.range(of: boldPartsOfString))
    }

    if changeFontString.count > 0 {
        let colorFontAttribute = [NSForegroundColorAttributeName: MyColor.darkPink]
        changeThemeString.addAttributes(colorFontAttribute, range: fullString.range(of: changeFontString))
    }

    return changeThemeString
}

问题是,每当更改计时器时,UIButton标签都会闪烁。这就是为什么我想知道如何通过更改上面的代码来创建不闪烁的UIButton标签。谢谢。

在这两次通话之间添加标题更新:

UIView.setAnimationsEnabled(false)
/// update button title
UIView.setAnimationsEnabled(true)

我找到了一种解决方法,我们需要添加UIView.performWithoutAnimation,以防止不刷新UIView

UIView.setAnimationsEnabled(false)
myTimer = myTimer - 1
UIView.performWithoutAnimation {
    /* Change UIButton Label */
    UIView.setAnimationsEnabled(true)
}

您是否可以附加正在发生的事情的gif?尝试在主队列中执行以下代码->self.continueButton.setAttributedTitleCountertAttribute,例如:。normal。