kitchen800
kitchen800

Reputation: 227

Delay animation of different cells of UITableView

I am making an app using swift. I have a UITableView consisting of 5 different rows. I have a UILabel on each row of the table. (This label provides the title of the row) This UILabel is animated so it enters the screen from left to right on each row.

My problem is that when I run my animation all 5 UILabels enter their row at the same time. I want to put a delay between the each UILabels animation. That is I want the UILabel on the first row to enter the screen first, then 1 second later I want the UILabel on the second row to enter the screen, then 1 second later the 3rd UILabel ...etc.

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell!



    let imageView = cell.viewWithTag(1) as! UIImageView
    imageView.image = UIImage(named: imageArray[indexPath.row])

    let imageText = cell.viewWithTag(2) as! UILabel
    imageText.text = sectionName[indexPath.row]
    imageText.textAlignment = .Center



    //starting position for animation of imageText
    imageText.center.x = self.view.frame.width - 500


    //animating UILables comming into screen
    UIView.animateWithDuration(3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: ({

        imageText.center.x = ((self.view.frame.width) / 1)


    }), completion: nil)




    return cell

}

Upvotes: 1

Views: 1185

Answers (1)

Sean Kladek
Sean Kladek

Reputation: 4446

Add a delay based on the index path. The 1 in the delay is the number of seconds you want between animations.

let delay = 1 * indexPath.row
UIView.animateWithDuration(3, delay: delay, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [], animations: ({
    imageText.center.x = ((self.view.frame.width) / 1)
}), completion: nil)

I'm assuming that the 5 rows are all that the table view will need to display and that all are on screen at all times, meaning, you don't need to consider how to handle the animation as the rows scroll. If scrolling is a consideration, then this will likely need to change to only animate in when the table view is initially displayed.

Upvotes: 2

Related Questions