标签:
具体使用的CADisplayLink和贝塞尔曲线
效果:
// // DisplayView.m // CustomAnimation // // Created by LV on 16/1/6. // Copyright © 2016年 Wieye. All rights reserved. // #import "DisplayView.h" @interface DisplayView () @property (nonatomic, strong) CADisplayLink * displayLink; @property (nonatomic, assign) CGFloat to; @property (nonatomic, assign) CGFloat from; @end @implementation DisplayView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { NSLog(@"Init"); self.backgroundColor = [UIColor clearColor]; } return self; } - (void)drawRect:(CGRect)rect { NSLog(@"DrawRect"); [[UIColor purpleColor] setFill]; CALayer *layer = self.layer.presentationLayer; CGFloat progress = 1 - (layer.position.y - self.to) / (self.from - self.to); 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 = [UIBezierPath bezierPath]; [path moveToPoint:topLeft]; [path addQuadCurveToPoint:topRight controlPoint:CGPointMake(CGRectGetMidX(rect), 0)]; [path addLineToPoint:bottomRight]; [path addQuadCurveToPoint:bottomLeft controlPoint:CGPointMake(CGRectGetMidX(rect), height - deltaHeight)]; [path closePath]; [path fill]; } #pragma mark - Public Action - (void)startAnimationFrom:(CGFloat)from to:(CGFloat)to { if (self.displayLink == nil) { NSLog(@"StartAnimation"); self.from = from; self.to = to; self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)]; [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; } [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0 options:0 animations:^{ self.center = CGPointMake(self.center.x, self.to); } completion:^(BOOL finished) { [self stopAnimation]; }]; } - (void)stopAnimation { if (self.displayLink != nil) { [self.displayLink invalidate]; self.displayLink = nil; } } #pragma mark - Private Action - (void)tick:(CADisplayLink *)displayLink { [self setNeedsDisplay]; } @end
#import <UIKit/UIKit.h> @interface DisplayView : UIView - (void)startAnimationFrom:(CGFloat)from to:(CGFloat)to; - (void)stopAnimation; @end
标签:
原文地址:http://www.cnblogs.com/R0SS/p/5106477.html