• 设置两条水纹动画


    怎么样实现两条水纹动画呢?

    代码如下

    #import "LXHTwoWaterWaveView.h"
    
    @interface LXHTwoWaterWaveView ()
    {
        UIColor *_waterColor1;
        UIColor *_waterColor2;
        
        float _currentLinePointY1;
        float _currentLinePointY2;
        
        float a;
        float b;
        
        BOOL flag;
    }
    
    @end
    
    @implementation LXHTwoWaterWaveView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            
            [self setBackgroundColor:[UIColor clearColor]];
            
            a = 1.5;
            b = 0;
            flag = NO;
            
            _waterColor1 = [UIColor redColor];
            _waterColor2 = [UIColor orangeColor];
            _currentLinePointY1 = 330;
            _currentLinePointY2 = 340;
            
            [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(animateWave) userInfo:nil repeats:YES];
            
        }
        return self;
    }
    
    -(void)animateWave
    {
        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];
    }
    
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        //第一条带上面线的水纹
        CGContextRef context1 = UIGraphicsGetCurrentContext();
        CGMutablePathRef path1 = CGPathCreateMutable();
        
        //画水
        CGContextSetLineWidth(context1, 5);
        CGContextSetFillColorWithColor(context1, [_waterColor1 CGColor]);
        
        //设置上面线的颜色
        CGContextSetStrokeColorWithColor(context1, [[UIColor blueColor] CGColor]);
        
        float y1 = _currentLinePointY1;
        CGPathMoveToPoint(path1, NULL, 0, y1);
        for(float x = 0;x <= self.bounds.size.width;x++){
            y1 = a * sin( x / 180 * M_PI + 4 * b / M_PI ) * 5 + _currentLinePointY1;
            CGPathAddLineToPoint(path1, nil, x, y1);
        }
        CGContextAddPath(context1, path1);
        
        //设置画线,当然也可以不要,根据需求
        CGContextStrokePath(context1);
        
        CGPathAddLineToPoint(path1, nil, self.bounds.size.width, rect.size.height);
        CGPathAddLineToPoint(path1, nil, 0, rect.size.height);
        CGPathAddLineToPoint(path1, nil, 0, _currentLinePointY1);
        
        CGContextAddPath(context1, path1);
        CGContextFillPath(context1);
        CGPathRelease(path1);
        
        
        //第二条不带上面线的水纹
        CGContextRef context2 = UIGraphicsGetCurrentContext();
        CGMutablePathRef path2 = CGPathCreateMutable();
        
        //画水
        CGContextSetLineWidth(context1, 1);
        CGContextSetFillColorWithColor(context2, [_waterColor2 CGColor]);
        
        float y2 = _currentLinePointY2;
        CGPathMoveToPoint(path2, NULL, 0, y2);
        for(float x = 0;x <= self.bounds.size.width;x++){
            y2 = a * cos(x / 180 * M_PI + 4 * b / M_PI ) * 5 + _currentLinePointY2;
            CGPathAddLineToPoint(path2, nil, x, y2);
        }
        CGContextAddPath(context2, path2);
        CGPathAddLineToPoint(path2, nil, self.bounds.size.width, rect.size.height);
        CGPathAddLineToPoint(path2, nil, 0, rect.size.height);
        CGPathAddLineToPoint(path2, nil, 0, _currentLinePointY2);
        
        CGContextAddPath(context2, path2);
        CGContextFillPath(context2);
        CGPathRelease(path2);
       
    }
    
    
    @end

    然后在控制器中调用该View

    #import "ViewController.h"
    #import "LXHTwoWaterWaveView.h"
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        LXHTwoWaterWaveView *waterView = [[LXHTwoWaterWaveView alloc]initWithFrame:self.view.bounds];
        
        [self.view addSubview:waterView];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    服务器×××上的MSDTC不可用解决办法
    安装VS2010后,更改iis的asp.net版本
    刷新后 页面 保持滚动条位置
    Atitit.java 反编译 工具  attilax 总结
    Atitit.收银系统模块架构attilax 总结
    Atitit.论垃圾文件的识别与清理 文档类型垃圾文件 与api概要设计pa6.doc
    atitit.guice3 绑定方式打总结生成非单例对象toInstance toProvider区别 v2 pb29
    Atitit. Derby的使用总结attilax
    Atitit.attilax的 case list 项目经验 案例列表
    Atitit.收银系统pos 以及打印功能的行业标准
  • 原文地址:https://www.cnblogs.com/funny11/p/5068585.html
Copyright © 2020-2023  润新知