iOS如何为导航栏添加播放动画

2020-01-21 01:27:20王振洲

ViewController.m

 


#import "ViewController.h"
#import "FLAudioVisualizerView.h"

#define kScreen_width [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@property (nonatomic, strong) FLAudioVisualizerView *visualizerView;

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  
  self.title = @"首页";
  //修改导航栏标题字体大小和颜色,背景颜色
  [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
  [self.navigationController.navigationBar setTitleTextAttributes:@{
                                   NSFontAttributeName:[UIFont systemFontOfSize:17],NSForegroundColorAttributeName:[UIColor blackColor]
                                   }];
  UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  playBtn.frame = CGRectMake(10, 100, 100, 20);
  [self.view addSubview:playBtn];
  [playBtn setTitle:@"开始播放" forState:UIControlStateNormal];
  [playBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [playBtn addTarget:self action:@selector(playClick) forControlEvents:UIControlEventTouchUpInside];
  
  UIButton *stopPlayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  stopPlayBtn.frame = CGRectMake(10, 150, 100, 20);
  [self.view addSubview:stopPlayBtn];
  [stopPlayBtn setTitle:@"停止播放" forState:UIControlStateNormal];
  [stopPlayBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  [stopPlayBtn addTarget:self action:@selector(stopPlayBtnClick) forControlEvents:UIControlEventTouchUpInside];
  [self setupVisualizerView];
}

- (void)setupVisualizerView
{
  CGSize size = CGSizeMake(23, 22);
  UIEdgeInsets insets = UIEdgeInsetsMake(10, 0, 10, 20);
  size.width += insets.left + insets.right;
  size.height += insets.top + insets.bottom;
  // 直接直接使用visualizerView的时候,在侧滑返回的时候,会出现很奇怪的现象,visualizerView的frame会发生变化,导致显示异常
  UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
  FLAudioVisualizerView *visualizerView = [[FLAudioVisualizerView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
  self.visualizerView = visualizerView;
  [containerView addSubview:visualizerView];
  visualizerView.barColor = [UIColor colorWithRed:63/255 green:63/255 blue:64/255 alpha:1];
  visualizerView.contentInsets = insets;
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gotoCurrentPlayPage)];
  [containerView addGestureRecognizer:tap];
  UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:containerView];
  self.navigationItem.rightBarButtonItem = barItem;
  
  
  
  
}

- (void)gotoCurrentPlayPage
{
  NSLog(@"点击了播放按钮");
}

- (void)playClick
{
  NSLog(@"1");
  [self.visualizerView restart];
}

- (void)stopPlayBtnClick
{
  NSLog(@"2");
  [self.visualizerView stop];
}


- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}


@end