静音并播放背景歌曲但不显示静音图像?

时间:2014-06-14 10:42:36

标签: ios objective-c uibutton avaudioplayer uianimation

我正在尝试播放背景歌曲它获得了成功,但我放置了一个播放按钮,我想根据某些条件更改我的按钮的图像,但它没有显示静音图像。如何为按钮设置动画以执行此操作。这是我的代码

//声明接口.h文件

UIButton *volumeBtn;  
Bool volu;

在.m文件中声明 在视图上创建按钮

-(void)creating
 {
   //volumeBtn
    volumeBtn = [UIButton buttonWithType: UIButtonTypeCustom];
     [volumeBtn setBackgroundImage:[UIImage imageNamed:@"volume.png"]forState:UIControlStateNormal];
    [volumeBtn addTarget:self action:@selector(volumeBtnClick)forControlEvents:UIControlEventTouchUpInside];


    [volumeBtn setFrame:CGRectMake(self.view.frame.origin.x+9,self.view.frame.origin.y+15, 40, 40)];

     volumeBtn.layer.cornerRadius=20;
     volumeBtn.layer.masksToBounds=NO;
    [volumeBtn setShowsTouchWhenHighlighted:YES];
   [self.view addSubview:volumeBtn];
}

- (void)viewDidLoad{


 [self creating];

}

动画目的

-(void)anim
{
 CGPoint vlm = CGPointMake( 530.0f , volumeBtn.center.y);
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:1.0f];
            volumeBtn.center = vlm;

            [UIView commitAnimations];
}

-(void)viewDidAppear:(BOOL)animated
{
    [self performSelector:@selector(anim) withObject:self afterDelay:1];

}

当按钮点击时,歌曲将停止播放

-(void)volumeBtnClick
{
    if (volu ==YES)
    {
        volumeBtn.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"mute.png"]];

        [app.player stop];
        volu=NO;
    }
    else if(volu==NO)
    {
     volumeBtn.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"volume.png"]];
        [app.player play];
        volu=YES;
    }
}

1 个答案:

答案 0 :(得分:1)

一切正常只需更改方法volumeBtnClick中的两行。 setBackgroundImage而不是它的背景颜色

[volumeBtn setBackgroundImage:[UIImage imageNamed:@"mute.png"]forState:UIControlStateNormal]

[volumeBtn setBackgroundImage:[UIImage imageNamed:@"volume.png"]forState:UIControlStateNormal];

所以你的代码将是

-(void)volumeBtnClick
{
if (volu ==YES)
{
    [volumeBtn setBackgroundImage:[UIImage imageNamed:@"mute.png"]forState:UIControlStateNormal]

    [app.player stop];
    volu=NO;
}
else if(volu==NO)
{
 [volumeBtn setBackgroundImage:[UIImage imageNamed:@"volume.png"]forState:UIControlStateNormal];

    [app.player play];
    volu=YES;
}
}