果冻效果-CADisplayLink结合绘图

效果图:

果冻效果.gif

这里自定义一个View(图中的蓝色视图)

  • .h文件:
#import <UIKit/UIKit.h>

@interface BlockView : UIView

- (void)startAnimationFrom:(CGFloat)from to:(CGFloat)to;

- (void)completeAnimation;

@end

声明了两个方法,一个用于开始动画,另外一个用于通知视图已完成动画

  • m 文件:
#import "BlockView.h"

@interface BlockView ()

@property (strong, nonatomic) CADisplayLink *displayLink;
@property (nonatomic) CGFloat from;
@property (nonatomic) CGFloat to;
@property (nonatomic) BOOL animating;
@end

@implementation BlockView

- (void)startAnimationFrom:(CGFloat)from to:(CGFloat)to {
    self.from = from;
    self.to = to;
    self.animating = YES;
    if (self.displayLink == nil) {
        self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
        
        [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSDefaultRunLoopMode];
    }
}

- (void)completeAnimation {
    self.animating = NO;
    [self.displayLink invalidate];
    self.displayLink = nil;
}

- (void)tick:(CADisplayLink *)displayLink {
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect {
    CALayer *layer = self.layer.presentationLayer;
    
    CGFloat progress = 1;
    if (!self.animating) {
        progress = 1;
    } else {
        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));
    NSLog(@"delta:%f", deltaHeight);
    
    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];
    [[UIColor blueColor] setFill];
    [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];
}

@end

  • 在动画开始的时候,初始化displayLink,指定tick方法;
  • 动画结束的时候invalidate displayLink:
  • 每一次调用tick时,我们需要根据当前的位置重绘边缘,所以只需调用setNeedsDisplay即可:
  • 在drawRect中,我们计算当前动画的progress,然后进行绘制。

需要注意的是,我们需要通过self.layer.presentationLayer来获取动画过程中的位置信息。
另外,搭建视图时,我将自定义的View和控制器的View设置成了同样的背景色,而在绘制图层时填充了蓝色,这样才能看到果冻效果,如果忽略了背景色,可能会导致复制代码后运行起来看不出果冻效果,可以通过慢速动画发现,自定义View的上边缘会有黑色区域浮动.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,638评论 25 708
  • 觉察很多的力量来自于父母,我不能表达自己因为曾经小时候的我没有被允许过,没有得到父母的支持。此刻我难受!断了这样的...
    奥黛丽赫木阅读 219评论 0 0
  • 文 | 东临瑞 图 | 网络(侵删) 时隔很久,似乎已经过了去看这部电影的最佳时机。常常看到其傲人的票房成绩,...
    且以沧海寄余生阅读 286评论 0 1
  • 小时候,外婆家门前河边有一棵桑果树,每逢夏天桑果成熟的时候,孩子们就会垂涎欲滴;但树长的挺大,下面够的着的桑果早已...
    正心007阅读 144评论 0 0