mercredi 20 décembre 2017

Randomized String & UIImageView together

I'm working on an app which lets you have randomized data when you shake your device.

I have 4 arrays to hold string data and function that creates randomized number;

let characters = ["Zoolog", "Xander"]

let problems = ["Asteroid", "Dr Evil"]

let places = ["Vast Desert", "Ice Caves"]

let time = ["Wednesday 12th, 1220", "1236"]



func randomCharacter() -> String {

    let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: characters.count)

    return characters[randomNumber]

}

func randomPlaces() -> String {

    let randomNumberOne = GKRandomSource.sharedRandom().nextInt(upperBound: places.count)

    return places[randomNumberOne]
}

func randomProblems() -> String {

    let randomNumberTwo = GKRandomSource.sharedRandom().nextInt(upperBound: problems.count)

    return problems[randomNumberTwo]

}

func randomTime() -> String {

    let randomNumberThree = GKRandomSource.sharedRandom().nextInt(upperBound: time.count)

    return time[randomNumberThree]
}

On my viewController, the arrays are listed.Once you shake the device, data is randomized and users get a randomized data on their screen.

 override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {
    if(event?.subtype == UIEventSubtype.motionShake) {

        characterName.text = myStoryCharacters.randomCharacter()
        placeName.text = myStoryCharacters.randomPlaces()
        problemName.text = myStoryCharacters.randomProblems()
        timeName.text = myStoryCharacters.randomTime()

    }
} 

I also have imageView for the character picture. So once the data is randomized I would like my users to see the characters and their names as well. But at the moment, I can only randomize the imageView and characters separately not together.

I've gone through some sample codes but couldn't understand how to approach this.




Aucun commentaire:

Enregistrer un commentaire