每次重置标题时如何阻止UIButton突出显示/闪烁?

时间:2015-08-06 03:41:34

标签: ios swift cocoa-touch uibutton

我使用UIButton作为标签在其标题中显示一些数字。每次调用该函数以重置此按钮的标题时,它都会闪烁。它不会影响功能但会损害用户体验。我想知道是否有办法阻止UIButton突然出现这种突出行为?

提前致谢!

编辑:以下是我只是调用委托方法updateDigits来刷新按钮标题的代码。

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, DigitsEntryViewDelegate {

    @IBOutlet weak var tipField1: UIButton!
    @IBOutlet weak var tipField2: UIButton!

    var buttonsArray = [UIButton]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tipField1.tag = 0
        self.tipField2.tag = 1

        self.tipField1.addTarget(self, action: "inputFieldClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        self.tipField2.addTarget(self, action: "inputFieldClicked:", forControlEvents: UIControlEvents.TouchUpInside)

        self.buttonsArray.append(self.tipField1)
        self.buttonsArray.append(self.tipField2)
    }


    func inputFieldClicked(sender: UIButton) {
        let anchor: UIView = sender

        let viewControllerForPopover = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DigitEnter") as! DigitsEntryViewController

        // set popover API values
        viewControllerForPopover.currentDigitsString = sender.currentTitle
        viewControllerForPopover.tagNumber = sender.tag

        viewControllerForPopover.delegate = self
        viewControllerForPopover.preferredContentSize = CGSizeMake(240, 320)
        let popover = UIPopoverController(contentViewController:viewControllerForPopover)
        popover.presentPopoverFromRect(anchor.frame, inView: anchor.superview!, permittedArrowDirections: UIPopoverArrowDirection.Down, animated: false)
    }

    func updateDigits(returnedDigits: String, tagNumber: Int) {
        self.buttonsArray[tagNumber].setTitle(returnedDigits, forState: UIControlState.Normal)
    }
}

3 个答案:

答案 0 :(得分:9)

尝试将按钮类型设置为“自定义”,而不是“系统”。

答案 1 :(得分:1)

我可能会弄错,但闪烁看起来可能是因为数据传输延迟造成的;您正在尝试在仍然检索新标题时更改标题,因此闪光。

我会:

  1. 检索数据并将其存储在临时但专门创建的字符串中。
  2. 在传递数据之后,调用方法将按钮的文本更改为存储的字符串的文本。

答案 2 :(得分:0)

这可以解决您的问题:

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}];