1

Edit: I didn't realize that it is the Animates (in segue in storyboard) and animated in pushViewController() which does the job.

How to smooth transition to next view when we tap on a row in table view. I want to achieve similar smooth transitioning effect as Apple's Settings app. Right now my transition happens immediately as soon as row is touched.

On Apple's settings app when you tap on a row on Settings menu, it first highlights the row then smoothly transitions to next view. This is what I want to achieve.

I've searched but can't find any relevant solution. Any help?

Following is my code for row select

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    tableView.deselectRow(at: indexPath, animated: true)
    
    //.....
    self.navigationController?.pushViewController(destinationVC, animated: false)
}
Parth
  • 2,682
  • 1
  • 20
  • 39
Raymond
  • 1,108
  • 13
  • 32

1 Answers1

1

You need to change the presentation style. So click on your segue in the storyboard. Then select Kind as Push from the dropdown in Attributes Inspector on the right side.

Also, to present it programmatically, you need to set an identifier in the first text field in Attributes Inspector. After that in your tableView(_:didSelectRowAt:) add this.

performSegue(withIdentifier: "SegueIdentifier", sender: nil)

If you want a delayed action then you can use asyncAfter, like suggested here.

DispatchQueue.main.asyncAfter(deadline: .now() + 0.33) {
   self.deselectRow(at: indexPath, animated: true)
   self.performSegue(withIdentifier: "SegueIdentifier", sender: nil)
}
Parth
  • 2,682
  • 1
  • 20
  • 39
  • Just wanted to add that Animates has to be selected too. It's the animation property which has this effect. I didn't realize it. Thanks for the help. – Raymond Feb 16 '21 at 16:43
  • NP! :) "Just wanted to add that Animates has to be selected too" is this another property in the attributes inspector? – Parth Feb 16 '21 at 16:45
  • Also, could you accept the answer if this worked for you? Thanks :) – Parth Feb 16 '21 at 16:45
  • It does work however it doesn't show the same animation effect as Settings app. Settings app has a little more delay before it transitions to the next view. Do you know how to achieve the same level of delay as Settings app? – Raymond Feb 16 '21 at 16:56
  • Yeah! You need to manually initiate the segue I think. Let me look it up. – Parth Feb 16 '21 at 17:01
  • I'm already doing it manually via pushViewController() – Raymond Feb 16 '21 at 17:05
  • Wait how are you using pushViewController if it's a segue based animation? – Parth Feb 16 '21 at 17:06
  • No. I'm not using segue when using pushViewController. It's either segue or pushViewController. In both cases I observe that my apps is quicker than Settings app. – Raymond Feb 16 '21 at 17:12
  • Hmm, interesting. And what is the source of click? A UIButton or a TableViewCell? – Parth Feb 16 '21 at 17:14
  • It's TableViewCell. Have a look at Apple's Settings app first and try your app to observe the delay after tapping on a row in a table view. I've a feeling that in Settings app there is more delay after tap and before showing next view. – Raymond Feb 16 '21 at 17:18
  • You are right. The settings app does add a slight delay. Does the `asyncAfter` help? If not I will look into it in few hours. – Parth Feb 16 '21 at 17:24
  • I think my updated answer should do exactly what you are looking for! :3 Cheers! – Parth Feb 16 '21 at 17:35
  • Np! And thanks for pointing it out. I'll update my didSelect too :) – Parth Feb 16 '21 at 17:56