快速播放完成时更改按钮图像

Posted

技术标签:

【中文标题】快速播放完成时更改按钮图像【英文标题】:change button image when play completes swift 【发布时间】:2016-10-04 08:56:40 【问题描述】:

当我点击播放按钮时发件人按钮发生变化,但是当我点击表格视图中的另一个按钮时,按钮第一个按钮图像不会改变为播放

 @IBAction func playSound(sender: UIButton) 
if self.player != nil && self.player.playing 
            player.stop()
            sender.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)            
        
else 
sender.setImage(UIImage(named: "pause.png"), forState: UIControlState.Normal) 


【问题讨论】:

如果条件 self.btnmale.setBackgroundImage(UIImage(named: "imagename")!, forState: .Normal) else self.btnmale.setBackgroundImage(UIImage(named: "imagename1")! , forState: .Normal) 我已经这样做了,请参阅上面的代码 您是否使用 Tableview 创建播放音频文件? 是的,我有一个表格视图,然后在表格视图单元格中我有声音按钮 你给我看那个屏幕的用户界面吗? 【参考方案1】:
**Try this code ...**  

 Declare one variable for get index.

 var songnamearray : NSArray = NSArray()
 var audioPlayer = AVAudioPlayer()
 var selectedindex : Int?
 var isFristtime : Bool = false



override func viewDidLoad() 
        super.viewDidLoad()

        songnamearray = ["Saiyaan (Sanam) 320Kbps(MajMasti.in)" ,"Phone Mein Teri Photo (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Mile Ho Tum Humko (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ," Rain Mashup (Neha Kakkar) - 320 Kbps(GoldMusic.In)" ,"Car Mein Music Baja (Neha Kakkar) 320Kbps"]

    



func numberOfSectionsInTableView(tableView: UITableView) -> Int 
        return 1
    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        return songnamearray.count
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
        let cell = tableView.dequeueReusableCellWithIdentifier("Songcell", forIndexPath: indexPath)

      // info 5 play 6 name 7
        let eventname: UILabel = (cell.viewWithTag(7) as! UILabel)

        let info: UIButton = (cell.viewWithTag(5) as! UIButton)
         let play: UIButton = (cell.viewWithTag(6) as! UIButton)
        if selectedindex == indexPath.row
            if isFristtime == true
            
                play.setImage(UIImage(named: "pause-button.png"), forState: UIControlState.Normal)
            else
                play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal)
            
        else
            play.setImage(UIImage(named: "play-button.png"), forState: UIControlState.Normal)
        
        info.setImage(UIImage(named: "icon (5).png"), forState: UIControlState.Normal)
        play.addTarget(self, action: #selector(self.CloseMethod(_:event:)), forControlEvents: .TouchDown)
        info.addTarget(self, action: #selector(self.CloseMethod1(_:event:)), forControlEvents: .TouchDown)
        eventname.text = songnamearray.objectAtIndex(indexPath.row) as? String

        return cell

    



@IBAction func CloseMethod(sender: UIButton, event: AnyObject) 

        let touches = event.allTouches()!
        let touch = touches.first!
        let currentTouchPosition = touch.locationInView(self.Songlisttable)
        let indexPath = self.Songlisttable.indexPathForRowAtPoint(currentTouchPosition)!
        selectedindex = indexPath.row
        if isFristtime == true
        
            audioPlayer.pause()
            isFristtime = false
        else
            let songnamestring :  NSString  = songnamearray.objectAtIndex(indexPath.row) as! String
            do 
                if let bundle = NSBundle.mainBundle().pathForResource(songnamestring as String, ofType: "mp3") 
                    let alertSound = NSURL(fileURLWithPath: bundle)
                    try AVAudiosession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
                    try AVAudioSession.sharedInstance().setActive(true)
                    try audioPlayer = AVAudioPlayer(contentsOfURL: alertSound)
                    audioPlayer.prepareToPlay()
                    audioPlayer.play()
                
             catch 
                print(error)
            

            isFristtime = true
        
        self.Songlisttable.reloadData()
    

【讨论】:

let play: UIButton = (cell.viewWithTag(11) as!UIButton) 这一行显示编译错误 在你的 Storyboard tableviewcell 播放按钮中给出一个标签 11 这样你的错误就消失了。 不,卡在 IF 条件中 你遇到什么样的错误?应用程序运行时。 让我们continue this discussion in chat。【参考方案2】:

尝试为按钮设置背景图片

let image = UIImage(named: "play.png") as UIImage
sender.setBackgroundImage(image, forState: UIControlState.Normal)

希望这会有所帮助。

【讨论】:

【参考方案3】:

就是那个呼叫单选按钮。我从不在单元格中使用单选按钮。这对我来说很难。我是新手。如果你需要它,那就是:Radio button logic in UItableViewCells。 但我使用其他方式:didSelectRowAtIndexPath。当您单击单元格时,当前按钮显示暂停图像,其他按钮显示播放图像。你可以参考一下

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    currentIndexPath = indexPath;
    YourTableViewCell *cell = nil;

    if (!previousIndexPath) 
        previousIndexPath = currentIndexPath;
     
    if (previousIndexPath != currentIndexPath) 
        cell = (YourTableViewCell *) [_yourTableView cellForRowAtIndexPath:previousIndexPath];
        [cell.radioBtn setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        cell = (YourTableViewCell *) [_yourTableView  cellForRowAtIndexPath:currentIndexPath];
        [cell.radioBtn setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
        previousIndexPath = currentIndexPath;
    


    if (previousIndexPath == currentIndexPath) 
        cell = (YourTableViewCell *) [_yourTableView  cellForRowAtIndexPath:previousIndexPath];
        [cell.radioBtn setBackgroundImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    

【讨论】:

当你点击单元格但我没有点击单元格我点击单元格中的按钮时实现此方法 我知道,您需要阅读:在我的 ans 中的 UItableViewCells 中链接单选按钮逻辑。这是 U 的解决方案。使用委托 我实现了你的答案但没有工作,DidSelectedRow .. 与单击单元格而不是按钮有关,我只想单击按钮, 哦,不。 UItableViewCells 中的单选按钮逻辑是单击单元格中的按钮。这不是我的方式。

以上是关于快速播放完成时更改按钮图像的主要内容,如果未能解决你的问题,请参考以下文章

音频完成后更改按钮 Xcode

单击另一个按钮 swift 2.2 更改声音按钮图像

快速更改按钮图像在旧图像上附加新图像

从 Swift 中的属性检查器列表更改条形按钮图像

如何在单击按钮操作时更改图像

如何使用方法更改按钮的 on_release 功能?