I am creating an application that uses two songs: one from a local file and one from the user's iPod library. I would like to create a software mixing tool, meaning that the volume of each audio can be set independently. I using two UISlider
s for both volumes.
I would like to implement a cross-f开发者_开发百科ade type of behavior, meaning that if the component of one component is set to maximum, the component of the other audio is set to zero. How do I implement this?
You can set the slider current_value related to volume of song from library, and MAX_Slider_Value - current_Value the volume of song from local file, I think this can do the trick
You would implement this kind of behavior by using key-value-coding (KVC) and key-value-observing (KVO). If the value of the first slider is changed, change the value of the second one appropriately. But make sure you do not fire any KVO notifications, otherwise you are trapped in an infinite loop.
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([[object class] isEqual:NSClassFromString(@"UISlider")] {
if ([keyPath isEqual:@"value"]) {
if (object == volumeSlider1) {
// set value of second slider without firing KVO notifications
} else {
// set value of first slider without firing KVO notifications
}
}
}
}
- (void) viewDidLoad
{
[volumeSlider1 addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:NULL];
[volumeSlider2 addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:NULL];
}
精彩评论