Ios Swift-如何更改UIButton的颜色';s NSMUTABLeAttributeString在设置之后

Ios Swift-如何更改UIButton的颜色';s NSMUTABLeAttributeString在设置之后,ios,swift,uibutton,nsmutableattributedstring,Ios,Swift,Uibutton,Nsmutableattributedstring,我有两个ui按钮,它们的标题和颜色最初是使用NSMutableAttributedString设置的。正文是 我有一个自定义的UISegmentedControl,当切换段时,我只需要更改按钮的颜色。我现在的做法是颜色确实会改变,但当这种情况发生时会出现难看的闪烁,因为实际的文本会再次设置。我只需要颜色的平滑过渡 var selectedSegmentIndex = 0 { didSet { layoutIfNeeded() UIView.animate

我有两个
ui按钮
,它们的
标题
颜色
最初是使用
NSMutableAttributedString
设置的。正文是

我有一个自定义的
UISegmentedControl
,当切换段时,我只需要更改按钮的颜色。我现在的做法是颜色确实会改变,但当这种情况发生时会出现难看的闪烁,因为实际的文本会再次设置。我只需要颜色的平滑过渡

var selectedSegmentIndex = 0 {
    didSet {

        layoutIfNeeded()
        UIView.animate(withDuration: 0.1) {
            self.setTextForFollowersButton()
            self.setTextForFollowingButton()
            self.layoutIfNeeded()
        }
    }
}
如何仅更改按钮的
NSMutableAttributedString
的颜色?

按钮代码:

var numOfFollowers = 0 {
    didSet {
        setTextForFollowersButton()
    }
}
var numOfFollowing = 0 {
    didSet {
        setTextForFollowingButton()
    }
}

lazy var followersButton: UIButton = {
    let button = UIButton.init(type: .system)
    // ...
    return button
}()

lazy var followingButton: UIButton = {
    let button = UIButton.init(type: .system)
    //...
    return button
}()

func setTextForFollowersButton() {

    let button = self.followersButton
    let buttonText = "Following\n\(String(numOfFollowing))" as NSString
    // other code for 2 lines of text

    var color = UIColor.blue

    if selectedSegmentIndex == 0 {
        color = UIColor.blue
    } else {
        color = UIColor.gray
    }

    let attrString1 = NSMutableAttributedString(string: substring1,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 16),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    let attrString2 = NSMutableAttributedString(string: substring2,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 14),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    attrString1.append(attrString2)
    button.setAttributedTitle(attrString1, for: [])
}

func setTextForFollowingButton() {

    let button = self.followingButton

    let buttonText = "Following\n\(String(numOfFollowing))" as NSString
    // other code for 2 lines of text

    var color = UIColor.blue

    if selectedSegmentIndex == 0 {
        color = UIColor.gray
    } else {
        color = UIColor.blue
    }

    let attrString1 = NSMutableAttributedString(string: substring1,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 16),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    let attrString2 = NSMutableAttributedString(string: substring2,
                                                attributes: [NSMutableAttributedString.Key.font:
                                                    UIFont.boldSystemFont(ofSize: 14),
                                                             NSMutableAttributedString.Key.foregroundColor: color])

    attrString1.append(attrString2)
    button.setAttributedTitle(attrString1, for: [])
}

您应该在
DispatchQueue.main.async{}
中插入
UIView.animate
函数,以实现平滑过渡。始终建议在主线程上异步制作任何类型的UI动画。

另一种方法是使用
UIView来实现平滑过渡,而不闪烁。无动画的性能

DispatchQueue.main.async {
    self.layoutIfNeeded()
    UIView.performWithoutAnimation {

        self.setTextForFollowersButton()
        self.setTextForFollowingButton()

        self.layoutIfNeeded()
    }
}

您是否尝试在
DispatchQueue.main.async{}
中插入
UIView.animate
以使转换顺利进行?@LewWinczynski谢谢,您的回答有效。请把它寄出去,这样我就可以接受了。我不得不将时间改为0.05,这样看起来也更好。很高兴听到这个消息!好的,我会发帖的。:)@LewWinczynski这里有另一种不用动画的方法