一,效果图。

二,工程图。

三,代码。

RootViewController.h

#import <UIKit/UIKit.h>
//加入头文件
#import <AudioToolbox/AudioToolbox.h> @interface RootViewController : UIViewController
{
NSString *soundFile;
} @end

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. //初始化界面
[self addView];
}
#pragma -mark -fucntions
-(void)addView
{
NSArray *titleArray=[[NSArray alloc]initWithObjects:@"DO",@"RE",@"MI",@"FA",@"SO",@"LA",@"SI",@"C",@"D",@"E",@"F",@"G", nil]; for (int i=0; i<12; i++) {
UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(50, 80+30*i, 70, 20)];
[button setTitle:[titleArray objectAtIndex:i] forState:UIControlStateNormal];
[button addTarget:self action:@selector(doClickAction:) forControlEvents:UIControlEventTouchUpInside];
button.tag=i;
button.backgroundColor=[UIColor redColor];
[self.view addSubview:button];
} }
-(void)playSound:(NSString*)soundKey{ NSString *path = [NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle] resourcePath],soundKey];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID); }
#pragma -makr -doClickActions
-(void)doClickAction:(UIButton *)btn
{
NSLog(@"---doClickActions--");
NSLog(@"--btn.tag---%ld",btn.tag); if (btn.tag==0) {
soundFile = [NSString stringWithFormat:@"/001.mp3"];
[self playSound: soundFile]; }else if (btn.tag==1){
soundFile = [NSString stringWithFormat:@"/002.mp3"];
[self playSound: soundFile];
}else if (btn.tag==2){
soundFile = [NSString stringWithFormat:@"/003.mp3"];
[self playSound: soundFile];
}else if (btn.tag==3){
soundFile = [NSString stringWithFormat:@"/004.mp3"];
[self playSound: soundFile];
}else if (btn.tag==4){
soundFile = [NSString stringWithFormat:@"/005.mp3"];
[self playSound: soundFile];
}else if (btn.tag==5){
soundFile = [NSString stringWithFormat:@"/006.mp3"];
[self playSound: soundFile];
}else if (btn.tag==6){
soundFile = [NSString stringWithFormat:@"/007.mp3"];
[self playSound: soundFile];
}else if (btn.tag==7){
soundFile = [NSString stringWithFormat:@"/C.mp3"];
[self playSound: soundFile];
}else if (btn.tag==8){
soundFile = [NSString stringWithFormat:@"/D.mp3"];
[self playSound: soundFile]; }else if (btn.tag==9){
soundFile = [NSString stringWithFormat:@"/E.mp3"];
[self playSound: soundFile]; }else if (btn.tag==10){
soundFile = [NSString stringWithFormat:@"/F.mp3"];
[self playSound: soundFile]; }else if (btn.tag==11){
soundFile = [NSString stringWithFormat:@"/G.mp3"];
[self playSound: soundFile]; }
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
 
 

【代码笔记】iOS-钢琴小游戏的更多相关文章

  1. 常用Java API之Ramdom--用代码模拟猜数小游戏

    常用Java API之Ramdom Ramdom类用来生成随机数字.使用起来也是三个步骤: 1.导包 import java.util.Random; 2.创建 Random r = new Rand ...

  2. Html5游戏开发-145行代码完成一个RPG小Demo

    lufy前辈写过<[代码艺术]17行代码的贪吃蛇小游戏>一文,忽悠了不少求知的兄弟进去阅读,阅读量当然是相当的大.今天我不仿也搞一个这样的教程,目地不在于忽悠人,而在于帮助他人. 先看de ...

  3. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  4. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  5. 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)

    微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  6. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  7. 小游戏——js+h5[canvas]+cs3制作【五子棋】小游戏

    五子棋小游戏学习—— 有一个问题是,棋盘线的颜色,在canvas中,明明设置了灰色,但在我的预览中还是黑色的,很重的颜色. 以下是复刻的源码: <!DOCTYPE html> <ht ...

  8. 3行代码,为QQ轻游戏加上语音互动能力

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯游戏云 发表于云+社区专栏 游戏和社交往往有着密不可分的关系,QQ轻游戏就是一款集成在手Q里面的游戏平台,直接通过手Q入口就能随开 ...

  9. iOS开发实战-基于SpriteKit的FlappyBird小游戏

    写在前面 最近一直在忙自己的维P恩的事情 公司项目也是一团乱 于是...随手找了个游戏项目改了改就上线了,就当充数了. SpriteKit简介 SpriteKit是iOS 7之后苹果推出的2D游戏框架 ...

  10. iOS 基础入门--Bull' Eye 小游戏 

      说明   Bull's Eye小游戏是http://www.raywenderlich.com/store/ios-apprentice里非常酷的入门demo 跟着该教程一步步做下来便有了 ...

随机推荐

  1. Coreseek + Sphinx + Mysql + PHP构建中文检索引擎

    首先明确几个概念 Sphinx是开源的搜索引擎,它支持英文的全文检索.所以如果单独搭建Sphinx,你就已经可以使用全文索引了.但是往往我们要求的是中文索引,怎么做呢?国人提供了一个可供企业使用的,基 ...

  2. 【转载】IE浏览器常见的9个css Bug以及解决办法

    IE浏览器常见的9个css Bug以及解决办法 我们在浏览网页的时候经常看见这样的现象:某个网页在IE6浏览器中打开很正常,但是在IE8里面打开可能完全变形了.或者也有可能出现完全相反的现象.这让We ...

  3. 如何将CKeditor编辑器的上传和thinkphp结合

    转载:THINKPHP代码中 lxepoo的文章 有时候我们会发现,我们需要对上传文件进行日志记录,这样的记录通常写在LOG文件或数据库中,那么,通过TP本身的上传类,可以实现的,但是只能做到编辑器外 ...

  4. 使用CallerMemberName简化InotifyPropertyChanged的实现

    在WPF中,当我们要使用MVVM的方式绑定一个普通对象的属性时,界面上往往需要获取到属性变更的通知,     class NotifyObject : INotifyPropertyChanged   ...

  5. Nlog 记录日志到 sqlite

    最近研究了一下Nlog这个日志框架,这里记录一下如何将日志写到sqlite中. 第一步:使用NuGet获取Nlog和Sqlite 第二步:在sqlite中创建一个database,这里我用了SQLit ...

  6. AlertDialog.Builder弹出对话框

    在Android中,弹出对话框使用AlertDialog.Builder方法. new AlertDialog.Builder(MainActivity.this).setTitle("本机 ...

  7. TINYINT,SMALLINT,MEDIUMINT,INT,INTEGER,BIGINT;text,longtext,mediumtext,ENUM,SET等字段类型区别

    http://www.path8.net/tn/archives/951 MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型.本节首先给出可用类型的一个概述, ...

  8. 传智播客JavaWeb听课总结

    一. JavaWeb基础 第一天: 1.Eclipse详解: (1).Bad versionnumber in .class file:编译器版本和运行(JRE)版本不符合.高的JRE版本兼容低版本的 ...

  9. windows下打包react-native应用程序

    P.S.0:不截图了,上传图片太麻烦,每次只能上传一张.... 先生成签名文件,如果已有签名文件略过此步: keytool -genkey -v -keystore my-release-key.ke ...

  10. 用php模拟做服务端侦听端口

    参考:http://www.cnblogs.com/thinksasa/archive/2013/02/26/2934206.html http://blog.csdn.net/alongken200 ...