我正在尝试播放背景歌曲,它获得了成功,但我放置了一个播放按钮,我想根据某些条件更改按钮的图像,但它没有显示静音图像。我如何为按钮设置动画来执行此操作。这是我的代码
//声明的接口(interface).h文件
UIButton *volumeBtn;
Bool volu;
在 .m 文件中声明
在 View 上创建按钮
-(void)creating
{
//volumeBtn
volumeBtn = [UIButton buttonWithType: UIButtonTypeCustom];
[volumeBtn setBackgroundImage:[UIImage imageNamed"volume.png"]forState:UIControlStateNormal];
[volumeBtn addTarget:self actionselector(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)viewDidAppearBOOL)animated
{
[self performSelectorselector(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;
}
}
Best Answer-推荐答案 strong>
一切都是正确的,只需更改方法卷 BtnClick 中的两行。 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;
}
}
关于ios - 静音并播放背景歌曲但不显示静音图像?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24219095/
|