iOS如何为导航栏添加播放动画
本文实例为大家分享了iOS为导航栏添加播放动画的具体代码,供大家参考,具体内容如下
FLAudioVisualizerView.h
#import@interfaceFLAudioVisualizerView:UIView #pragmamark- //默认UIEdgeInsetsZero @property(nonatomic,assign)UIEdgeInsetscontentInsets; //默认为4 @property(nonatomic,assign)NSIntegerbarCount; @property(nonatomic,copy)NSArray *barHeightRateList; //默认白色 @property(nonatomic,copy)UIColor*barColor; //默认2 @property(nonatomic,assign)CGFloatcornerRadius; //默认5 @property(nonatomic,assign)CGFloatbarSpace; //NSValue包装CGPoint @property(nonatomic,strong)NSArray *aniamteOffsetList; @property(nonatomic,readonly)BOOLisAniamting; -(void)restart; -(void)start; -(void)stop; @end
FLAudioVisualizerView.m
#import"FLAudioVisualizerView.h" @interfaceFLAudioVisualizerView() @property(nonatomic,strong)NSArray*barList; @property(nonatomic,assign)BOOLisAniamting; @end @implementationFLAudioVisualizerView #pragmamark- -(id)initWithFrame:(CGRect)frame { if(self=[superinitWithFrame:frame]){ [selfsetBarCount:4]; _barSpace=5; _barColor=[UIColorwhiteColor]; self.cornerRadius=2; self.barHeightRateList=@[@(0.4),@(0.75),@(0.55),@(0.95)]; self.transform=CGAffineTransformMakeRotation(M_PI); self.aniamteOffsetList=@[[NSValuevalueWithCGPoint:CGPointMake(0.1,0.4)], [NSValuevalueWithCGPoint:CGPointMake(0.75,0.3)], [NSValuevalueWithCGPoint:CGPointMake(0.2,0.55)], [NSValuevalueWithCGPoint:CGPointMake(0.94,0.4)], ]; self.contentInsets=UIEdgeInsetsZero; } returnself; } -(void)layoutSubviews { [superlayoutSubviews]; CGRectrect=self.bounds; if(fabs(rect.size.width)<1e-3||fabs(rect.size.height)<1e-3||rect.size.width<0||rect.size.height<0){ return; } rect=CGRectWithEdgeInserts(rect,self.contentInsets); __blockCGRectbarRect=rect; barRect.size.width=(rect.size.width-(self.barCount-1)*self.barSpace)/self.barCount; NSArray *barHeightRateList=self.barHeightRateList.reverseObjectEnumerator.allObjects; [self.barListenumerateObjectsUsingBlock:^(UIView*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop){ obj.layer.anchorPoint=CGPointZero; CGFloatrate=1.0; if(idx 0){ NSArray *removeBarViewList=[self.barListsubarrayWithRange:NSMakeRange(barCount,diff)]; [removeBarViewListmakeObjectsPerformSelector:@selector(removeFromSuperview)]; self.barList=[self.barListsubarrayWithRange:NSMakeRange(0,barCount)]; }elseif(diff<0){ diff=-diff; NSMutableArray*addBarViewList=[NSMutableArrayarrayWithCapacity:diff]; for(NSIntegerindex=0;index *aniamteOffsetList=self.aniamteOffsetList.reverseObjectEnumerator.allObjects; [self.barListenumerateObjectsUsingBlock:^(UIView*_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop){ if(idx ViewController.m
#import"ViewController.h" #import"FLAudioVisualizerView.h" #definekScreen_width[UIScreenmainScreen].bounds.size.width #defineSCREEN_HEIGHT[UIScreenmainScreen].bounds.size.height @interfaceViewController() @property(nonatomic,strong)FLAudioVisualizerView*visualizerView; @end @implementationViewController -(void)viewDidLoad{ [superviewDidLoad]; self.view.backgroundColor=[UIColorwhiteColor]; self.title=@"首页"; //修改导航栏标题字体大小和颜色,背景颜色 [self.navigationController.navigationBarsetBarTintColor:[UIColorwhiteColor]]; [self.navigationController.navigationBarsetTitleTextAttributes:@{ NSFontAttributeName:[UIFontsystemFontOfSize:17],NSForegroundColorAttributeName:[UIColorblackColor] }]; UIButton*playBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; playBtn.frame=CGRectMake(10,100,100,20); [self.viewaddSubview:playBtn]; [playBtnsetTitle:@"开始播放"forState:UIControlStateNormal]; [playBtnsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal]; [playBtnaddTarget:selfaction:@selector(playClick)forControlEvents:UIControlEventTouchUpInside]; UIButton*stopPlayBtn=[UIButtonbuttonWithType:UIButtonTypeCustom]; stopPlayBtn.frame=CGRectMake(10,150,100,20); [self.viewaddSubview:stopPlayBtn]; [stopPlayBtnsetTitle:@"停止播放"forState:UIControlStateNormal]; [stopPlayBtnsetTitleColor:[UIColorblackColor]forState:UIControlStateNormal]; [stopPlayBtnaddTarget:selfaction:@selector(stopPlayBtnClick)forControlEvents:UIControlEventTouchUpInside]; [selfsetupVisualizerView]; } -(void)setupVisualizerView { CGSizesize=CGSizeMake(23,22); UIEdgeInsetsinsets=UIEdgeInsetsMake(10,0,10,20); size.width+=insets.left+insets.right; size.height+=insets.top+insets.bottom; //直接直接使用visualizerView的时候,在侧滑返回的时候,会出现很奇怪的现象,visualizerView的frame会发生变化,导致显示异常 UIView*containerView=[[UIViewalloc]initWithFrame:CGRectMake(0,0,size.width,size.height)]; FLAudioVisualizerView*visualizerView=[[FLAudioVisualizerViewalloc]initWithFrame:CGRectMake(0,0,size.width,size.height)]; self.visualizerView=visualizerView; [containerViewaddSubview:visualizerView]; visualizerView.barColor=[UIColorcolorWithRed:63/255green:63/255blue:64/255alpha:1]; visualizerView.contentInsets=insets; UITapGestureRecognizer*tap=[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(gotoCurrentPlayPage)]; [containerViewaddGestureRecognizer:tap]; UIBarButtonItem*barItem=[[UIBarButtonItemalloc]initWithCustomView:containerView]; self.navigationItem.rightBarButtonItem=barItem; } -(void)gotoCurrentPlayPage { NSLog(@"点击了播放按钮"); } -(void)playClick { NSLog(@"1"); [self.visualizerViewrestart]; } -(void)stopPlayBtnClick { NSLog(@"2"); [self.visualizerViewstop]; } -(void)didReceiveMemoryWarning{ [superdidReceiveMemoryWarning]; //Disposeofanyresourcesthatcanberecreated. } @end以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。