Lucy-jayne Lester Lucy-jayne Lester - 1 year ago 74
Swift Question

How do you have two random word generators on one screen

I am currently working on an app where you can swipe two halves of the phone screen separately with each half generating a random word when you swipe left.

I have created a list of words for the top half of the screen that randomly generate words. How would I apply this same method for the bottom half of the screen? I have got 2 images of the code that I currently have in view controller

import UIKit

class ViewController: UIViewController {
// put the labels and the buttons
@IBOutlet var InspirationalThought: UILabel!
@IBOutlet var Click: UIButton!

//what the label is going to show

var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle", "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion","Ancient Greek", "Philosophers", "Fairy tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air", "Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior","Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]

var whichQuotestoChoose = 0

// what the button is going to show
var ButtonText = ["Inspiration", "Tell me more", "more inspirational"]
var whichButtonTexttoChoose = 0

@IBAction func ClickButtonClicked(sender: UIButton) {

func chooseAQuote(){
whichQuotestoChoose = Int(arc4random_uniform (84))

func showTheQuote(){
InspirationalThought.text = "\(quotes[whichQuotestoChoose])"

func chooseTextonButton(){
Click.setTitle("\(ButtonText[whichButtonTexttoChoose])", forState:UIControlState.Normal)

enter image description here

enter image description here

Answer Source

This is how you can do it. If your background remains the same, with top and bottom color always same with star then get it as a image.

Add a imageView and set the image as background image

Add 2 UIView's view1 and view2 as top and bottom view. Set there background color as clearColor and tag as 1 and 2 respectively

Add swipe gesture 2 both of them with same selector.

Add UILabel to each namely view label1 and label2.

Create a string to hold the text from quotes when swipped. Based on the tag of the view, set the label text accordingly.

Below is the sample implementation:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var view1: UIView!
@IBOutlet weak var view2: UIView!
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!

var displayString: String?
var quotes = ["Geometrics", "Vectors", "Celebration", "Triangle",      "Landscapes", "Seasons", "Snow", "Rain", "Sunrays", "Stencils", "Paint", "Graphics", "Graffiti", "Sports","Fashion","Ancient Greek", "Philosophers", "Fairy tales", "Fantasy", "Clouds", "Mystery", "Time Clocks", "Canvas", "Tie-dye", "Glitter", "Dessert", "Desert", "Energy", "Astrology", "Solar Systems", "Sea", "Beach", "Sphere", "Roots", "Lights", "Darks", "Fire", "Air", "Aperture", "Long exposure", "Portraits", "World", "Travel", "Architecture", "Freedom", "Old", "New", "Urban", "Lenses", "Fisheye", "Chords", "Music notes", "Spices", "Herbs", "Natural", "Marbles", "Wood", "Trees", "Forests", "Interior","Mammals", "Reptiles", "Ocean", "Birds", "Photography", "Exposure", "Opaque", "Translucent", "Freestyle", "Spots", "Stripes", "Zig Zag", "Spiral", "Glass", "Feathers", "Calm", "Bulb", "Heat", "Cold", "Stitches", "Views", "Birds", "Sunset", "Earth"]

var whichQuotestoChoose = 0

override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.viewSwipped(_:)))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.Left

    let swipeLeft1 = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.viewSwipped(_:)))
    swipeLeft1.direction = UISwipeGestureRecognizerDirection.Left

func viewSwipped(gesture: UIGestureRecognizer) {
    if let swippedView = gesture.view {
        if swippedView.tag == 1 {
            label1.text = displayString
        } else {
            label2.text = displayString

func chooseAQuote() {
    displayString = quotes[whichQuotestoChoose]
    whichQuotestoChoose = Int(arc4random_uniform (84)) 

override func didReceiveMemoryWarning() {
    // Dispose of any resources that can be recreated.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download