ios 水波纹(波浪形式)

创建水波纹波浪形式方法很多,此处采用CGMutablePathRef类画线

一、正弦函数画线

#import "ZJSinWaveView.h"

 

@interface ZJSinWaveView ()

{

    CGFloat waveA;//水纹振幅

    CGFloat waveW ;//水纹周期

    CGFloat offsetX; //位移

    CGFloat currentK; //当前波浪高度Y

    CGFloat wavesSpeed;//水纹速度

    CGFloat WavesWidth; //水纹宽度

}

@property (nonatomic,strong)CADisplayLink * wavesDisplayLink;

@property (nonatomic,strong)CAShapeLayer  * sinLayer;

@property (nonatomic,strong)UIColor       * layerColor;

@end

/*

 y =Asin(ωx+φ)+C

 A表示振幅,也就是使用这个变量来调整波浪的高度

 ω表示周期,也就是使用这个变量来调整在屏幕内显示的波浪的数量

 φ表示波浪横向的偏移,也就是使用这个变量来调整波浪的流动

 C表示波浪纵向的位置,也就是使用这个变量来调整波浪在屏幕中竖直的位置。

 */

@implementation ZJSinWaveView

- (instancetype)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.backgroundColor = [UIColor clearColor];

        

        self.layer.masksToBounds = YES;

        

        [self setUpSinWaves];

    }

    return self;

}

- (CAShapeLayer *)sinLayer{

    if(_sinLayer==nil){

        _sinLayer = [CAShapeLayer new];

    }

    return _sinLayer;

}

- (void)setUpSinWaves{

    //设置波浪的宽度

    WavesWidth = self.frame.size.width;

    //设置波浪的速度

    wavesSpeed = 0.2/M_PI;

    //设置振幅 最大峰值

    waveA = 6;

    //设置波浪纵向位置 上加下减

    currentK = self.frame.size.height-waveA*2.0;

    //设置周期 周期越长屏幕显示的波纹就越多

    waveW = 0.025;

    

    [self.sinLayer setFillColor:[[[UIColor whiteColor] colorWithAlphaComponent:0.2] CGColor]];

    [[self layer] addSublayer:self.sinLayer];

 

    //启动定时器

    self.wavesDisplayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave:)];

    [self.wavesDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];

}

-(void)getCurrentWave:(CADisplayLink *)displayLink{

    //实时的位移

    offsetX += wavesSpeed;

    [self setCurrentFirstWaveLayerPath];

}

-(void)setCurrentFirstWaveLayerPath{

    

    //创建一个路径

    CGMutablePathRef path = CGPathCreateMutable();

    

    CGFloat y = currentK;

    //将点移动到 x=0,y=currentK的位置

    CGPathMoveToPoint(path, nil, 0, y);

    

    for (NSInteger i =0.0f; i<=WavesWidth; i++) {

        //正弦函数波浪公式

        y = waveA * sin(waveW * i+ offsetX)+currentK;

        //将点连成线

        CGPathAddLineToPoint(path, nil, i, y);

    }

    CGPathAddLineToPoint(path, nil, WavesWidth, self.frame.size.height);

    CGPathAddLineToPoint(path, nil, 0, self.frame.size.height);

    

    CGPathCloseSubpath(path);

    [self.sinLayer setPath:path] ;

    

    //使用layer 而没用CurrentContext

    CGPathRelease(path);

}

 

二、通过余弦函数:

//余弦函数波浪公式

 y = waveA * cos(waveW*i + offsetX)+currentK;

其他方法步骤和同上,只是在计算Y值的时候换余弦公式即可。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值