IOS animation implementation: Spring Effect

Source: Internet
Author: User

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 +

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.