我有一个按钮可以在播放和暂停之间切换以进行音频播放。当我使用标准的播放/暂停按钮(例如,initWithBarButtonSystemItem:UIBarButtonSystemItemPlay)时,当它们来回切换时,我在按钮上获得了漂亮的白色闪光动画效果。但是现在我使用的是自定义图像,我不再得到那种效果。我怎样才能找回它?
也非常感谢任何简化此操作的提示,因为它似乎很重量级。我看到了https://stackoverflow.com/a/9104587/1462372但不清楚这是否适用于这种情况(在按钮栏中)。
这是代码:
- (void) setAsPlaying:(BOOL)isPlaying
{
self.rootViewController.playing = isPlaying;
// we need to change which of play/pause buttons are showing, if the one to
// reverse current action isn't showing
if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
{
UIBarButtonItem *buttonToRemove = nil;
UIBarButtonItem *buttonToAdd = nil;
if (isPlaying)
{
buttonToRemove = self.playButton;
self.playButton = nil;
UIImage *pauseButtonImage = [UIImage imageNamed:@"icon_pause.png"];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton addTarget:self action:@selector(pauseAudio:) forControlEvents:UIControlEventTouchUpInside];
pauseButton.bounds = CGRectMake(0, 0, pauseButtonImage.size.width, pauseButtonImage.size.height);
[pauseButton setImage:pauseButtonImage forState:UIControlStateNormal];
self.pauseButton = [[UIBarButtonItem alloc] initWithCustomView:pauseButton];
buttonToAdd = self.pauseButton;
}
else
{
buttonToRemove = self.pauseButton;
self.pauseButton = nil;
// this was the way I was doing it before:
// self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(playAudio:)];
UIImage *playButtonImage = [UIImage imageNamed:@"icon_play.png"];
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
playButton.bounds = CGRectMake(0, 0, playButtonImage.size.width, playButtonImage.size.height);
[playButton setImage:playButtonImage forState:UIControlStateNormal];
self.playButton = [[UIBarButtonItem alloc] initWithCustomView:playButton];
buttonToAdd = self.playButton;
}
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];
// Remove a button from the toolbar and add the other one
if (buttonToRemove)
[toolbarButtons removeObject:buttonToRemove];
if (![toolbarButtons containsObject:buttonToAdd])
[toolbarButtons insertObject:buttonToAdd atIndex:5];
[self.toolbar setItems:toolbarButtons];
}
}