几个小问题,是在看《精通IOS开发》第六版第七章遇到的

7.8章节:使用自定议选取器创建一个简单游戏

1、P 159 书上写着:添加一个选取器视图,在选取器视图下方添加一个分页

妹啊,这个分页究竟是什么?开始以为是pagecontrol组件,但看完本章后才发现,这就是一个Label,为什么前后翻译 会不一样??

2、P159最后一段:需要取消选择View设置底部的User Interaction Enabled,这样用户就不能够手动更改刻度盘作弊了

我擦 ,我把View设置的User Interaction Enabled 的勾取消后,整个页面的button,label什么的全部没有事件了。

明明只是将pickerview的user interaction enabled的勾取消么,害我浪费了2个小时,我一直在编译、改代码就是想处理为什么我的button不能点击了,点击了之后为什么没反应

3、例程中:

//当然 self.images 是 NSArray
self.images = @[[UIImage imageNamed:@"seven"] , [UIImage imageNamed:@"apple"]];

但是如果纯粹翻译成swift这是错误的

最后我改成了:

self.images = ["seven","bar","crown","cherry","lemon","apple"]

然后在调用的时候再创建UIImage,比如:

func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {

var img = self.images?[row] as String ;
return UIImageView(image: UIImage(named: img));
}

这也算是曲线救国吧,等下次学的深的时候再来看一下这个问题

4、声音

原文中,#import <AudioToolbox/AudioToolbox.h>在swift中就方便了,直接 import AutioToolbox

不过读取声音资源的时候也确实不一样

//.h
@implementation xxxController{
SystemSoundID xxxSoundId
}
//.m
if (xxxSoundId == 0) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"wav"];
NSURL *soundURL = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL , &xxxSoundId );
}
//然后就可以播放了
AudioServicePlaySystemSound(xxxSoundId);

在swift中就不能这么写了。首先,没看到有(__bridge CFURLRef),经过google,我找到了这里:http://stackoverflow.com/questions/24043904/creating-and-playing-a-sound-in-swift,有很多人回答了,有人说flappyswift中有人这样用:

import SpriteKit
import AVFoundation

class GameScene: SKScene {

// Grab the path, make sure to add it to your project!
var coinSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("coin", ofType: "wav"))
var audioPlayer = AVAudioPlayer()

// Initial setup
override func didMoveToView(view: SKView) {
audioPlayer = AVAudioPlayer(contentsOfURL: coinSound, error: nil)
audioPlayer.prepareToPlay()
}

// Trigger the sound effect when the player grabs the coin
func didBeginContact(contact: SKPhysicsContact!) {
audioPlayer.play()
}

}

也有人提了个主意,但是说新版的xcode已经不能用了。最后有人贴了段代码:

import AudioToolbox

let chaChingSound: SystemSoundID = createChaChingSound()

class CashRegisterViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
AudioServicesPlaySystemSound(chaChingSound)
}
}

func createChaChingSound() -> SystemSoundID {
var soundID: SystemSoundID = 0
let soundURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), "Cha-Ching", "aiff", nil)
AudioServicesCreateSystemSoundID(soundURL, &soundID)
CFRelease(soundURL) //新版xocde已经不要这一句了,提示自动管理内存,不要你主动释放了
return soundID
}

嗯,我就是用的这个。

5、[sef performSelector]

标题的这个方法,在swift中已经没有了,那怎么延迟0.5秒呢?stackoverflow上也有答案:http://stackoverflow.com/questions/24170282/swift-performselector-withobject-afterdelay

最简单的就是:

var timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("someSelector"), userInfo: nil, repeats: false)

func someSelector() {
// Something after a delay
}

或者 :

dispatch_after(0.1, dispatch_get_main_queue(), {
// your function here
})

---至此,第7章全部看完。

整个第一章就一个Tabbar和pickerview加datepickerview,却有这么多的问题,而且还有一个就是pickerview中的数据怎么循环?暂时还没有仔细的了解 ,先把整本书读完试试