iOS学习笔记-017.剪刀石头布

本文详细介绍了一个简单的剪刀石头布游戏的实现过程,包括帧动画、块动画的应用,NSString到NSInteger的转换方法,以及如何导入和使用AVFoundation和AudioToolbox框架来播放背景音乐和音效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

剪刀石头布


一、帧动画

//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的使用比较简单:

  1. 初始化AVAudioPlayer对象,此时通常指定本地文件路径。
  2. 设置播放器属性,例如重复次数、音量大小等。
  3. 调用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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值