#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 = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
self.width =frame.size.width;
self.height = frame.size.height;
_pointArray = [NSMutableArray array];
self.aPath = [UIBezierPath bezierPath];
self.pathLayer = [CAShapeLayer layer];
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 {
[self drawBackground];
// 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 = [UIColor grayColor];
[color set]; //设置线条颜色
[self.aPath removeAllPoints];
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.aPath moveToPoint:CGPointMake(a*num, self.height/2)];
[self.aPath addCurveToPoint: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 = [[UIColor grayColor] CGColor];
self.pathLayer.fillColor = nil;
self.pathLayer.lineWidth = 1.0f;
self.pathLayer.lineJoin = kCALineJoinBevel;
[self.layer addSublayer:self.pathLayer];
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 2.0;
pathAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
pathAnimation.toValue = [NSNumber numberWithFloat:1.0f];
[self.pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
}