UI常用控件总结(三)
音频
//1.设置背景音乐 //初始化音乐链接 NSURL *avUrl = [[NSBundle mainBundle] URLForResource:@"背景音乐" withExtension:@"caf"]; //初始化音乐播放器类 AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:avUrl error:nil]; _player = player; //循环次数 _player.numberOfLoops = 10; //音量(0~1) _player.volume = 0.3; //播放 [_player play]; //2.设置音效 //设置系统音效ID SystemSoundID ID; //根据音效名称,获取音效所在地址 NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"胜利" ofType:@"aiff"]; //根据地址把音效文件取出 NSURL *audioUrl = [NSURL fileURLWithPath:audioPath]; //将url转换为sound ID AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(audioUrl), &ID); //播放(带振动) AudioServicesPlayAlertSound(ID); //播放(不振动) AudioServicesPlaySystemSound(ID);
动画
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //动画方法一(复杂) //开始动画 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:5]; //设置动画延迟时间 [UIView setAnimationDelay:1]; //设置动画重复次数 [UIView setAnimationRepeatCount:5]; //设置自动返回动画 [UIView setAnimationRepeatAutoreverses:YES]; //设置动画状态 [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //必须要设置代理才能调用其结束方法 [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(stop)]; //提交动画(不能缺少这一步) [UIView commitAnimations]; //动画方法二(简单) [UIView animateWithDuration:5 animations:^{ //动画代码 }]; [UIView animateWithDuration:5 animations:^{ //动画代码 } completion:^(BOOL finished) { //动画完成后执行的代码 }]; //设置动态缓冲效果 [UIView animateWithDuration:5 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:5 options:UIViewAnimationOptionLayoutSubviews animations:^{ } completion:^(BOOL finished) { }]; }
UITabBarController
//初始化各个控制器 MyFirstViewController *mFVC = [MyFirstViewController new]; MySecondViewController *mSVC = [MySecondViewController new]; MyThirdViewController *mTVC = [MyThirdViewController new]; //添加到根控制器上 self.viewControllers = @[mFVC,mSVC,mTVC]; //给每个视图控制器添加 tab 选项 tabBarItem ——系统自带的样式 UITabBarItem *firstItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:101]; UITabBarItem *secondItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:102]; UITabBarItem *thirdItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:103]; //给每个视图控制器添加 tab 选项 tabBarItem ——自定义的样式(图片加文字描述) //取消系统渲染,始终显示用户所用图片 UIImage *image = [UIImage imageNamed:@"1"]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UITabBarItem *firstItem1 = [[UITabBarItem alloc] initWithTitle:@"" image:image selectedImage:image]; UITabBarItem *secondItem2 = [[UITabBarItem alloc] initWithTitle:@"" image:[UIImage imageNamed:@""] tag:104]; //设置按钮上面的数字小气泡 firstItem.badgeValue = @"99+"; //给最下面的条形框加背景 self.tabBar.backgroundImage = [UIImage imageNamed:@""]; //设置item mFVC.tabBarItem = firstItem; mSVC.tabBarItem = secondItem; mTVC.tabBarItem = thirdItem; //利用数组设置item字体颜色 NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor purpleColor]}; [firstItem setTitleTextAttributes:dic forState:UIControlStateNormal]; //隐藏tabBar self.tabBar.hidden = YES;