Regarding the logic of 8:33 in this video, I have some details, hopefully it would help someone out one day.
@objc func handlePanGuesture(guesture: UIPanGestureRecognizer){
let translation = guesture.translation(in: self.view)
var x = translation.x
//if menueIsShown {
//x += self.menueWidth
//}
x = min(self.menueWidth, x) // moving left x increate
x = max(0, x) // moving right x decrease
if guesture.state == .changed {
let transform = CGAffineTransform(translationX: x, y: 0)
// self.showAnimation(transform: transform, onComplete: nil)
self.menuViewController.view.transform = transform
self.navigationController?.view.transform = transform
}
else if guesture.state == .ended{
handleGuestureEnd(guesture: guesture)
}
}
Let say the if menueIsShown is commented out
Assume slide from left to right, one slide 100px horizontally, so x = 100, menueWidth = 300
So x = min (300, 100) -> x = 100
x = max (0, ,100) --> x = 100
So the slide from left to right is smooth in .changed state
Let’s consider from right to left
Assume one slide 100px horizontally from right to left, so x = -100, menueWidth = 300
So x = min(300, -100) —> x = -100
x = max(0, -100) --> x = 0
Here problem comes.
In .changed state,
let transform = CGAffineTransform (translationX: 0, y: 0)
All the sudden the transform of both menueViewController and self.view are translate from current position to 0, that’s why the animation is so abrupt
With the menueIsShown is active, see what’s gonna happen
Assume slide from left to right, one slide 100px horizontally, so x = 100, menueWidth = 300
If statement is not executed
So x = min (300, 100) -> x = 100
x = max (0, ,100) -> x == 100
So the slide from left to right is smooth in .changed state
Let’s consider from right to left
Assume one slide 100px horizontally from right to left, so x = -100, menueWidth = 300
in if statement
x += menueWdith -> -100 + 300 => 200
So x = min(300, 200) —> x = 200
x = max(0, ,200) --> x = 200
let transform = CGAffineTransform (translationX: 200, y: 0)
Both menueViewController and self.view are translate from current position to x = 200, that keeps both animation smooth and nature.
The if statement is more like to offer some offset for x. Instead of decrease from 0, it decreases from 300(menueWidth)
So when min and max function is executed, we still have a very nice (positive) x for menViewController and self.view's transform.
Hope it is clear a bit.
Spent an hour to figure out what’s wrong since I was absent minded why Brian was talking about that part.
Back to his video and spend 10 minutes to figure out why that if statement works, physically.