【问题标题】:Getting white flash animation effect upon clicking play and pause buttons单击播放和暂停按钮时获得白色 Flash 动画效果
【发布时间】:2013-06-15 12:52:41
【问题描述】:

我有一个按钮可以在播放和暂停之间切换以进行音频播放。当我使用标准的播放/暂停按钮(例如,initWithBarButtonSystemItem:UIBarButtonSystemItemPlay)时,当它们来回切换时,我在按钮上获得了漂亮的白色 Flash 动画效果。但是现在我使用的是自定义图像,我不再得到那种效果。怎么找回来?

也非常感谢任何简化此操作的提示,因为它似乎很重。我看到了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];
    }
}

【问题讨论】:

  • 查看我的代码....

标签: ios


【解决方案1】:

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

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];

【讨论】:

  • 简单易行的方法....!!重点是您必须设置按钮的背景图像。如果你设置前景图像你看不到效果....
  • 这对我来说很好用..我认为这是对按钮产生影响的非常简单的方法..
  • 是的,我也测试过了。工作正常
  • 谢谢。包括addTarget 行,这与我的代码行数相同,但可能更简单一些。不管怎样,发光效果的关键线是[button setShowsTouchWhenHighlighted:YES];。即使添加到我的原始代码中,它也有效。
  • 另外,我认为 image.size.height-10 是特定于您的图像的 - 它正在剪切我的图像,所以我删除了 -10
【解决方案2】:

将其复制并粘贴到您的 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

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2020-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2020-06-05
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多