Unable to play a sequence of images while clicking on a button - ios

I am trying to play separated images of an animated image by clicking on the next button. Image names were stored as strings in an array. When I click on next button, the image transition is happening for the first time. Next time, it is not happening. When I tried to print the value of i, to check what is happening when the next button is clicked, the value is not getting incremented. Below is my code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var image2: UIImageView!
// #IBOutlet weak var imageView1: UIImageView!
// img1.image=UIImage(named:"frame2.gif")
#IBAction func next(_ sender: Any) {
var imgarray=["frame2.gif","frame3.gif","frame4.gif","frame5.gif","frame6.gif"]
var i=0
if i<7{
image2.image=UIImage(named:imgarray[i])
i+=1
}
else if i>=7{
i=0
}
print(i)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

put var i=0 outside the button action. Otherwise when you click on button your i will always reinitialize with 0
import UIKit
class ViewController: UIViewController {
var i=0
#IBOutlet weak var image2: UIImageView!
// #IBOutlet weak var imageView1: UIImageView!
// img1.image=UIImage(named:"frame2.gif")
#IBAction func next(_ sender: Any) {
var imgarray=["frame2.gif","frame3.gif","frame4.gif","frame5.gif","frame6.gif"]
if i<7{
image2.image=UIImage(named:imgarray[i])
i+=1
}
else if i>=7{
i=0
}
print(i)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

Related

Geting a fatal "error: unexpectedly found nil while unwrapping an Optional value" when chaging value of label on button press

I keep getting nil value when I press button.
import UIKit
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var txt_vrednost: UITextField!
#IBOutlet weak var btn_dodaj: UIButton!
#IBOutlet weak var lbl_stanje: UILabel!
var novcanik = 0.0
//MARK: Acttion
#IBAction func btn_dodaj_pressed(_ sender: Any) {
let values = Double(txt_vrednost.text!)
novcanik = novcanik + values!
lbl_stanje.text=String(novcanik)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
First of all make sure all your outlets are connected with storyboard. You are using force unwrap everywhere, which is causing crash :
import UIKit
class ViewController: UIViewController {
//MARK: Properties
#IBOutlet weak var txt_vrednost: UITextField!
#IBOutlet weak var btn_dodaj: UIButton!
#IBOutlet weak var lbl_stanje: UILabel!
var novcanik = 0.0
//MARK: Action
#IBAction func btn_dodaj_pressed(_ sender: Any) {
guard let values = txt_vrednost.text as? Double else {
print("not convertible")
return
}
novcanik = novcanik + values
lbl_stanje.text = "\(novcanik)"
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}

Change the text of a label with a button in iOS

Firstly, let me say I'm new to Swift and Xcode. I have a label that displays some text and I want that text to change when a button is pressed.
Here is the code in the UIViewController:
var txt = "Hey"
#IBOutlet weak var Text: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
showText()
}
func showText(){
Text.text = txt
}
#IBAction func Button(_ sender: Any) {
txt = "Hello"
}
The buttons and labels are correctly linked to their main.storyboard counterparts. When I run this and press the button the text doesn't change.
viewDidLoad() is only called one time when the ViewController is created. showText() needs to be called after txt is set if you want to display it. One way to automate this is to add a property observer to txt to call showText() when txt is updated:
var txt = "Hey" {
didSet {
showText()
}
}
I will add that it is more flexible and perhaps elegant to have your showText() method take the actual text to show as an argument, instead of depending on some internal state (the value of the property txt: String, that isn't immediately evident at the call site) to accomplish its task:
#IBOutlet weak var textLabel: UILabel!
func showText(_ text: String){
self.textLabelabel?.text = text
// (optional chaining to avoid crash if accidentally
// called before viewDidLoad())
}
#IBAction func buttonAction(_ sender: Any) {
self.showText("Hello") // From here it is clear what text will be shown
}
change your code as per below
var txt = "Hey"
#IBOutlet weak var Text: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
showText()
}
func showText(){
Text.text = txt
}
#IBAction func Button(_ sender: UIButton) {
txt = "Hello"
showText()
}
Change Text of label with button just need to pass the text in label;
//Outlets
#IBOutlet weak var firstLabel: UILabel!
#IBOutlet weak var secondLabel: UILabel!
//Button Action
#IBAction func textChabge(_ sender: Any) {
firstLabel.text = "newText"
secondLabel.text = "newText"
}
just change your IBAction like this.
#IBAction func Button(_ sender: Any) {
txt = "Hello"
showText()
}

Swift Thread 1:EXC_BREAKPOINT error

I'm a newbie to swift and for now it's a pain in the a$$ to code.
I'm trying to make a very simple app where the user enters a number to a textfield
and then when a button is clicked it shows the number + 1 in a label.
I always get this error:
Thread 1:EXC_BREAKPOINT (code=1, subcode=0x1002c11ec)
This is my code:
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var tav: UITextField!
#IBOutlet weak var fogy: UITextField!
#IBOutlet weak var ar: UITextField!
#IBOutlet weak var eredmeny: UILabel!
#IBOutlet weak var szamolasBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func szamol(_ sender: Any) {
let number = Int(tav.text!)! + 1
eredmeny.text = String(describing: number)
}
}
I ran your code and it works fine if an integer in entered in the textfield. But if you enter any character other than a string then the app crashes because Int(tav.text!)! returns nil and adding 1 to nil. Check for nil first and then add something to it.

Counting Up and Down using Volume buttons on iOS (Swift)

Hi I creating an app for myself, where I want to count and display the # on the screen. I want to use the volume buttons to count up and down. I was able create a button on screen to count up and down but I want to use the physical buttons. I have listed what I have so far, but the volume buttons doesn't seem to work. I've replaced AVAudioSession with AVAudioPlayer hoping to control the volume with the slider. I get a error EXC_BAD_ACCESS (Code=1, Adress=0x48)
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer = AVAudioPlayer()
#IBOutlet weak var counter: UILabel!
#IBOutlet weak var tapCounterUp: UIButton!
#IBOutlet weak var tapCounterDown: UIButton!
#IBOutlet weak var tapReset: UIButton!
#IBOutlet weak var volumeController: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
//Starts the count at 0
var count : Int = 0
//Adds +1 to Count
#IBAction func buttonUp(_ sender: Any) {
count += 1
counter.text = String(count)
}
//Subtracts -1 to Count
#IBAction func buttonDown(_ sender: Any) {
count -= 1
counter.text = String(count)
}
//Resets the Count to "0"
#IBAction func buttonReset(_ sender: Any) {
count = 0
counter.text = String(count)
}
//Volume Controller
#IBAction func volumeControl(_ sender: UISlider) {
let sliderValue = lroundf(sender.value)
counter.text = "\(sliderValue)"
/// **This is were the Error occurs.**
audioPlayer.volume = volumeController.value
}
// End of function, don't delete
}

Add the value of multiple sliders in swift then average them?

Im doing this wrong I but I'm not sure how to get it I could really use some help. I have 3 sliders (I may need more later) and basically they are 1-10 in value kinda for a rating system and i want them averaged to create an average score.. so this is what I have
#IBOutlet weak var speed: UISlider!
#IBAction func speedChange(sender: AnyObject) {
let speedValue = speed.value
}
#IBOutlet weak var body: UISlider!
#IBAction func bodyChange(sender: AnyObject) {
let bodyValue = body.value
}
#IBOutlet weak var details: UISlider!
#IBAction func detailsChange(sender: AnyObject) {
let detailsValue = details.value
}
#IBOutlet weak var score: UILabel!
func final() {
var thescore = (speed.value + body.value + details.value) / 3
score.text = "\(thescore)"
}
Only problem is it doesn't work. the + errors out, so I need to fix that, but even still i think my methodology is completely off.
It worked for me. Are you sure the outlets in the storyboard are linked to your Controller ?
Also, you may want to call your function final every time a slider changed its value, like this :
#IBOutlet weak var sliderA: UISlider!
#IBAction func sliderAchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderB: UISlider!
#IBAction func sliderBchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var sliderC: UISlider!
#IBAction func sliderCchanged(sender: AnyObject) {
fonction()
}
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
fonction()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func fonction() {
var thescore = (sliderA.value + sliderB.value + sliderC.value ) / 3
label.text = "\(thescore)"
}​
And I added a call to the function in viewDidLoad method to initilaize the label's text.

Resources