Ray Wenderlich的swift教程04--布局,添加图片,音频

故事板上添加图片,按钮可以通过添加正常状态和高亮状态两幅图片,静态图片可以用UIImageView。再给上下边栏图片调整布局的时候,发现左右两边会有空隙:

图2中设置距离左右边框为0的下面,不要勾选“constrain to margins”,继续设置约束,空隙就会消失。

使用UIColor来修改背景的颜色,在viewDidLoad(),添加view.backgroundColor =UIColor.purpleColor(),背景改为紫色了。

view.backgroundColor =UIColor(patternImage:UIImage(named:"bg_tile.png")!),可以用名为“bg_tile.png”的图片替换背景色

scoreLabel.backgroundColor =UIColor(patternImage:UIImage(named:"field_score.png")!),得分标签替换图片背景色

timerLabel.backgroundColor =UIColor(patternImage:UIImage(named:"field_time.png")!),时间标签替换图片背景色

添加音频:

import AVFoundation,如下建立一个函数,输入为文件名和文件格式,输出是音频文件。

<span style="font-size:14px;"> func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer  {
        //1,音频文件的路径path,转为URL格式
        var path = NSBundle.mainBundle().pathForResource(file as String, ofType: type as String)
        var url = NSURL.fileURLWithPath(path!)
        
        //2,错误
        var error: NSError?
        
        //3,获得音频文件
        var audioPlayer:AVAudioPlayer?
        audioPlayer = AVAudioPlayer(contentsOfURL: url, error: &error)
        
        //4,返回
        return audioPlayer!
    }</span>
例如,var buttonBeep =AVAudioPlayer(),,buttonBeep =self.setupAudioPlayerWithFile("ButtonTap", type:"wav"),buttonBeep就得到音频文件“ButtonTap.wav”,

buttonBeep.volume =0.3, buttonBeep.play(),就会以0.3的音量(音量范围是0~1.0)播放这个音频了。

小游戏结束,截图纪念







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值