美文网首页
动态波浪wave

动态波浪wave

作者: 草原烈鹰 | 来源:发表于2017-01-13 14:46 被阅读23次
多用于头部图片的下边缘的动态波浪,效果
w01.png

下面直接上代码:


#import "ViewController.h"
#import "wgjWave.h"

@interface ViewController ()

@property (nonatomic, strong) wgjWave *waveView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [self.view addSubview:self.waveView];
}

- (UIView *)waveView
{
    if (!_waveView) {
        self.waveView = [[wgjWave alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
        _waveView.backgroundColor = [UIColor orangeColor];
    
        _waveView.waveCurvature = 0.8;
        _waveView.waveSpeed = 0.6;
        _waveView.waveHeight = 6;
        _waveView.realWaveColor = [UIColor colorWithRed:0.7 green:0.9 blue:0.9 alpha:1];
        _waveView.maskWaveColor = [UIColor whiteColor];
        
        
    }
    return  _waveView;
}

@end


#import <UIKit/UIKit.h>

@interface wgjWave : UIView


@property (nonatomic, assign) CGFloat waveCurvature;//曲度
@property (nonatomic, assign) CGFloat waveSpeed; //浪速
@property (nonatomic, assign) CGFloat waveHeight; //浪高
@property (nonatomic, strong) UIColor *realWaveColor; //实浪颜色
@property (nonatomic, strong) UIColor *maskWaveColor; //遮罩浪颜色



@end


#import "wgjWave.h"

@interface wgjWave ()

@property (nonatomic, assign) CGFloat offset;
@property (nonatomic, strong) CAShapeLayer *realWaveLayer;
@property (nonatomic, strong) CAShapeLayer *maskWaveLayer;

@property (nonatomic, strong) UIImageView *iconImageView;


@end

@implementation wgjWave


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createTimeControl];
        
        [self addSubview:self.iconImageView];
        [self.layer addSublayer:self.realWaveLayer];
        [self.layer addSublayer:self.maskWaveLayer];

        
    }
    return self;
}

- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        UIImage *image = [UIImage imageNamed:@"wgjIcon"];
        self.iconImageView = [[UIImageView alloc] initWithImage:image];
        
    }
    return _iconImageView;
}

- (CAShapeLayer *)realWaveLayer
{
    if (!_realWaveLayer) {
        self.realWaveLayer = [CAShapeLayer layer];

        _realWaveLayer.fillColor = self.realWaveColor.CGColor;
        
        
    }
    return _realWaveLayer;
}

- (CAShapeLayer *)maskWaveLayer
{
    if (!_maskWaveLayer) {
        self.maskWaveLayer = [CAShapeLayer layer];

        _maskWaveLayer.fillColor = self.maskWaveColor.CGColor;
        
        
    }
    return _maskWaveLayer;
}

- (void)setWaveHeight:(CGFloat)waveHeight
{
    _waveHeight = waveHeight;
    
    self.realWaveLayer.frame = CGRectMake(0, self.bounds.size.height - _waveHeight, self.bounds.size.width, _waveHeight);

    self.maskWaveLayer.frame = CGRectMake(0, self.bounds.size.height - _waveHeight, self.bounds.size.width, _waveHeight);
    
}

- (void)createTimeControl
{
    CADisplayLink *timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(wave)];
    [timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)wave
{
    self.offset += self.waveSpeed;
    CGFloat waveWidth = CGRectGetWidth(self.frame);
    CGFloat waveHeight = CGRectGetHeight(self.realWaveLayer.frame);
    CGFloat selfHeight = CGRectGetHeight(self.frame);
    CGFloat iconHeight = CGRectGetHeight(self.iconImageView.frame);
    
    //真实浪
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, waveHeight);
    CGFloat y = 0.f;
    for (CGFloat x = 0.f; x <= waveWidth; x++) {
        y = waveHeight * sinf(0.01 * self.waveCurvature * x + self.offset * 0.045);
        CGPathAddLineToPoint(path, NULL, x, y);
    }

    CGPathAddLineToPoint(path, NULL, waveWidth, waveHeight);
    CGPathAddLineToPoint(path, NULL, 0, waveHeight);
    self.realWaveLayer.path = path;
    self.realWaveLayer.fillColor = self.realWaveColor.CGColor;
    
    //遮罩浪
    CGMutablePathRef maskPath = CGPathCreateMutable();
    CGPathMoveToPoint(maskPath, NULL, 0, waveHeight);
    CGFloat maskY = 0.f;
    for (CGFloat x = 0.f; x <= waveWidth; x++) {
        maskY = waveHeight * cosf(0.01 * self.waveCurvature * x + self.offset * 0.045);
        CGPathAddLineToPoint(maskPath, NULL, x, maskY);
    }
    
    CGPathAddLineToPoint(maskPath, NULL, waveWidth, waveHeight);
    CGPathAddLineToPoint(maskPath, NULL, 0, waveHeight);
    self.maskWaveLayer.path = maskPath;
    self.maskWaveLayer.fillColor = self.maskWaveColor.CGColor;
    
    
    CGFloat centX = self.bounds.size.width * 0.5;
    CGFloat centY = waveHeight *cosf(0.01 * self.waveCurvature *centX + self.offset * 0.045);
    CGRect iconFrame = self.iconImageView.frame;
    iconFrame.origin.y = selfHeight - iconHeight - waveHeight + centY;
    iconFrame.origin.x = centX - iconFrame.size.width * 0.5;
    self.iconImageView.frame = iconFrame;
    
}

@end

相关文章

  • 动态波浪wave

    多用于头部图片的下边缘的动态波浪,效果 下面直接上代码:

  • 2022-10-30

    艾略特波浪理论(Elliott Wave Principle)——道氏理论告诉人们何谓大海,而波浪理论指导你如何在...

  • 2022-10-30

    艾略特波浪理论(Elliott Wave Principle)——道氏理论告诉人们何谓大海,而波浪理论指导你如何在...

  • 一款可定制的波纹进度(waveProgress)动画

    LXWaveProgress A simple wave components 一个简单的波浪进度动画,高度可定制...

  • 未解之谜

    My love, 我的爱人 you’re the unsolved wave, 你是未解的波浪 I am the ...

  • a song

    It was an ordinary day 平凡的一天 I woke up on a wave 我在梦中波浪的摇...

  • 每日十个单词:day37

    1、wave n. 波, 波浪, 波动, 起伏, 高潮, 潮涌, 挥手致意, (气压)突变 ;vi. 波动, 飘动...

  • SwiftUI 动画教程之实现波浪文字效果Wave Animat

    实战需求 SwiftUI 动画教程之实现波浪文字效果Wave AnimatableModifier 本文价值与收获...

  • PR预设(模仿创意波浪扭曲过渡和RGB效果)

    Wave Transitions是一个出色的Premiere Pro项目。 这些惊人的预设模仿创意波浪扭曲和RGB...

  • 2019-06-25

    第131讲v和w(v的第一讲) wave波浪。waver动摇,摇摆。waive宣布放弃,推迟。 waiver弃权。...

网友评论

      本文标题:动态波浪wave

      本文链接:https://www.haomeiwen.com/subject/fvrsbttx.html