绘制播放音乐时的音波图形的View

简介:

绘制播放音乐时的音波图形的View

这个效果类似于这个哦:

效果如下:

源码:

MusicView.h 与 MusicView.m

//
//  MusicView.h
//  Music
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MusicView : UIView

@property (nonatomic, assign) CGFloat  progress;      // 进程百分比,取值为[0,1]
@property (nonatomic, assign) CGFloat  timeInterval;  // 时间间隔

@end


//
//  MusicView.m
//  Music
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "MusicView.h"

@interface MusicView ()

@property (nonatomic, assign) CGRect  baseRect; // 备份原始的frame值

@end

@implementation MusicView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _baseRect = frame;
    }
    return self;
}

@synthesize progress = _progress;
- (void)setProgress:(CGFloat)progress
{
    if (progress <= 0) {
        _progress = 0;
    } else if (progress >= 1) {
        _progress = 1;
    } else {
        _progress = progress;
    }
    
    [UIView animateWithDuration:(_timeInterval > 0 ? _timeInterval : 0.99) animations:^{
        CGRect rect       = _baseRect;
        rect.size.height *=  _progress;
        self.frame        = rect;
    }];
}
- (CGFloat)progress
{
    return _progress;
}

@end

使用时的情形:
//
//  RootViewController.m
//  Music
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "MusicView.h"

@interface RootViewController ()

@property (nonatomic, strong) NSTimer    *timer;
@property (nonatomic, strong) MusicView  *musicViewLine1;
@property (nonatomic, strong) MusicView  *musicViewLine2;
@property (nonatomic, strong) MusicView  *musicViewLine3;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 线条1
    _musicViewLine1 = [[MusicView alloc] initWithFrame:CGRectMake(100, 100, 4, 20)];
    _musicViewLine1.backgroundColor = [UIColor redColor];
    _musicViewLine1.timeInterval    = 0.5f;
    [self.view addSubview:_musicViewLine1];
    
    // 线条2
    _musicViewLine2 = [[MusicView alloc] initWithFrame:CGRectMake(108, 100, 4, 20)];
    _musicViewLine2.backgroundColor = [UIColor redColor];
    _musicViewLine2.timeInterval    = 0.5f;
    [self.view addSubview:_musicViewLine2];
    
    // 线条3
    _musicViewLine3 = [[MusicView alloc] initWithFrame:CGRectMake(116, 100, 4, 20)];
    _musicViewLine3.backgroundColor = [UIColor redColor];
    _musicViewLine3.timeInterval    = 0.5f;
    [self.view addSubview:_musicViewLine3];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5f
                                              target:self
                                            selector:@selector(timerEvent)
                                            userInfo:nil
                                             repeats:YES];
}

- (void)timerEvent
{
    _musicViewLine1.progress = arc4random()%100/100.f;
    _musicViewLine2.progress = arc4random()%100/100.f;
    _musicViewLine3.progress = arc4random()%100/100.f;
}

@end

以下是核心代码:

目录
相关文章
|
iOS开发
iOS 动画绘制圆形
iOS 动画绘制圆形
78 1
|
存储 计算机视觉
vs2019+Qt 使用 Qlabel 在界面上显示图像及显示失真问题
vs2019+Qt 使用 Qlabel 在界面上显示图像及显示失真问题
|
Android开发
Android 裁剪摄像头预览窗口-SurfaceView
Android 裁剪摄像头预览窗口-SurfaceView
727 0
Android 裁剪摄像头预览窗口-SurfaceView
|
Android开发
自定义 View | 画板
自定义 View | 画板
自定义 View | 画板
|
图形学 C++ Windows
C++ | GDI+绘制界面
界面除了拖控件贴图,最根本的要学会绘制界面
335 0
|
图形学
GDI+——使用Graphics类绘制基本图形
GDI+——使用Graphics类绘制基本图形
406 0
GDI+——使用Graphics类绘制基本图形
|
前端开发 API Android开发
3.3 自定义控件基础 之 View的绘制
当测量好了一个View之后,我们就可以简单地重写onDraw()方法,并在Canvas对象上来绘制所需要的图形。首先我们来了解一下利用系统2D绘图API所必须要使用到的Canvas对象。
675 0
|
前端开发
自定义控件详解(三):Canvas效果变换
Canvas 画布 从前面我们已经知道了 Canvas 类可以绘出 各种形状。 这里学习一下Canvas 类的变换效果(平移,旋转等)   首先需要了解一下Canvas 画布, 我们用Canvas.DrawXXX()方法的时候并不是在一张画布上进行绘制。
942 0
|
Android开发
Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现
 Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现 LayerDrawable实现的结果和附录文章1,2,3中的layer-list一致。
1129 0
Qt之绘制闪烁文本
简述 根据之前的二位绘图,我们可以很轻松的进行文本的绘制,如果需要一些特效,比如:文本闪烁。我们就必须借助其它辅助类来完成。 简述 原理 实现 效果 源码 原理 主要涉及两个辅助类: QFontMetrics 用于获取文本字体的像素高度与宽度 QBasicTimer 定时器,用于更新文本绘制。 原理: 利用QBasicTimer
1550 0