美文网首页iOS动画及开源库IOS程序员
iOS开发之 - CADisplayLink 实现酷炫动画

iOS开发之 - CADisplayLink 实现酷炫动画

作者: Q以梦为马 | 来源:发表于2017-01-04 15:39 被阅读408次

偶然发现了一个好玩的类, CADisplayLink,出于好奇所以就尝试了一下,用 CADisplayLink 做了个类似云飘的效果。由于对 CADisplayLink 的认识还比较浅,如果哪里写的不正确,还请各位大大能够指出来!看效果图先,就是有点丑,😂

ning.gif
  • 核心代码如下:

一、创建 CADisplayLink,添加事件,绑定 Runloop。

// 创建 CADisplayLink
- (CADisplayLink *)displayLink {
    if (!_displayLink) {
        _displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(makeCloud)];

        // 当把 CADisplayLink 对象添加到 Runloop 中后,selector就能被周期性调用
        [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
    return _displayLink;
}

// 周期调用的方法
- (void)makeCloud {
    self.cloudOffsetX += self.cloudSpeed;
    
    [self cloudLayerName:self.firstCloudLayer];
    [self cloudLayerName:self.secondCloudLayer];
    [self cloudLayerName:self.thirdCloudLayer];
    [self cloudLayerName:self.fourthCloudLayer];
    [self cloudLayerName:self.fifthCloudLayer];
}

二、配置参数,加载图层

  self.cloudWidth = self.frame.size.width;            // 云彩宽度
  self.cloudColor = RGBA(255, 255, 255, 0.3);         // 云彩颜色
  self.cloudSpeed = 0.05 / M_PI;                      // 云彩飘动的速度
  self.cloudPointY = 100;                             // 云彩Y坐标
  self.cloudOffsetX = 0;                              // 云彩位移X
  self.cloudAmplitude = 30;                           // 振幅大小
  self.cloudCycle =  1.03 * M_PI / self.cloudWidth;   // 周期大小

  // 添加图层
  [self.layer addSublayer:self.firstCloudLayer];
  [self.layer addSublayer:self.secondCloudLayer];
  [self.layer addSublayer:self.thirdCloudLayer];
  [self.layer addSublayer:self.fourthCloudLayer];
  [self.layer addSublayer:self.fifthCloudLayer];

三、创建 CAShapeLayer 动画

// 五个图层动画
- (void)cloudLayerName:(CAShapeLayer *)cloudLayerName {
    // 创建一个Path句柄
    CGMutablePathRef path = CGPathCreateMutable();
    CGFloat y = self.cloudPointY;
   // 初始化该path到一个初始点
    CGPathMoveToPoint(path, nil, 0, y);
    for (float x = 0.0f; x <= self.cloudWidth; x++) {
        if (cloudLayerName == self.firstCloudLayer) {
          // 云彩的 Y 值
            y = self.cloudAmplitude * sin(self.cloudCycle * x + self.cloudOffsetX - 10) + self.cloudPointY + 10;
        } else if (cloudLayerName == self.secondCloudLayer) {
            y = (self.cloudAmplitude + 15) * sin(self.cloudCycle * x + self.cloudOffsetX ) + self.cloudPointY ;
        } else if (cloudLayerName == self.thirdCloudLayer) {
            y = (self.cloudAmplitude + 30)* sin(self.cloudCycle * x + self.cloudOffsetX + 20) + self.cloudPointY + 10;
        } else if (cloudLayerName == self.fourthCloudLayer) {
            y = (self.cloudAmplitude + 20)* sin(self.cloudCycle * x + self.cloudOffsetX - 20) + self.cloudPointY - 10;
        } else if (cloudLayerName == self.fifthCloudLayer) {
            y = (self.cloudAmplitude + 10)* sin(self.cloudCycle * x + self.cloudOffsetX - 10) + self.cloudPointY + 2;
        }
        // 添加一条直线
        CGPathAddLineToPoint(path, nil, x, y);
    }

    // 添加一条直线
    CGPathAddLineToPoint(path, nil, self.cloudWidth, self.frame.size.height);
    // 添加一条直线
    CGPathAddLineToPoint(path, nil, 0, self.frame.size.height);
    // 关闭该path
    CGPathCloseSubpath(path);
    cloudLayerName.path = path;
    // 释放该path
    CGPathRelease(path);
}

相关文章

  • iOS开发之 - CADisplayLink 实现酷炫动画

    偶然发现了一个好玩的类, CADisplayLink,出于好奇所以就尝试了一下,用 CADisplayLink 做...

  • 炫酷转场动画-iOS

    炫酷转场动画-iOS 炫酷转场动画-iOS

  • iOS动画之UIView动画

    iOS酷炫的动画效果可以很好的增强用户体验。在iOS开发中实现动画的方式有多种,一般而言,简单的动画使用UIKit...

  • iOS开发简单实现炫酷动画

    目前移动端开发,现在的动画库差不多都需要手动去编写动画代码。这样的话,iOS 和 Android 开发者就需要分别...

  • iOS 常用第三方-动画

    常见动画 awesome-ios-animation - iOS Animation 主流炫酷动画框架(特效)收集...

  • iOS动画三板斧(一)--UIView动画

    前言 iOS 精致的app,离不开酷炫合宜的动画。而iOS中的动画实现也有多种不同的方式。今天就来介绍一下iOS中...

  • UIView动画简介

    iOS的动画效果一直都很棒很,给人的感觉就是很炫酷很流畅,起到增强用户体验的作用。在APP开发中实现动画效果有很多...

  • iOS动画篇:UIView动画

    iOS的动画效果一直都很棒很,给人的感觉就是很炫酷很流畅,起到增强用户体验的作用。在APP开发中实现动画效果有很多...

  • iOS 基础动画

    iOS的动画效果一直都很棒很,给人的感觉就是很炫酷很流畅,起到增强用户体验的作用。在APP开发中实现动画效果有很多...

  • 使用CADisplayLink实现UILabel动画特效

    使用CADisplayLink实现UILabel动画特效 使用CADisplayLink实现UILabel动画特效

网友评论

    本文标题:iOS开发之 - CADisplayLink 实现酷炫动画

    本文链接:https://www.haomeiwen.com/subject/rfnovttx.html