'Show / Hide Image in Swift

how to show or hide a image in Swift by taping on a Button? For example:

i have ImageA and ImageB and one Button which i want to use to move ImageA to ImageB and back to ImageA and so on..

It works perfect to move from ImageA to ImageB, but how can i move back to ImageA?

My code is:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var Bild1: UIImageView!
@IBOutlet weak var Bild2: UIImageView!

@IBAction func pressedButton(sender: AnyObject) {

    Bild1.hidden = true
    Bild2.hidden = false
}


override func viewDidLoad() {
    super.viewDidLoad()

    Bild1.hidden = false
    Bild2.hidden = true

    // 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.
}


}


Solution 1:[1]

@IBAction func pressedButton(sender: AnyObject) {

    Bild1.isHidden = !Bild1.isHidden
    Bild2.isHidden = !Bild2.isHidden
}

This will toggle the image property between show and hidden. Syntax is Swift4 compatible

Solution 2:[2]

try replacing with:

@IBAction func pressedButton(sender: AnyObject) {
    if Bild1.hidden {
        Bild1.hidden = false
        Bild2.hidden = true
    } else {
        Bild1.hidden = true
        Bild2.hidden = false
    } 
}

Solution 3:[3]

@IBAction func pressedButton(sender: AnyObject) {

    Bild1.hidden = !Bild1.hidden
    Bild2.hidden = !Bild2.hidden
}

Solution 4:[4]

At i=0 you are trying to access nums[i-1] = num[-1] which is an invalid position and hence an ArrayOutOfBound exception is thrown.
So, the modified version would be:

        for (j=0; j<k; j++)
        {
            for (int i=1;i<nums.length;i++)
            {
                temp=nums[i-1];
                nums[i-1]=nums[i];
                nums[i]=temp;
            } 
        }

But the above will rotate the array by k times towards the left not right as you are shifting the elements towards the left. So, to get the right rotation you need to shift the elements from the end of the array. Like:

        for (j=0; j<k; j++)
        {
            for (int i=nums.length-1; 0<i; i--)
            {
                // shifting towards the right
                temp=nums[i-1];
                nums[i-1]=nums[i];
                nums[i]=temp;
            } 
        }

Solution 5:[5]

A better solution would be taking a copy of the given array with the values '0' then looping through the given array to obtain a new_index.

Formula for the New_index for the Clock-wise rotating array:

for(int i=0;i<nums.length;i++){
 int new_index = (old_index+k)%(a.length)
 copy[new_index] = a[old_index]
}

Now the entire function code would be:

    
public static void rotate(int[] a, int k)
    {
     int n = a.length;
     int[] copy = new int[n];
    //  fill the values with zeroes
     for(int i=0;i<n;i++){
         copy[i]=0;
     }
    //  rotate the array
    for(int i=0;i<n;i++){
        int new_index = (i+k)%n;
        copy[new_index] = a[i];
    }
    // Now that the array is copied, copy the elements to the original array. Because they asked to rotate the given array.
    for(int i=0;i<n;i++){
         a[i]=copy[i];
     }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sabyasachi Ghosh
Solution 2 ppp
Solution 3 Shadow Of
Solution 4 GURU Shreyansh
Solution 5 Raj