路径动画(波浪)

- (void)viewDidLoad {
    [super viewDidLoad];

//layer属性
//@property (nonatomic, strong) CAShapeLayer *shapeLayer3;
//定时移动的偏移量
//@property (nonatomic, assign) CGFloat tempTranslationX;


    self.shapeLayer3 = [CAShapeLayer layer];
    // 填充的颜色
    self.shapeLayer3.fillColor = [[UIColor whiteColor] CGColor];
    // 路径线条的颜色
    self.shapeLayer3.lineWidth = 5;//0.1;
    // 路径线条的颜色
    self.shapeLayer3.strokeColor = [[UIColor greenColor] CGColor];

    // 要实现动画的对象  添加到layer层
    [self.aView.layer addSublayer:self.shapeLayer3];
    //初始化偏移量
    self.tempTranslationX = 0.1;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.01f target:self selector:@selector(Stack) userInfo:nil repeats:YES];
}

-(void)Stack
{
    NSLog(@"调用定时器");
    
    // 绘制的路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    CGFloat tempWaterWaveHeight = 50;
    //定时器动态偏移量
    self.tempTranslationX += 0.1;
    //起点  (1)左上角的点
    [path moveToPoint:CGPointMake(0, tempWaterWaveHeight)];
    
/*
y=Asin(ωx+φ)+b//三角函数公式
A振幅   控制最大值,最小值(控制波浪的幅度大小)
2π/ω是周期
φ表示初始x轴偏移量
b表示y轴偏移量
*/
    CGFloat y = 0.0f;
    for (float x = 0; x <= self.aView.frame.size.width; x++) {
//for内部添加波浪线上的点
        y= 10 * sin( x / 180 * M_PI - self.tempTranslationX / M_PI ) + tempWaterWaveHeight;
//添加点  (2)右上角的点
        [path addLineToPoint:CGPointMake(x, y)];
    }
    //(3)右下角的点
    [path addLineToPoint:CGPointMake(self.aView.frame.size.width,self.aView.frame.size.height)];
    //(4)左下角的点
    [path addLineToPoint:CGPointMake(0, self.aView.frame.size.height)];
//    [path addLineToPoint:CGPointMake(0, tempWaterWaveHeight)];
    
//这句话使(起点)和(终点)闭合   (4)
    [path closePath];
    
    self.shapeLayer3.path = [path CGPath];
}

屏幕快照 2019-05-30 上午9.46.00.png

你可能感兴趣的:(路径动画(波浪))