波形图

来源:互联网 发布:宁波邦元单片机 编辑:程序博客网 时间:2024/05/01 08:00

#import "Wave.h"


static BOOL layer =YES;


@interface Wave()


@property UIBezierPath* aPath;

@property CAShapeLayer *pathLayer;




@end


static NSInteger numhorizontal;


static NSUInteger numVertical;


@implementation Wave

-(instancetype)initWithFrame:(CGRect)frame{

    

    

    if (self = [superinitWithFrame:frame]) {

        self.backgroundColor = [UIColorwhiteColor];

        self.width =frame.size.width;

        self.height = frame.size.height;

        _pointArray = [NSMutableArrayarray];

        self.aPath = [UIBezierPathbezierPath];

        self.pathLayer = [CAShapeLayerlayer];

        

        

        numhorizontal =8;

        numVertical = 6;

    }

    

    return self;

}

/**

 *  画背景

 */

-(void)drawBackground{

    float  horizontalSpacing = self.height/numhorizontal;

    float  verticalSpacing = self.width/numVertical;


    

    CGContextRef context =UIGraphicsGetCurrentContext();

    

    

    for (int a=0; a<=numhorizontal; a++) {

        if (a==numhorizontal/2) {

            CGContextBeginPath(context);

            CGContextMoveToPoint(context, 0., a*horizontalSpacing);

            CGContextAddLineToPoint(context, self.width, a*horizontalSpacing);

            //    CGContextSetRGBStrokeColor(currentContext,

            //                                   192/255.0, 192/255.0,192/255.0, 1.0);

            [[UIColor redColor] set];

            CGContextStrokePath(context);

        }else{

            CGContextBeginPath(context);

            CGContextMoveToPoint(context, 0., a*horizontalSpacing);

            CGContextAddLineToPoint(context, self.width, a*horizontalSpacing);

            //    CGContextSetRGBStrokeColor(currentContext,

            //                                   192/255.0, 192/255.0,192/255.0, 1.0);

            [[UIColor grayColor] set];

            CGContextStrokePath(context);

            

        }

        

        

        

    }

    for (int a=0; a<=numVertical; a++) {

        

        CGContextBeginPath(context);

        CGContextMoveToPoint(context, a*verticalSpacing,0);

        CGContextAddLineToPoint(context,a*verticalSpacing,self.height);

        //    CGContextSetRGBStrokeColor(currentContext,

        //                                   192/255.0, 192/255.0,192/255.0, 1.0);

        [[UIColor grayColor] set];

        CGContextStrokePath(context);

    }

    


    

}


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

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    

    [selfdrawBackground];

//        UIColor *color = [UIColor redColor];

//    [color set];  //设置线条颜色

    

//    UIBezierPath* aPath = [UIBezierPath bezierPath];

//    aPath.lineWidth = 5.0;

//    

//    aPath.lineCapStyle = kCGLineCapRound;  //线条拐角

//    aPath.lineJoinStyle = kCGLineCapRound;  //终点处理

//    

//    // Set the starting point of the shape.

//    [aPath moveToPoint:CGPointMake(100.0, 0.0)];

//    

//    // Draw the lines

//    [aPath addLineToPoint:CGPointMake(200.0, 40.0)];

//    [aPath addLineToPoint:CGPointMake(160, 140)];

//    [aPath addLineToPoint:CGPointMake(40.0, 140)];

//    [aPath addLineToPoint:CGPointMake(0.0, 40.0)];

//    [aPath closePath]; //第五条线通过调用closePath方法得到的

//    

//    [aPath stroke]; //Draws line 根据坐标点连线

    

    UIColor *color = [UIColorgrayColor];

    [color set];  //设置线条颜色

    



    [self.aPathremoveAllPoints];


    self.aPath.lineWidth =1;

    self.aPath.lineCapStyle = kCGLineCapRound//线条拐角

    self.aPath.lineJoinStyle = kCGLineCapRound//终点处理

    float num = self.width/10;

    int ponit = num/3;

    float cont = 0.0;

    

    for (int a=0; a<=10; a++) {

        

      cont  = (a+1)*num;

        

        int ard = arc4random()%50+200;

        

        int are = arc4random()%50+200;

        

        [self.aPathmoveToPoint:CGPointMake(a*num,self.height/2)];

        

        [self.aPathaddCurveToPoint:CGPointMake(cont,self.height/2)controlPoint1:CGPointMake(cont-2*ponit,self.height/2-ard)controlPoint2:CGPointMake(cont-ponit,self.height/2+are)];

        

//        [aPath stroke];

        

        

    }

    


    self.pathLayer.frame =self.bounds;

    self.pathLayer.path =self.aPath.CGPath;

  

    self.pathLayer.strokeColor = [[UIColorgrayColor] CGColor];



    

    self.pathLayer.fillColor =nil;

    self.pathLayer.lineWidth =1.0f;

    self.pathLayer.lineJoin =kCALineJoinBevel;

    

    [self.layeraddSublayer:self.pathLayer];

    

    CABasicAnimation *pathAnimation = [CABasicAnimationanimationWithKeyPath:@"strokeEnd"];

    pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];

    pathAnimation.duration = 2.0;

    pathAnimation.fromValue = [NSNumbernumberWithFloat:0.0f];

    pathAnimation.toValue = [NSNumbernumberWithFloat:1.0f];

    [self.pathLayeraddAnimation:pathAnimation forKey:@"strokeEnd"];


}

0 0