本文介绍了在UIBarButtonSystemItemPlay和UIBarButtonSystemItemPause之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让两个UIBarButtonItem想要将它作为一个UIBarButtonItem并在它们之间切换
Have two UIBarButtonItems want to make it as one UIBarButtonItem and toggle between them
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
target:self
action:@selector(playaudio:)];
systemItem1.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemPause
target:self
action:@selector(pause:)];
systemItem2.style = UIBarButtonItemStyleBordered;
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, modalBarButtonItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[modalBarButtonItem release];
[flexItem release];
-(void) playaudio: (id) sender {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme"
ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[audioPlayer play];
[fileURL release];
}
- (void)pause: (id)sender {
if
([audioPlayer isPlaying])
{[audioPlayer pause];}
else
{[audioPlayer play];}
}
我有什么想法可以做到这一点。
Any ideas how i can do that.
推荐答案
基本上,每次更新播放/暂停状态时,您都希望运行一种方法来更新工具栏。这样的事情应该有效。
你可以创建一个这样的方法:
Essentially, every time the play/pause status is updated, you're going to want to run a method to update the toolbar. Something like this should work.You can create a method like this:
-(void)playPause{
if(audioPlayer == nil){
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
audioPlayer.currentTime = 0;
[fileURL release];
}
UIBarButtonSystemItem buttontype = UIBarButtonSystemItemPlay;
if([audioPlayer isPlaying]){
[audioPlayer pause];
}
else {
[audioPlayer play];
buttontype = UIBarButtonSystemItemPause;
}
UIBarButtonSystemItem *item = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:buttontype
target:self
action:@selector(playPause)] autorelease];
self.toolbar.items = [NSArray arrayWithObject:item];
}
这篇关于在UIBarButtonSystemItemPlay和UIBarButtonSystemItemPause之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!