Search code examples
swiftuitableviewopacity

I have my UITableViewCell Opacity Change on Scroll, but there is a second or two delay on user interaction. How can I fix this?


Trying to get it so the opacity is set to 0, but then when the cell is displayed, have it be one. So I have a kind of fade in look when scrolling through posts. I got the look I want, but then when I scroll, I have like a second or two on unable to interact with the tableview.

I am kinda new at Swift as well so any help would be great!!

Here is my code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PostCell {

        cell.contentView.alpha = 0

        cell.updateUI(postData: posts[indexPath.row])

        return cell
    } else {
        return UITableViewCell()
    }

}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    UIView.animate(withDuration: 0.4) {
        cell.contentView.alpha = 1
    }
}

Solution

  • UIView.animate will block user interaction by default. You can try add .allowUserInteraction options.

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        UIView.animate(withDuration: 0.4, delay: 0, options: UIViewAnimationOptions.allowUserInteraction, animations: {
            cell.contentView.alpha = 1
        }, completion: nil)
    }