UITableView header cannot work with UIViewAnimationOptions.Repeat

I want the title of the table view to blink, but it doesn't work.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()
    headerView.backgroundColor = UIColor.redColor()

    UIView.animateWithDuration(
        0.3, delay: 0,
        options: UIViewAnimationOptions.Repeat,
        animations: { headerView.alpha = 0 },
        completion: nil)

    return headerView
}

      

+3


source to share


4 answers


You must set UIView.setAnimationsEnabled(true);

before calling the animation like this:



func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let headerView = UIView()
    headerView.backgroundColor = UIColor.redColor()

    UIView.setAnimationsEnabled(true)

    UIView.animateWithDuration(
        0.3, delay: 0,
        options: UIViewAnimationOptions.Repeat,
        animations: { headerView.alpha = 0 },
        completion: nil)

    return headerView
}

      

+2


source


try to do it with a timer, this problem might be solved.



0


source


Try to move the animation block to the display method of the delegate `displayHeaderView which will work

Swift 3

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
            UIView.animate(
                withDuration: 0.3, delay: 0,
                options: UIViewAnimationOptions.repeat,
                animations: { view.alpha = 0
            },
                completion: nil)
}

      

0


source


It will not display because no view has been added to the supervisor. Try after reloading the table:

let headerView =   tableVIew.headerView(forSection: 0)
UIView.transition(with: tableVIew, duration: 2, options: [.repeat], animations: {
          headerView?.alpha = 0
        })

      

0


source







All Articles