我有一个带有一个按钮(用于播放/暂停)的桌面视图,其中包含两个文本字段。 点击播放单元格1时,按钮文字播放,正在播放歌曲。
但是我想在点击cell2或任何其他单元格时将按钮文本从播放更改为暂停。
按下标有“播放”的按钮后,它会变为“暂停”并播放音频,如果在其他单元格中按下另一个按钮,则前一个单元格按钮应该将其标签更改为“播放”。
这意味着我想根据我们在音乐播放器上看到的播放/暂停来更改按钮文本/图像。请帮助
我写过这个。请让我下一步做什么:
块引用
========更新的答案:
- (IBAction)playCallBack:(id)sender {
UIButton *clickedBtn = (UIButton*)sender;
NSIndexPath *indexPath =[self.tableView indexPathForCell:
(UITableViewCell *)[[sender superview] superview]];
NSLog(@"indexpath %ld",(long)indexPath.row);
if (_playBtn != nil && _playBtn != (UIButton*)sender) {
[_playBtn setTitle:@"Play" forState:UIControlStateNormal];
}
if (!player)
{
NSString *path=[[NSBundle mainBundle]
pathForResource:@"iphone_6_original" ofType:@"mp3"];
NSURL *url=[NSURL URLWithString:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setDelegate:self];
[player prepareToPlay];
}
if(_playBtn == clickedBtn)
{
if ([player isPlaying])
{
[clickedBtn setTitle:@"Play" forState:UIControlStateNormal];
[player pause];
}
else
{
[clickedBtn setTitle:@"Pause" forState:UIControlStateNormal];
[player play];
}
}
else
{
if ([player isPlaying]) {
player.currentTime = 0;
[player play];
[clickedBtn setTitle:@"Pause" forState:UIControlStateNormal];
}
else{
[player play];
[clickedBtn setTitle:@"Pause" forState:UIControlStateNormal];
}
}
_playBtn = (UIButton*)sender;
[_tableView reloadData];
}
答案 0 :(得分:1)
- (IBAction)playCallBack:(id)sender {
NSIndexPath *indexPath =[self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSLog(@"indexpath %ld",(long)indexPath.row);
if (!player)
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"iphone_6_original" ofType:@"mp3"];
NSURL *url=[NSURL URLWithString:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setDelegate:self];
[player prepareToPlay];
}
if (![player isPlaying])
{
[sender setTitle:@"Play" forState:UIControlStateNormal];
[player play];
}
else
{
[player pause];
[sender setTitle:@"Pause" forState:UIControlStateNormal];
}
[tableview reloadData];
}
如果在按下按钮时重新加载。然后获得当前索引
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableViewIdentifier=@"cell";
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewIdentifier];
if (cell==nil)
{
cell=[[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableViewIdentifier];
}
cell.musicName.text=[musicList objectAtIndex:indexPath.row];
cell.singer.text=[singerList objectAtIndex:indexPath.row];
cell.duration.text=[durationList objectAtIndex:indexPath.row];
if(currentIndex==indexpath.row)
{
//do
}
else
{
//or change anything
}
[cell.playBtn addTarget:self action:@selector(playCallBack:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
答案 1 :(得分:0)
- (IBAction)buttonClicked:(id)sender {
yourButton.backgroundColor = [UIColor blackColor];
}
答案 2 :(得分:0)
制作新的UIButton
@property (strong, nonatomic) UIButton *currentButton;
然后在行动方法
- (IBAction)playCallBack:(id)sender {
NSIndexPath *indexPath =[self.tableView indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
NSLog(@"indexpath %ld",(long)indexPath.row);
UIButton *clickedBtn = (UIButton*)sender;
if (self.currentButton != nil && self.currentButton != clickedBtn) {
[self.currentButton setTitle:@"Play" forState:UIControlStateNormal];
}
if (!player)
{
NSString *path=[[NSBundle mainBundle] pathForResource:@"iphone_6_original" ofType:@"mp3"];
NSURL *url=[NSURL URLWithString:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setDelegate:self];
[player prepareToPlay];
}
if(self.currentButton == clickedBtn){
if ([player isPlaying])
{
[sender setTitle:@"Play" forState:UIControlStateNormal];
[player pause];
}
else
{
[player play];
[sender setTitle:@"Pause" forState:UIControlStateNormal];
}
}else{
if ([player isPlaying]) {
[player pause];
}
player.currentTime = 0;
[player play];
}
self.currentButton = (UIButton*)sender;
}
像这样,您可以维护以前的点击按钮,并在点击其他按钮时根据您的要求进行更改