Change Button Image When Play Completes Swift

How to automatically change background image of a button after audio stops playing in swift?

you can extend AVAudioPlayerDelegate and override the function below. Then this function triggers you understand that audio finished.

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
print("Finish")
}

Change Button when Audio is finished Xcode

You need to implement AVAudioPlayerDelegate in class:

class ViewController: UIViewController, AVAudioPlayerDelegate {
...

and use this function inside class:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
button.setImage(UIImage(named:"pause.png"),for: .normal)
}

Show a second ImageView on UIButton

If you want to add 2 images, add them under your button.

In order to stretch the first image, set constraint of the width of the second image = 0 and set constraint of the space between the images = 0.

Sample Image

How to change UIButton image in Swift

From your Obc-C code I think you want to set an Image for button so try this way:

let playButton  = UIButton(type: .Custom)
if let image = UIImage(named: "play.png") {
playButton.setImage(image, forState: .Normal)
}

In Short:

playButton.setImage(UIImage(named: "play.png"), forState: UIControlState.Normal)

For Swift 3:

let playButton  = UIButton(type: .custom)
playButton.setImage(UIImage(named: "play.png"), for: .normal)

Toggle tableview cell image when AVAudioPlayer finishes playing

You can subscribe to be the delegate of the AVAudioPlayer and then check when the audio is complete. Can you play more than one file at a time or multiple? If you can play multiple you'll have to track all the playing audio and then toggle the button on the row based on when the delegate is fired and which row the player was associated with. If you can only play one then when the user starts playing the audio just save that row and when the audio ends toggle the button for that row.

https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/

private var xoAssociationKey: UInt8 = 0

extension AVAudioPlayer {
var name: String! {
get {
return objc_getAssociatedObject(self, &xoAssociationKey) as? String
}
set(newValue) {
objc_setAssociatedObject(self, &xoAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
}
}
}

It might be helpful to make an extension as above to store a name to the AVAudioPlayer instance. You can then keep track of the name of the audio that is playing and the cell's index path in a mutable dictionary. When you get the call back to the delegate function optional func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) you can look in the dictionary for the player.name which will give you back the index path to that button's cell. Get the cell from the table view and then toggle the buttons selected state.

EDIT

For a single cell playing at a time you could add a variable to you class as var playingAudioIndexPath:NSIndexPath?

When the audio starts playing set this to the cell index path for the button that was clicked.

Then on the playback finish event just grab the cell and toggle the button

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer,
successfully flag: Bool) {
guard let indexPath = self.playingAudioIndexPath,
cell = self.table.cellForRowAtIndexPath(indexPath) as? AudioCell else {
return;
}

cell.playButton.selected = false
self.playingAudioIndexPath = nil
}

DMCApps

Is there any way to make a button do something on the first press and something else on the second press in Swift

You can use state pattern here.

First create enum with all button states

enum ButtonState {
case notTapped, tappedOnce
}

Then create a property in a class that handle button tap

var buttonState: ButtonState = .notTapped

Now you can handle button tap in a way you want

@IBAction func buttonTapped(_ sender: Any) {
switch buttonState {
case .notTapped:
print("do something")
buttonState = .tappedOnce
case .tappedOnce:
print("do something again")
}
}

You can add any states you want and handle them as you want

UIButton image behavior changed in iOS 15?

Is this a change in iOS 15?

Yes and no. There is indeed a change in iOS 15, but the reason for the problem you're experiencing is a change in Xcode 13.

The change in iOS 15 is that there's a whole new way of configuring a button. This starts with giving the button one of four new iOS 15 types: Plain, Gray, Tinted, and Filled. If you set your button to have any of those types, you are opting in to the new behavior.

The problem you're seeing is because, in Xcode 13, when you make a button in the storyboard, it does give the button one of those types: Plain. So you have opted into the new dispensation without knowing it!

The solution, if you want the old behavior, is to change the Style pop-up menu (in the Attributes inspector) from Plain to Default. Now you have an old-style button and it will behave in the way you're accustomed to.

(Of course, in the long run, you're going to want to adopt the new dispensation. I'm just explaining the apparent change in behavior.)



Related Topics



Leave a reply



Submit