0

我有一个按钮可以在播放和暂停之间切换以进行音频播放。当我使用标准的播放/暂停按钮(例如,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];
    }
}
4

2 回答 2

3

试试这个代码可能对你有帮助......

UIImage* image = [UIImage imageNamed:@"facebook.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height-10);
UIButton *rightbtnItem = [[UIButton alloc] initWithFrame:frame];

[rightbtnItem setBackgroundImage:image forState:UIControlStateNormal];

[rightbtnItem setShowsTouchWhenHighlighted:YES];
于 2013-06-15T04:52:41.550 回答
0

将其复制并粘贴到您的 viewDidload 中,然后查看触摸时的发光效果。

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"ar.png"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0, 0, 30, 30)];
button.center=self.view.center;
[button setBackgroundImage:[UIImage imageNamed:@"RS2kQ.png"] forState:UIControlStateHighlighted];

从这里获取图像ar.pngRS2kQ.png

于 2013-06-15T04:24:29.697 回答