Source Code address: https://github.com/thermogl/TISpringLoadedViews,
This is complicated. I wrote a simplified version:
Https://www.dropbox.com/s/sv3yhm8dovh0adq/SpringDemo.zip
- ()simulateSpringWithDisplayLink:(CADisplayLink * (springEnabled && != CGPointMake(self.center.x -- CGPoint kx = CGPointMake(springConstant * displacement.x, springConstant * CGPoint bv = CGPointMake(dampingCoefficient * velocity.x, dampingCoefficient * CGPoint acceleration = CGPointMake((kx.x + bv.x) / mass, (kx.y + bv.y) /-= (acceleration.x *-= (acceleration.y * CGPoint newCenter =+= (velocity.x *+= (velocity.y *
This method is called by CADisplayLink. The default value is 60 times per second. To understand this code, first you need to understand the principle of the spring, such as how to calculate the Force received by the spring, and how to calculate the resistance received by the spring.
The following is the code of the gesture operation, that is, the method triggered when dragging:
- ()viewWasPanned:(UIPanGestureRecognizer *== CGPointMake(self.center.x + translation.x, self.center.y + (translation.x > && (translatedCenter.x - restCenter.x) >-= (translatedCenter.x - restCenter.x) - (translation.x < && (restCenter.x - translatedCenter.x) >+= (restCenter.x - translatedCenter.x) - (translation.y > && (translatedCenter.y - restCenter.y) >-= (translatedCenter.y - restCenter.y) - (translation.y < && (restCenter.y - translatedCenter.y) >+= (restCenter.y - translatedCenter.y) -+ translation.x, self.center.y +