Use CADisplayLink to implement jelly effect animation (learned from Glow Technical Team) and cadisplaylinkglow

Source: Internet
Author: User

Use CADisplayLink to implement jelly effect animation (learned from Glow Technical Team) and cadisplaylinkglow
Use CADisplayLink for jelly effect Animation
CADisplayLink object is a timer object that allows your application to synchronize its drawing to the refresh rate of the display.
Github

1. Define a View@ InterfaceJellyView ()
@ Property (strong, nonatomic) CADisplayLink * displayLink;
@ Property (nonatomic) CGFloat from;
@ Property (nonatomic) CGFloat;
@ Property (nonatomic) BOOL animating;@ End
2. Draw View and move path-(Void) drawRect :( CGRect) rect {
CALayer * layer = self. layer. presentationLayer;

CGFloat progress = 1;
If (! Self. animating ){
SS = 1;
} Else {
Progress = 1-(layer. position. y-self. to)/(self. from-self. );
}

CGFloat height = CGRectGetHeight (rect );
CGFloat deltaHeight = height/2 * (0.5-fabs (progress-0.5 ));

CGPoint topLeft = CGPointMake (0, deltaHeight );
CGPoint topRight = CGPointMake (CGRectGetWidth (rect), deltaHeight );
CGPoint bottomLeft = CGPointMake (0, height );
CGPoint bottomRight = CGPointMake (CGRectGetWidth (rect), height );

UIBezierPath * path = [UIBezierPathbezierPath];
[[UIColorblueColor] setFill];
[Path moveToPoint: topLeft];
[Path addQuadCurveToPoint: topRightcontrolPoint: CGPointMake (CGRectGetMidX (rect), 0)];
[Path addLineToPoint: bottomRight];
[Path addQuadCurveToPoint: bottomLeftcontrolPoint: CGPointMake (CGRectGetMidX (rect), height-deltaHeight)];
[Path closePath];
[Path fill];}
3. Add CADisplayLink-(Void) startAnimationFrom :( CGFloat) from to :( CGFloat) {
Self. from = from;
Self. to =;
Self. animating = YES;
If (self. displayLink = nil ){
Self. displayLink = [CADisplayLinkdisplayLinkWithTarget: selfselector: @ selector (tick :)];
[Self. displayLinkaddToRunLoop: [nsunloopcurrentrunloop]
ForMode: NSDefaultRunLoopMode];
}
}

-(Void) completeAnimation {
Self. animating = NO;
[Self. displayLinkinvalidate];
Self. displayLink = nil;
}

-(Void) tick :( CADisplayLink *) displayLink {
[SelfsetNeedsDisplay];}
4. CallCGFloat from = CGRectGetHeight (self. view. bounds)-CGRectGetHeight (self. blockView. bounds)/2; CGFloat to = 100; self. blockView. center = CGPointMake (self. blockView. center. x, from); [self. blockViewstartAnimationFrom: fromto: to];
[UIViewanimateWithDuration: 1 delay: 0 usingSpringWithDamping: 0.85 initialSpringVelocity: 0 options: 0 animations: ^ {
Self. blockView. center = CGPointMake (self. blockView. center. x, );
} Completion: ^ (BOOL finished) {[self. blockViewcompleteAnimation];}];

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.