How Can I Function Didtap Button Repeat One Song In Swift? - ios

I am trying to repeat a song in my app. How can I put in a loop feature for this?
#IBAction private func handlePauseTouchUpInside(_ sender: UIButton) {
if player?.timeControlStatus == .paused {
player?.play()
isPause = false
playPauseButton.setImage(UIImage(imageLiteralResourceName: "ic-pause"), for: .normal)
rotateView()
} else {
player?.pause()
isPause = true
playPauseButton.setImage(UIImage(imageLiteralResourceName: "ic-play"), for: .normal)
}
}
//here function repeat
#IBAction private func repeatButtonTapped(_ sender: UIButton) {
enter code here
}
(Xcode Version 13.4.1 (13F100))

Related

changing UIButton image after click a button

#IBAction func play(_ sender: UIButton) {
if player.isPlaying == true {
let myImage = UIImage(named:"stop")
sender.setImage(myImage, for: .normal)
if player.isPlaying == false {
(sender as AnyObject).setImage(UIImage(named: "play"), for: .normal)}
}
}
after I click the button, Its button image changes but it never goes back afterwards. how should I make it go back to the original button image?
I partially understand your question, try this
#IBAction func play(_ sender: UIButton) {
sender.setImage(UIImage(named:player.isPlaying ? "stop" : "play") , for: .normal)
}
#IBAction func play(_ sender: UIButton) {
if player.isPlaying == true {
let myImage = UIImage(named:"stop")
sender.setImage(myImage, for: .normal)
} else {
sender.setImage(UIImage(named: "play"), for: .normal)
}
}
If you don't stop the player when you press the button, isPlaying will always be true. So first if conditions works.
#IBAction func play(_ sender: UIButton) {
if player.isPlaying == true {
player.stop()
sender.setImage(UIImage(named:"stop"), for: .normal)
}
if player.isPlaying == false {
player.play()
sender.setImage(UIImage(named: "play"), for: .normal)
}
}

UIImageView image changes with different songs

I have a simple media player and I'm trying to make it change the artwork image as the songs change. With the code I have now it will display the artwork when you hit play but when I hit the next button to skip to the next item it stays the same unless you hit another button.
How can I make the UIImageView image change as the song media item changes?
import UIKit
import MediaPlayer
class ViewController: UIViewController {
#IBOutlet weak var coverImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createQueue()
}
func showArt(){
coverImageView.image =
myMediaPlayer.nowPlayingItem?.artwork!.image(at: CGSize.init(width: 500, height: 500))
coverImageView.isUserInteractionEnabled = true
}
#IBAction func playButton(_ sender: UIButton) {
togglePlay(on: sender)
showArt()
}
#IBAction func backButton(_ sender: UIButton) {
back()
}
#IBAction func nextButton(_ sender: UIButton) {
skip()
}
}
My other functions are as followed:
import MediaPlayer
let myMediaPlayer = MPMusicPlayerApplicationController.systemMusicPlayer
let playDrake = MPMediaPropertyPredicate(value: "Drake", forProperty: MPMediaItemPropertyArtist, comparisonType: MPMediaPredicateComparison.equalTo)
let myFilterSet: Set<MPMediaPropertyPredicate> = [playDrake]
func createQueue() {
let drakeQuery = MPMediaQuery(filterPredicates: myFilterSet)
myMediaPlayer.setQueue(with: drakeQuery)
}
func skip() {
myMediaPlayer.skipToNextItem()
}
func back() {
if myMediaPlayer.currentPlaybackTime > 0.05 {
myMediaPlayer.skipToPreviousItem()
} else if myMediaPlayer.currentPlaybackTime < 0.05 {
myMediaPlayer.skipToBeginning()
} else {
//do nothing
}
}
func togglePlay(on: UIButton) {
if myMediaPlayer.playbackState.rawValue == 2 || myMediaPlayer.playbackState.rawValue == 0 {
on.setTitle("Pause", for: UIControlState.normal)
myMediaPlayer.play()
} else if myMediaPlayer.playbackState.rawValue == 1{
on.setTitle("Play", for: UIControlState.normal)
myMediaPlayer.pause()
} else {
// do nothing
}
}
Try loading the image asynchronously
DispatchQueue.global(qos: .background).async {
myMediaPlayer.nowPlayingItem?.artwork!.image(at: CGSize.init(width: 500, height: 500))
}

let user press button only one time

I want to let user give +1 with one button or -1 points with another button, but they should be able to only press one of these buttons one time...
I use this code, but the user can still click on the button multiple times...
var job: Job! {
didSet {
jobLabel.text = job.text
likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
}
}
#IBAction func dislikeDidTouch(_ sender: AnyObject)
{
(dislikeDidTouch).isEnabled = false
job.dislike()
dislikeButton.setTitle("👎 \(job.numberOfDislikes)", for: [])
dislikeButton.setTitleColor(dislikeColor, for: []) }
#IBAction func likeDidTouch(_ sender: AnyObject)
{
sender.userInteractionEnabled=YES;
job.like()
likeButton.setTitle("👍 \(job.numberOfLikes)", for: [])
likeButton.setTitleColor(likeColor, for: [])
}
Since the sender is a UIButton , it's better to construct the funcs like this
#IBAction func dislikeDidTouch(_ sender: UIButton)
#IBAction func likeDidTouch(_ sender: UIButton)
and inside each one do
sender.isEnabled = false

How do I save a UIButton being selected (showing checkmark) and load it in iOS?

I am a beginner and the following is what code I have so far. When clicking the button, it'll switch between the checkmark and the empty box (that's good). However, when exiting the app, the button won't save the state of being selected (check marked).
Attached is all my code, so that you can clearly see what's going on.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var checkMarkButton: UIButton!
let save = "save"
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.bool(forKey: "save") == true {
checkMarkButton.setImage(#imageLiteral(resourceName: "greyChecked"), for: .normal)
}else{
checkMarkButton.setImage(#imageLiteral(resourceName: "greyUnchecked"), for: .normal)
}
}
#IBAction func checkMarkAction(_ sender: UIButton) {
UserDefaults.standard.set(true, forKey: "save")
}
}
video of what is happening
Give the image name directly ,I tested my answer it is working
override func viewDidLoad() {
super.viewDidLoad()
if UserDefaults.standard.bool(forKey: "checkBoxSave01") == true {
checkBoxButton1.setImage(#imageLiteral(resourceName: "id"), for: .normal)
}else{
checkBoxButton1.setImage(#imageLiteral(resourceName: "phone"), for: .normal)
}
}
var isFirstClick = true
#IBAction func checkBoxTapped(_ sender: UIButton) {
if isFirstClick{
checkBoxButton1.setImage(#imageLiteral(resourceName: "id"), for: .normal)
UserDefaults.standard.set(true, forKey: "checkBoxSave01")
ifFirstClick = false
}else{
checkBoxButton1.setImage(#imageLiteral(resourceName: "phone"), for: .normal)
UserDefaults.standard.set(false, forKey: "checkBoxSave01")
isFirstClick = true
}
}
Once you click on button checkBoxSave01key value will be true until you delete the app.

checkbox save/load NSUserdefaults

I have this app with two checkboxes that I want to save when I close and load when I open but I cannot figure it out. Please can someone help me edit the code. I know need to save in UserDefaults somehow but I don't know how to code it.
class CheckBox: UIButton {
// Images
let checkedImage = UIImage(named: "checked")! as UIImage
let uncheckedImage = UIImage(named: "unchecked")! as UIImage
// Bool property
var isChecked: Bool = false {
didSet{
if isChecked == true {
self.setImage(checkedImage, for: UIControlState.normal)
} else {
self.setImage(uncheckedImage, for: UIControlState.normal)
}
}
}
override func awakeFromNib() {
self.addTarget(self, action:#selector(buttonClicked(sender:)), for: UIControlEvents.touchUpInside)
self.isChecked = false
}
#objc func buttonClicked(sender: UIButton) {
if sender == self {
isChecked = !isChecked
}
}
}
Use following to save bool value to UserDefaults:
UserDefaults.standard.set(isChecked, forKey: "com.myCompany.checkboxState")
And load it using:
self.isChecked = UserDefaults.standard.bool(forKey: "com.myCompany.checkboxState")
Read more in documentation for UserDefaults.
Just remember that if you have several checkBoxes, you have to use unique key for each one, e.g. use UIButton's tag to differentiate between them:
class CheckBox: UIButton {
// Images
let checkedImage = UIImage(named: "checked")! as UIImage
let uncheckedImage = UIImage(named: "unchecked")! as UIImage
// Bool property
var isChecked: Bool = false {
didSet{
if isChecked == true {
self.setImage(checkedImage, for: UIControlState.normal)
} else {
self.setImage(uncheckedImage, for: UIControlState.normal)
}
// save it like this
UserDefaults.standard.set(isChecked, forKey: "com.myCompany.checkboxStateFor\(self.tag)")
}
}
override func awakeFromNib() {
self.addTarget(self, action:#selector(buttonClicked(sender:)), for: UIControlEvents.touchUpInside)
// load it like this
self.isChecked = UserDefaults.standard.bool(forKey: "com.myCompany.checkboxStateFor\(self.tag)")
}
#objc func buttonClicked(sender: UIButton) {
if sender == self {
isChecked = !isChecked
}
}
}
Then don't forget to setup a different tag for each checkBox in your app.
Probably something like this you are looking for?
extension UserDefaults{
func setChecked(value: Bool, forId id: String){
set(value, forKey: id)
}
func getChecked(forId id: String) -> Int{
return bool(forKey: id)
}
}
use:
UserDefaults.standard.setChecked(true, "box1")
UserDefaults.standard.getChecked("box1")
override func awakeFromNib() {
self.addTarget(self, action:#selector(buttonClicked(sender:)), for: UIControlEvents.touchUpInside)
isChecked = UserDefaults.standard.bool(forKey: "buttonChecked")
}
#objc func buttonClicked(sender: UIButton) {
isChecked = !isChecked
UserDefaults.standard.set(isChecked, forKey: "buttonChecked")
}

Resources