动画绘制水波纹

简介:

动画绘制水波纹

使用drawRect:方式绘制的动画效果,右图为占用了多少CPU.

虽然画起来挺好看的,但占用的内存真心吃不消,原因其实很简单哦,drawRect:方法只调用CPU进行图形绘制,所以非常非常的消耗CPU性能,把它集成到应用程序中,我觉得是不靠谱的呢.

//
//  WaterView.h
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface WaterView : UIView

@end


//
//  WaterView.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "WaterView.h"

@interface WaterView ()

{
    UIColor *_currentWaterColor;
    float   _currentLinePointY;
    
    float   a;
    float   b;
    
    BOOL    flag;
}

@end

@implementation WaterView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setBackgroundColor:[UIColor clearColor]];
        
        a    = 1.5;
        b    = 0;
        flag = NO;
        
        _currentWaterColor = [UIColor colorWithRed:86/255.0f
                                             green:202/255.0f
                                              blue:139/255.0f
                                             alpha:1];
        
        _currentLinePointY = 250;
        
        [NSTimer scheduledTimerWithTimeInterval:0.02
                                         target:self
                                       selector:@selector(linkRun)
                                       userInfo:nil
                                        repeats:YES];
    }
    return self;
}

- (void)linkRun
{
    if (flag) {
        a += 0.01;
    }else{
        a -= 0.01;
    }
    
    if (a<=1) {
        flag = YES;
    }
    
    if (a>=1.5) {
        flag = NO;
    }
    
    b+=0.1;
    
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    // 获取一个path
    CGMutablePathRef path = CGPathCreateMutable();
    
    {
        // 移动到起始点
        CGPathMoveToPoint(path, nil, 0, 100);
        
        // 绘制水平方向上所有的点
        float y = _currentLinePointY;
        CGPathMoveToPoint(path, NULL, 0, y);
        for(float x = 0; x <= 320; x++)
        {
            y= a * sin(x/180.f * M_PI + 4*b / M_PI) * 5 + _currentLinePointY;
            CGPathAddLineToPoint(path, nil, x, y);
        }
        
        // 移动到屏幕底部
        CGPathAddLineToPoint(path, nil, 320, rect.size.height);
        CGPathAddLineToPoint(path, nil, 0, rect.size.height);
        
        // 闭合曲线
        CGPathAddLineToPoint(path, nil, 0, _currentLinePointY);
    }
    
    // 获取绘制句柄
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 设置线宽为1
    CGContextSetLineWidth(context, 1);
    
    // 设置颜色为红色
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    
    // context接受path
    CGContextAddPath(context, path);
    
    // context填充path
    CGContextFillPath(context);
    
    // 描绘path
    CGContextDrawPath(context, kCGPathStroke);
    
    // 释放path
    CGPathRelease(path);
}

@end

以下效果:

效率相差十万八千里呢,这是因为CoreAnimation使用GPU渲染,所以不仅流畅,还消耗CPU,如果配置的路径多一些,动画效果将会非常流畅的.

//
//  RootViewController.m
//  Cell
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "YXGCD.h"

@interface RootViewController ()

@property (nonatomic, strong) GCDTimer  *timer;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor  = [UIColor blackColor];
    
    // shapeLayer
    CAShapeLayer *circleLayer = [CAShapeLayer layer];
    circleLayer.frame         = (CGRect){CGPointMake(0, 0), CGSizeMake(200, 200)};
    circleLayer.position      = self.view.center;
    circleLayer.path          = [self path1].CGPath;
    circleLayer.fillColor     = [UIColor redColor].CGColor;
    circleLayer.strokeColor   = [UIColor redColor].CGColor;
    circleLayer.lineWidth     = 2.f;
    [self.view.layer addSublayer:circleLayer];
    
    // 定时器
    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{
        static int i = 0;
        if (i++ % 2 == 0)
        {            
            CABasicAnimation *circleAnim = [CABasicAnimation animationWithKeyPath:@"path"];
            circleAnim.removedOnCompletion = NO;
            circleAnim.duration  = 1;
            circleAnim.fromValue = (__bridge id)(circleLayer.path);
            circleAnim.toValue   = (__bridge id)[self path2].CGPath;
            circleLayer.path = [self path2].CGPath;
            [circleLayer addAnimation:circleAnim forKey:@"animateCirclePath"];
        }
        else
        {
            CABasicAnimation *circleAnim = [CABasicAnimation animationWithKeyPath:@"path"];
            circleAnim.removedOnCompletion = NO;
            circleAnim.duration  = 1;
            circleAnim.fromValue = (__bridge id)(circleLayer.path);
            circleAnim.toValue   = (__bridge id)[self path1].CGPath;
            circleLayer.path = [self path1].CGPath;
            [circleLayer addAnimation:circleAnim forKey:@"animateCirclePath"];
        }
    } timeInterval:NSEC_PER_SEC];
    [_timer start];
    
}

- (UIBezierPath *)path1
{
    //// Bezier Drawing
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(0.5, 38.5)];
    [bezierPath addCurveToPoint: CGPointMake(124.5, 38.5) controlPoint1: CGPointMake(0.5, 38.5) controlPoint2: CGPointMake(74.82, 114.88)];
    [bezierPath addCurveToPoint: CGPointMake(240.5, 38.5) controlPoint1: CGPointMake(174.18, -37.88) controlPoint2: CGPointMake(240.5, 38.5)];
    [bezierPath addLineToPoint: CGPointMake(240.5, 120.5)];
    [bezierPath addLineToPoint: CGPointMake(0.5, 120.5)];
    [bezierPath addLineToPoint: CGPointMake(0.5, 38.5)];
    [bezierPath closePath];

    return bezierPath;
}

- (UIBezierPath *)path2
{
    //// Bezier Drawing
    UIBezierPath* bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint: CGPointMake(0.5, 38.5)];
    [bezierPath addCurveToPoint: CGPointMake(124.5, 38.5) controlPoint1: CGPointMake(0.5, 38.5) controlPoint2: CGPointMake(64.14, -22.65)];
    [bezierPath addCurveToPoint: CGPointMake(240.5, 38.5) controlPoint1: CGPointMake(184.86, 99.65) controlPoint2: CGPointMake(240.5, 38.5)];
    [bezierPath addLineToPoint: CGPointMake(240.5, 120.5)];
    [bezierPath addLineToPoint: CGPointMake(0.5, 120.5)];
    [bezierPath addLineToPoint: CGPointMake(0.5, 38.5)];
    [bezierPath closePath];

    return bezierPath;
}

@end

不过,使用path路径动画绘制波形图需要考验你的空间感觉了.

目录
相关文章
|
4月前
水波纹按钮动画
水波纹按钮动画
25 0
水波纹按钮动画
|
8月前
|
移动开发 前端开发 JavaScript
使用Canvas绘制图形和动画
使用Canvas绘制图形和动画
130 0
An动画优化之遮罩层动画
An动画优化之遮罩层动画
175 0
An动画优化之遮罩层动画
|
前端开发
基于canvas绘制边框环绕进度条
基于canvas绘制边框环绕进度条
193 0
基于canvas绘制边框环绕进度条
|
前端开发 JavaScript 内存技术
css动画animation绘制向四周扩散的圆圈
css动画animation绘制向四周扩散的圆圈
1321 0
|
前端开发
用canvas绘制一个烟花动画
前言 在我们日常开发中贝塞尔曲线无处不在: svg 中的曲线(支持 2阶、 3阶) canvas 中绘制贝塞尔曲线 几乎所有前端2D或3D图形图表库(echarts,d3,three.js)都会使用到贝塞尔曲线 所以掌握贝塞尔曲线势在必得。这篇文章主要是实战篇,不会介绍和贝塞尔相关的知识, 如果有同学对贝塞尔曲线不是很清楚的话:可以查看我这篇文章——深入理解SVG 绘制贝塞尔曲线 第一步我们先创建ctx, 用ctx 画一个二阶贝塞尔曲线看下。二阶贝塞尔曲线有1个控制点,一个起点,一个终点。 const canvas = document.getElementById( 'canvas'
用canvas绘制一个烟花动画
SwiftUI—使用withAnimation制作缩放和渐隐动画
SwiftUI—使用withAnimation制作缩放和渐隐动画
894 0
SwiftUI—使用withAnimation制作缩放和渐隐动画
|
前端开发 算法 图形学
用canvas绘制一个曲线动画——深入理解贝塞尔曲线
用canvas绘制一个曲线动画——深入理解贝塞尔曲线
713 0
|
前端开发 数据可视化
canvas绘制折线路径动画
canvas绘制折线路径动画
canvas绘制折线路径动画
3D立方体图片切换动画
在线演示 本地下载
966 0