iOS学习笔记-017.剪刀石头布
来源:互联网 发布:永久刷钻软件 编辑:程序博客网 时间:2024/06/05 08:15
- 剪刀石头布
- 一帧动画
- 二块动画
- 三NSString 转 NSInteger
- 四导入声音框架
- AudioToolboxframework
- AVFoundationframework
- 导入框架步骤
- 五 注意 移除框架
- 六AVAudioPlayer 播放背景音乐
- 七AVAudioPlayer 播放音效
- 八代码实现
剪刀石头布
一、帧动画
//1.设置图片数组 _imageList = @[[UIImage imageNamed:@"石头.png"], [UIImage imageNamed:@"剪刀.png"], [UIImage imageNamed:@"布.png"] ]; //2.设置动画的图片数组 [_computerImageView setAnimationImages:_imageList]; [_onselfImgeView setAnimationImages:_imageList]; //3.设置动画时长 [_computerImageView setAnimationDuration:1]; [_onselfImgeView setAnimationDuration:1]; //4.开始动画 [_computerImageView startAnimating]; [_onselfImgeView startAnimating];
二、块动画
//1.隐藏出拳的view [UIView animateWithDuration:kActionViewDuration animations:^{ [_myView setCenter:CGPointMake(_myView.center.x, _myView.center.y+_myView.frame.size.height)]; }];
三、NSString 转 NSInteger
//获取分数NSInteger oneselfScore = [_onselfFs.text integerValue];oneselfScore++;//设置分数[_onselfFs setText:[NSString stringWithFormat:@"%ld", oneselfScore]];
四、导入声音框架
需要导入的框架是: AVFoundation.framework 和 AudioToolbox.framework
这个两个框架的基本介绍 blog : http://www.cnblogs.com/kenshincui/p/4186022.html
1.AudioToolbox.framework
音效
AudioToolbox.framework是一套基于C语言的框架,使用它来播放音效其本质是将短音频注册到系统声音服务(System Sound Service)。System Sound Service是一种简单、底层的声音播放服务,但是它本身也存在着一些限制:
音频播放时间不能超过30s
数据必须是PCM或者IMA4格式
音频文件必须打包成.caf、.aif、.wav中的一种(注意这是官方文档的说法,实际测试发现一些.mp3也可以播放)
使用System Sound Service 播放音效的步骤如下:
1. 调用AudioServicesCreateSystemSoundID( CFURLRef inFileURL, SystemSoundID* outSystemSoundID)函数获得系统声音ID。
2. 如果需要监听播放完成操作,则使用AudioServicesAddSystemSoundCompletion( SystemSoundID inSystemSoundID,
3. CFRunLoopRef inRunLoop, CFStringRef inRunLoopMode, AudioServicesSystemSoundCompletionProc inCompletionRoutine, void* inClientData)方法注册回调函数。
4. 调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) 或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效(后者带有震动效果)。
2. AVFoundation.framework
音乐
如果播放较大的音频或者要对音频有精确的控制则System Sound Service可能就很难满足实际需求了,通常这种情况会选择使用AVFoundation.framework中的AVAudioPlayer来实现。AVAudioPlayer可以看成一个播放器,它支持多种音频格式,而且能够进行进度、音量、播放速度等控制。
AVAudioPlayer的使用比较简单:
- 初始化AVAudioPlayer对象,此时通常指定本地文件路径。
- 设置播放器属性,例如重复次数、音量大小等。
- 调用play方法播放。
3. 导入框架步骤
第一个步骤
第二个步骤
五、 注意!!! 移除框架
移除框架的时候一定要注意: 选择“Move to Trach”
六、AVAudioPlayer 播放背景音乐
//1.初始化路径 NSString *path = [[NSBundle mainBundle]pathForResource:@"背景音乐" ofType:@"caf"]; //2 将路径字符串转换成url,从本地读取文件,需要使用fileURL NSURL *url = [NSURL fileURLWithPath:path]; //3.初始化音乐播放器 AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; //4.设置循环次数 // 设置循环播放的次数 // 循环次数=0,声音会播放一次 // 循环次数=1,声音会播放2次 // 循环次数小于0,会无限循环播放 [player setNumberOfLoops:-1]; //5.准备播放 [player prepareToPlay]; //6、设置音量 [_backMusicPlayer setVolume:0.5f]; //7、播放 [_backMusicPlayer play];
七、AVAudioPlayer 播放音效
//1.初始化路径 NSString * soundPath = [[NSBundle mainBundle]pathForResource:"胜利" ofType:"aiff"]; //2.将路径字符串转成url,从本地读取文件,需要使用 fileURL NSURL * url = [NSURL fileURLWithPath:soundPath]; //3.创建一个 SystemSoundID,用来存储声音id SystemSoundID soundId; //4.获取声音id (__bridge CFURLRef _Nonnull)(url) 可以先写个错de,如:只写一个url,让xcode 提示 AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundId); //5.播放 //调用AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID) //或者AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID) 方法播放音效(后者带有震动效果)。 AudioServicesPlaySystemSound(soundId);
八、代码实现
//// ViewController.m// 03_UIView05_石头剪刀布//// Created by 杞文明 on 15/12/23.// Copyright © 2015年 杞文明. All rights reserved.//#import "ViewController.h"#import <AVFoundation/AVFoundation.h>#import <AudioToolbox/AudioToolbox.h>// 操作面板的动画时长#define kActionViewDuration 0.5f@interface ViewController (){ NSArray * _imageList; // 背景音乐播放器 AVAudioPlayer *_backMusicPlayer; // 胜利音效 SystemSoundID _winSound; // 失败音效 SystemSoundID _faildSound; // 和局音效 SystemSoundID _drewSound; // 单击音效 SystemSoundID _clickSound;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //1.设置图片数组 _imageList = @[[UIImage imageNamed:@"石头.png"], [UIImage imageNamed:@"剪刀.png"], [UIImage imageNamed:@"布.png"] ]; //2.设置动画的图片数组 [_computerImageView setAnimationImages:_imageList]; [_onselfImgeView setAnimationImages:_imageList]; //3.设置动画时长 [_computerImageView setAnimationDuration:1]; [_onselfImgeView setAnimationDuration:1]; //4.开始动画 [_computerImageView startAnimating]; [_onselfImgeView startAnimating]; //5、初始化音乐播放器 _backMusicPlayer = [self loadMusic]; //6、设置音量 [_backMusicPlayer setVolume:0.5f]; //7、播放 [_backMusicPlayer play]; //8.初始化音效 _winSound = [self loadSound:@"点击按钮.aiff"]; _faildSound = [self loadSound:@"和局.aiff"]; _drewSound = [self loadSound:@"胜利.aiff"]; _clickSound = [self loadSound:@"失败.aiff"];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning];}/**继续*/- (IBAction)continiuNext:(id)sender { //1.出现选择出拳的view [UIView animateWithDuration:kActionViewDuration animations:^{ [_myView setCenter:CGPointMake(_myView.center.x, _myView.center.y-_myView.frame.size.height)]; }]; //2.开始动画 [_computerImageView startAnimating]; [_onselfImgeView startAnimating];}/**出拳*/- (IBAction)chuquan:(UIButton*)sender { //1.播放出拳的音效 AudioServicesPlaySystemSound(_clickSound); //2.隐藏出拳的view [UIView animateWithDuration:kActionViewDuration animations:^{ [_myView setCenter:CGPointMake(_myView.center.x, _myView.center.y+_myView.frame.size.height)]; }]; //3.停止动画 [_computerImageView stopAnimating]; [_onselfImgeView stopAnimating]; //3.显示出拳的图片 //用户的选项 NSInteger onselfInt = sender.tag; //电脑的选择 NSInteger compterInt = arc4random()%3; NSInteger result = onselfInt-compterInt; NSString * resultStr = @""; if (result==0) { resultStr = @"平局"; //4.播放平局的音效 AudioServicesPlaySystemSound(_drewSound); }else if(-1==result || 2==result){ resultStr = @"哦也,你赢了"; //获取分数 NSInteger oneselfScore = [_onselfFs.text integerValue]; oneselfScore++; //设置分数 [_onselfFs setText:[NSString stringWithFormat:@"%ld", oneselfScore]]; //5.播放胜利的音效 AudioServicesPlaySystemSound(_winSound); }else{ NSInteger compterScore = [_computerFs.text integerValue]; compterScore++; //设置分数 [_computerFs setText:[NSString stringWithFormat:@"%ld", compterScore]]; resultStr = @"太可惜了,你输了!"; //6.播放失败的音效 AudioServicesPlaySystemSound(_faildSound); } //设置图片 [_onselfImgeView setImage:_imageList[onselfInt]]; [_computerImageView setImage:_imageList[compterInt]]; //设置结果 [_resultLb setText:resultStr]; NSLog(resultStr);}/**加载音乐*/-(AVAudioPlayer*)loadMusic{ //1.初始化路径 NSString *path = [[NSBundle mainBundle]pathForResource:@"背景音乐" ofType:@"caf"]; //2 将路径字符串转换成url,从本地读取文件,需要使用fileURL NSURL *url = [NSURL fileURLWithPath:path]; //3.初始化音乐播放器 AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; //4.设置循环次数 // 设置循环播放的次数 // 循环次数=0,声音会播放一次 // 循环次数=1,声音会播放2次 // 循环次数小于0,会无限循环播放 [player setNumberOfLoops:-1]; //5.准备播放 [player prepareToPlay]; return player;}/**加载音效,获取声音id*/-(SystemSoundID)loadSound:(NSString*)fileName { //1.初始化路径 NSString * soundPath = [[NSBundle mainBundle]pathForResource:fileName ofType:nil]; //2.将路径字符串转成url,从本地读取文件,需要使用 fileURL NSURL * url = [NSURL fileURLWithPath:soundPath]; //3.创建一个 SystemSoundID,用来存储声音id SystemSoundID soundId; //4.获取声音id (__bridge CFURLRef _Nonnull)(url) 可以先写个错de,如:只写一个url,让xcode 提示 AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)(url), &soundId); return soundId;}@end
- iOS学习笔记-017.剪刀石头布
- 石头-剪刀-布
- 石头剪刀布
- 剪刀石头布
- 剪刀、石头、布游戏
- 剪刀 石头 布
- 石头剪刀布
- 剪刀石头布
- 石头剪刀布
- 剪刀石头布游戏
- 剪刀石头布
- 剪刀石头布游戏
- 剪刀、石头、布
- 1019:石头剪刀布
- 石头剪刀布1
- 石头-剪刀-布
- 石头剪刀布游戏
- 1019石头、剪刀、布
- Vue.js
- 如何安装光驱
- 金格公司对致远HandWrite产品的测试地址
- Spring + Springmvc + Mybatis 基本框架搭建集成教程
- PHP中curl的使用
- iOS学习笔记-017.剪刀石头布
- Maven中Snapshot版本与Release版本的区别
- Dubbo之XML找不到dubbo.xsd报错
- R语言学习笔记(一)
- matplotlib-在图表中显示中文
- JS获取各种宽高大小及屏幕分辨率等
- [版本控制之道] Git 常用的命令总结(欢迎收藏备用)
- Java安装环境 配置详解 (windows 10)
- 网线/水晶头 制作 说明图