视图旋转时播放有节奏的声音

时间:2015-07-24 11:12:21

标签: swift rotation core-animation caanimation

我希望在视图旋转时播放有节奏的声音,具体取决于速度。这里我有UIview的旋转动画。

    var spinAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
spinAnimation.fromValue = 0
spinAnimation.toValue =  NSNumber(double: 10.0+Double(arc4random_uniform(360))*M_PI * 3)
spinAnimation.duration = 5
spinAnimation.delegate = self
spinAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

layer.addAnimation(spinAnimation, forKey: "SpinAnimation")

如何通过声音向用户反馈旋转速度?

1 个答案:

答案 0 :(得分:0)

在苹果开发者文档中,他们提供了更改音乐音量的选项,pl。请参阅以下网址

https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVAudioPlayerClassReference/index.html

您可以使用以下代码段作为参考

var spinnigSpeedMusic= AVAudioPlayer()
var spinningMusicUrl:NSURL =    NSBundle.mainBundle().URLForResource("spinningSpeed", withExtension: ".mp33")
spinnigSpeedMusic = AVAudioPlayer(contentsOfURL:spinningMusicUrl, error: nil)      

spinnigSpeedMusic.prepareToPlay()
spinnigSpeedMusic.play()
spinnigSpeedMusic.volume = 0.1 // this value can be changed from 0.0 to 1, based on the rotation speed.