欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

画水波纹。 博客分类: iOS Objective-C 

程序员文章站 2024-03-11 22:59:19
...

#import "VWWWaterView.h"

 

@interfaceVWWWaterView ()

{

    UIColor *_currentWaterColor;

    

    float _currentLinePointY;///水的top

    

    float a;///水上下抖动的幅度。。。

    float b;

    

    BOOL jia;

}

@end

 

 

@implementation VWWWaterView

 

 

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        

        [selfsetBackgroundColor:[UIColorclearColor]];

        

        a = 1.5;

        b = 0;

        jia = NO;

        

        _currentWaterColor = [UIColorblueColor];

        _currentLinePointY = 111;

        

        [NSTimerscheduledTimerWithTimeInterval:0.02target:selfselector:@selector(animateWave) userInfo:nilrepeats:YES];

        

    }

    returnself;

}

 

-(void)animateWave

{

    if (jia) {

        a += 0.01;

    }else{

        a -= 0.01;

    }

    

    

    if (a<=1) {

        jia = YES;

    }

    

    if (a>=1.5) {

        jia = NO;

    }

    

    

    b+=0.1;

    

    [selfsetNeedsDisplay];

}

 

 

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGMutablePathRef path = CGPathCreateMutable();

    

    //画水

    CGContextSetLineWidth(context, 1);

    CGContextSetFillColorWithColor(context, [_currentWaterColorCGColor]);

    

    float y=_currentLinePointY;

    CGPathMoveToPoint(path, NULL, 0, y);

    for(float x=0;x<=320;x++){

        y= a * sin( x/180*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);

    

    CGContextAddPath(context, path);

    CGContextFillPath(context);

    CGContextDrawPath(context, kCGPathStroke);

    CGPathRelease(path);

 

    

 

}

 

相关标签: Objective-C