0

I have my background image set by self.view.insertSubview. I'm trying to create a UISwipeGestureRecognizer that advances through the background Image on swipe, while another image array cycles on tap. The tap image works fine but the swipe image only works on the first swipe.

Here's the gesture recognizer:

   let gestureRecognizerBackground = UISwipeGestureRecognizer(target: self, action: #selector(changeBackground))
        dragon2View.addGestureRecognizer(gestureRecognizerBackground)

and here's the changeBackground func:

func changeBackground() {

         let backgroundImageArray = [#imageLiteral(resourceName: "artic.png"),#imageLiteral(resourceName: "beach.png"),#imageLiteral(resourceName: "mountain.jpg"),#imageLiteral(resourceName: "spring.png")]
        let randomBackground = Int(arc4random_uniform(UInt32 (backgroundImageArray.count)))

        let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
        backgroundImage.image = backgroundImageArray[randomBackground]
        self.view.insertSubview(backgroundImage, at: 0)  

    }

Not sure why it's not advancing. Thanks in advance for your comments.

1 Answer 1

0

I was trying to set the background image with self.view.insertSubview because I didn't realize I could just have two views and 'send to back'. No problem with the array now.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.