一,效果图。

二,工程图。

三,代码。

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. linux的计划

    我接触linux也有好几年了,至少有5年了.最近公司在搞内部培训,人人都可以报名培训别人.想到自己在linux浸淫多年,应该出来出一份力.一直以来想就linux写一个专题.今天刚好在做相关的ppt,借 ...

  2. 【Android学习笔记】XmlResourceParser解析xml文件

    最近学习Android时,需要用到解析XML文件里的数据,可以用XmlResourceParser来解析xml文件,正好将此记录下来. XmlResourceParser里常用的字段和方法 首先先给出 ...

  3. 兼容各浏览器的iframe - onlaod事件

    上次工作中,在使用 Iframe+FormSubmit进行无刷新提交时,如果后台返回的数据有延迟,或者浏览器对Iframe内容的更改过慢的话,会遇到onload响应在Iframe内容改变之前触发,这也 ...

  4. 使用Tuple来实现多个Model传送至视图

    前面Insus.NET实现过<使用ViewModel来实现多个Model传送至视图>http://www.cnblogs.com/insus/p/5594134.html 和<使用E ...

  5. 新学C++的for,switch和随机数

    通过一个小程序学习: #include<iostream> #include<cstdlib> #include<ctime> using namespace st ...

  6. java反射的基础学习代码

    java反射的学习,好多东西不太理解,主要分析了constructor,method,field,数组和调用main函数等反射的多个方面小例子. 主要的练习类 package javaAdvanced ...

  7. QTableWidget 使用及美化_QtableWidget_QtableView滚动条宽度及样式

      //创建及属性设置m_tableWidget = new QTableWidget(this);m_tableWidget->setRowCount(10);m_tableWidget-&g ...

  8. LeetCode3:Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  9. 内存只有4G的MBP要怎么破

    开发工具包括浏览器都是极占内存的,没有个8G根本不行啊. 怎一个慢字了得? 补记: 放弃谷歌浏览器是正道

  10. MYSQL 解锁与锁表

    解锁 第一种 show processlist; 找到锁进程,kill id ; 第二种 mysql>UNLOCK TABLES; 锁表 锁定数据表,避免在备份过程中,表被更新 mysql> ...