In my app project I am displaying 107 of my images at random, I have set up a for loop that puts all of my images into an array. I then take that array and choose a random Index. That index correlates to a picture and then that picture appears on the screen when the user swipes to the left. My question is that can I make it so my code will not repeat the same index in the array until all of them (or the amount until closing the app) have been chosen at random. Here is my code
class ViewController: UIViewController {
var pictureArray: [String] = []
#IBOutlet weak var quoteImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
for i in 0...107{
pictureArray.append("quote\(i).jpg")
print(pictureArray)
}
// Do any additional setup after loading the view, typically from a nib.
let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (changeQuotes))
swipeRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeRecognizer)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var prefersStatusBarHidden: Bool{
return true
}
#objc func changeQuotes(){
let numberOfImages: UInt32 = 107
let randomIndex = Int(arc4random_uniform(UInt32(pictureArray.count)))
let imageName = "\(pictureArray[randomIndex])"
print(imageName)
quoteImage.image = UIImage(named: imageName)
}
}
Thanks for any help!
Flush your array randomly, then just get the "sorted" image one by one.
extension Sequence {
func randomSorted() -> [Element] {
var result = Array(self)
guard result.count > 1 else { return result }
for (i, k) in zip(result.indices, stride(from: result.count, to: 1, by: -1)) {
let newIndex = Int(arc4random_uniform(UInt32(k)))
if newIndex == i { continue }
result.swapAt(i, newIndex)
}
return result
}
}
You can generate unique random number by managing 2 arrays one contains images that are not yet displayed and one that are already displayed
Make following changes in your code
var pictureArray: [String] = []
var selectedPictureArray: [String] = []
static let TOTAL_IMAGES = 108
#IBOutlet weak var quoteImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.fillPictureArray()
let swipeRecognizer = UISwipeGestureRecognizer(target: self, action: #selector (changeQuotes))
swipeRecognizer.direction = UISwipeGestureRecognizerDirection.left
self.view.addGestureRecognizer(swipeRecognizer)
}
func fillPictureArray() {
self.selectedPictureArray.removeAll()
for i in 0..<ViewController.TOTAL_IMAGES {
pictureArray.append("quote\(i).jpg")
}
print(pictureArray)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var prefersStatusBarHidden: Bool{
return true
}
func changeQuotes(){
if self.pictureArray.count == 0 {
self.fillPictureArray()
}
var randomIndex = 0
while true {
randomIndex = Int(arc4random_uniform(UInt32(ViewController.TOTAL_IMAGES)))
if self.selectedPictureArray.contains("quote\(randomIndex).jpg") {
continue
}else {
break
}
}
let imageName = "quote\(randomIndex).jpg"
self.selectedPictureArray.append(imageName)
print(imageName)
quoteImage.image = UIImage(named: imageName)
let index = self.pictureArray.index(of: imageName)
self.pictureArray.remove(at: index!)
}
Happy coding :)
Related
I just start learning swift.
I installed Cocoapod and tried to see if i can implemented.
i truly dont know how to fix this problem, the rest of the code will be provided down below.
override func viewDidLoad() {
super.viewDidLoad()
let menuItemImage = UIImage(named: "bg-menuitem")!
let menuItemHighlitedImage = UIImage(named: "bg-menuitem-highlighted")!
let starImage = UIImage(named: "icon-star")!
let starMenuItem1 = PathMenuItem(image: menuItemImage, highlightedImage: menuItemHighlitedImage, contentImage: starImage)
let starMenuItem2 = PathMenuItem(image: menuItemImage, highlightedImage: menuItemHighlitedImage, contentImage: starImage)
let starMenuItem3 = PathMenuItem(image: menuItemImage, highlightedImage: menuItemHighlitedImage, contentImage: starImage)
let starMenuItem4 = PathMenuItem(image: menuItemImage, highlightedImage: menuItemHighlitedImage, contentImage: starImage)
let starMenuItem5 = PathMenuItem(image: menuItemImage, highlightedImage: menuItemHighlitedImage, contentImage: starImage)
let items = [starMenuItem1, starMenuItem2, starMenuItem3, starMenuItem4, starMenuItem5]
let startItem = PathMenuItem(image: UIImage(named: "bg-addbutton")!,
highlightedImage: UIImage(named: "bg-addbutton-highlighted"),
contentImage: UIImage(named: "icon-plus"),
highlightedContentImage: UIImage(named: "icon-plus-highlighted"))
let menu = PathMenu(frame: view.bounds, startItem: startItem, items: items)
menu.delegate = self
menu.startPoint = CGPointMake(UIScreen.mainScreen().bounds.width/2, self.view.frame.size.height - 30.0)
menu.menuWholeAngle = CGFloat(M_PI) - CGFloat(M_PI/5)
menu.rotateAngle = -CGFloat(M_PI_2) + CGFloat(M_PI/5) * 1/2
menu.timeOffset = 0.0
menu.farRadius = 110.0
menu.nearRadius = 90.0
menu.endRadius = 100.0
menu.animationDuration = 0.5
// 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.
}
}
Delegate protocol (PathMenuDelegate)
_ how to implement the rest of code show below into the viewDidLoad
func pathMenu(menu: PathMenu, didSelectIndex idx: Int)
func pathMenuDidFinishAnimationClose(menu: PathMenu)
func pathMenuDidFinishAnimationOpen(menu: PathMenu)
func pathMenuWillAnimateOpen(menu: PathMenu)
func pathMenuWillAnimateClose(menu: PathMenu)
class ViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
~~~
menu.delegate = self
self.view.addSubview(menu)
}
}
extension ViewController: PathMenuDelegate {
func didSelect(on menu: PathMenu, index: Int) {
print("Select the index : \(index)")
}
func willStartAnimationOpen(on menu: PathMenu) {
print("Menu will open!")
}
func willStartAnimationClose(on menu: PathMenu) {
print("Menu will close!")
}
func didFinishAnimationOpen(on menu: PathMenu) {
print("Menu was open!")
}
func didFinishAnimationClose(on menu: PathMenu) {
print("Menu was closed!")
}
}
You can't put these code in viewDidLoad.
Make the respective viewController conform to the PathMenuDelegate
And Start typing the protocol functions you'll get suggestions for 'em
Everything you're questioning, just see the author's sample code here: Very simple and straight forward. Those delegate's functions are just for handling events from the lib. And the sample here just prints something to show you that, "Your code works".
https://github.com/pixyzehn/PathMenu/blob/master/PathMenu-Sample/PathMenu-Sample/ViewController.swift
I have an UIPageViewController with a number in the center of each VC in it.
I want that when I swipe from view to view, the number will begin at 0 and count up until it gets to the correct number (or if the number is negative - count down) like in this gif:
https://d13yacurqjgara.cloudfront.net/users/345970/screenshots/2126044/shot.gif
How can I do that?
Thank you!
You can use NSTimer to achieve this.
Here is example project I created for you.
Create layout like this:
Then in your ViewController do like so:
import UIKit
class ViewController: UIViewController {
#IBOutlet var countingLabel: UILabel!
var number = 0
var destinationNumber = 30
var timer: NSTimer!
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 startButtonTapped(sender: AnyObject) {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "countUp", userInfo: nil, repeats: true)
}
func countUp() {
if number < destinationNumber {
number += 1
countingLabel.text = "\(number)"
} else {
timer.invalidate()
}
}
}
It will work.
Do not overcomplicate with timers and invalidations, etc.
extension UILabel {
func countAnimation(upto: Double) {
let from: Double = text?.replace(string: ",", replacement: ".").components(separatedBy: CharacterSet.init(charactersIn: "-0123456789.").inverted).first.flatMap { Double($0) } ?? 0.0
let steps: Int = 20
let duration = 0.350
let delay = duration / Double(steps)
let diff = upto - from
for i in 0...steps {
DispatchQueue.main.asyncAfter(deadline: .now() + delay * Double(i)) {
self.text = "\(from + diff * (Double(i) / Double(delay)))"
}
}
}
}
I am in the process of writing a Simon style memory game, the phase of the game where the program shows the user the current list of stuff to remember seems to run instantly.
The idea is to step through the list (in the code I have placed 1 of each item as debug data) and change the colour on screen for a set period then move to the next.
I thought using for each item in memory array and then call a simple procedure to check which one it is and then change colour for a set period then back to original.
The code I have added here will work if I put breaks in between the test change colour (grey) and the original colour. But for some reason the timer does not seem too work.
Any ideas ?
import UIKit
import Foundation
var gameisrunning = false
var playererror = false
var memoryArray = [Int]()
var currentScore = 0
var timer = NSTimer()
class ViewController: UIViewController {
#IBAction func startGameButton(sender: UIButton) {
if gameisrunning == false {
gameisrunning = true
memoryArray.append(1) //for debug
memoryArray.append(2) //for debug
memoryArray.append(3) //for debug
memoryArray.append(4) //for debug
print(memoryArray) //for debug
gameStart()
} else {
}
}
//these are to be implemented once i get the showing sequence sorted.
#IBAction func redButton(sender: UIButton) {
}
#IBAction func greenButton(sender: UIButton) {
}
#IBAction func yellowButton(sender: UIButton) {
}
#IBAction func blueButton(sender: UIButton) {
}
#IBOutlet weak var redLabel: UILabel!
#IBOutlet weak var greenLabel: UILabel!
#IBOutlet weak var yellowLabel: UILabel!
#IBOutlet weak var blueLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
func addAnotherItemToMemory () {
// adds another item to the memory
memoryArray.append(Int(arc4random_uniform(4)+1))
}
func gameStart () {
// main body of game
showPlayerTheMemory()
}
func showPlayerTheMemory () {
// go through list and highlight the colors one at a time
for eachItem in memoryArray {
self.showColor(eachItem)
}
}
func pauseForAWhile(length: Double) {
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: nil , userInfo: nil, repeats: false)
timer.invalidate()
}
func showColor(buttonItem: Int) {
//check to see which color, change to grey (test color) and back to original after a set time.
if buttonItem == 1 {
self.redLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
self.redLabel.backgroundColor = UIColor.redColor()
print(buttonItem) //for debug
} else if buttonItem == 2 {
self.greenLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
greenLabel.backgroundColor = UIColor.greenColor()
print(buttonItem) //for debug
} else if buttonItem == 3 {
self.yellowLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
yellowLabel.backgroundColor = UIColor.yellowColor()
print(buttonItem) //for debug
} else if buttonItem == 4 {
self.blueLabel.backgroundColor = UIColor.grayColor()
pauseForAWhile(2)
blueLabel.backgroundColor = UIColor.blueColor()
print(buttonItem) //for debug
}
}
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.
}
}
New relevant code changed to :
func colorChange (){
self.redLabel.backgroundColor = UIColor.redColor()
self.blueLabel.backgroundColor = UIColor.blueColor()
self.yellowLabel.backgroundColor = UIColor.yellowColor()
self.greenLabel.backgroundColor = UIColor.greenColor()
}
func showColor(buttonItem: Int, length: Double) {
//check to see which color, change to grey (test color) and back to original after a set time.
if buttonItem == 1 {
self.redLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
} else if buttonItem == 2 {
self.greenLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
} else if buttonItem == 3 {
self.yellowLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
} else if buttonItem == 4 {
self.blueLabel.backgroundColor = UIColor.grayColor()
timer = NSTimer.scheduledTimerWithTimeInterval(length, target:self, selector: ("colorChange") , userInfo: nil, repeats: false)
print(buttonItem) //for debug
}
}
I have been scratching head all day trying to solve this issue which is baffling me. I have copied the new latest code in below, please discard code above.
I have four labels coloured red blue green and yellow. The array which has test data of 4 3 2 1 inside needs to step through each item - change the colour of the label for x secs then return it to normal colour. I have tried NSTimer, I have tried the current delay as in the code attached. Am I missing something as to where I place the code - should it be under viewdidload ??? I have tried for loops and the current code example shows switch in case it acted differently - it didnt !!
What happens basically is simultaneously all labels go grey (test colour right now) and then all go original colour after the x sec delay.
I need some help before I go insane. I honestly know it is something basic but I just cannot figure it out.
import UIKit
import Foundation
var gameisrunning = false
var playererror = false
var memoryArray = [Int]()
var currentScore = 0
class ViewController: UIViewController {
#IBAction func startGameButton(sender: UIButton) {
if gameisrunning == false {
gameisrunning = true
memoryArray.append(4) //for debug
memoryArray.append(3) //for debug
memoryArray.append(2) //for debug
memoryArray.append(1) //for debug
print(memoryArray) //for debug
gameStart()
} else {
}
}
//these are to be implemented once i get the showing sequence sorted.
#IBAction func redButton(sender: UIButton) {
}
#IBAction func greenButton(sender: UIButton) {
}
#IBAction func yellowButton(sender: UIButton) {
}
#IBAction func blueButton(sender: UIButton) {
}
#IBOutlet weak var redLabel: UILabel!
#IBOutlet weak var greenLabel: UILabel!
#IBOutlet weak var yellowLabel: UILabel!
#IBOutlet weak var blueLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
func addAnotherItemToMemory () {
// adds another item to the memory
memoryArray.append(Int(arc4random_uniform(4)+1))
}
func gameStart () {
// main body of game
showPlayerTheMemory()
}
func delayProg (){
//attempt 100093287492 to get a delay in program
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(2.0 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
self.blueLabel.backgroundColor = UIColor.blueColor()
self.yellowLabel.backgroundColor = UIColor.yellowColor()
self.greenLabel.backgroundColor = UIColor.greenColor()
self.redLabel.backgroundColor = UIColor.redColor()
}
}
func showPlayerTheMemory () {
// go through list and highlight the colors one at a time
for var i=0; i <= memoryArray.count-1; i++ {
self.showColor(memoryArray[i])
}
}
func showColor(buttonItem: Int) {
//check to see which color, change to grey (test color) and back to original after a set time.
switch (buttonItem) {
case 1:
self.redLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
case 2:
self.greenLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
case 3:
self.yellowLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
case 4:
self.blueLabel.backgroundColor = UIColor.grayColor()
delayProg()
print(buttonItem) //for debug
default:
print("error")
}
}
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.
}
}
Here is an example of proper implementation of NSTimer()
var myTimer = NSTimer()
func startTimer() {
myTimer = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: "myFunction", userInfo: nil, repeats: true)
}
func myFunction() {
myTimer.invalidate()
//do other stuff
}
//the selector is "myFunction", this will be the name of the function that you wish to call every time the timer reaches its specified intervl
//the interval in this case is 10 seconds. In my experience NSTimer is good down to the second but is not precise enough beyond that
//repeats: true ... this will tell the timer to repeat its action consistently firing the selector each time the given time interval is reached. If repeat is set to false then the timer only fires once
//use myTimer.invalidate to stop the timer and to stop calling the selector.
be sure to invalidate your timer or set repeats: false to make sure it doesn't go forever. Make sure your selector is spelled exactly the same as your function. if your function is func myFunction() then the selector should be "myFunction". Make sure you specify a valid time interval, which is taken as seconds.
So, my assignment for my class is to make an app for the 7th graders at my school to work on math.
In this particular view controller, I am making something that they can practice prime numbers with. I couldn't get everything to load using the override func viewDidLoad(){} so I implemented a button to activate the random number, modulus to check if it's prime, and insert it into the label.
This is ALL of my code that is inside of the Class:
#IBAction func primeBack(sender: UIButton) {
self.performSegueWithIdentifier("primeBack", sender: nil)
}
#IBOutlet var start: UIButton!
#IBOutlet var primeNum: UILabel!
var num = 1
var check = Double()
var temp2 = Double()
let lower : UInt32 = 1
let upper : UInt32 = 100
override func viewDidLoad() {
super.viewDidLoad()
primeNum.hidden = true
start.hidden = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func start(sender: UIButton) {
primeNum.hidden = false
let temp1 = (arc4random_uniform(upper - lower) + lower)
primeNum.text = String(temp1)
while num <= 12
{
check = Double(temp1) / Double(num)
temp2 = check % 1
if temp2 == 0
{
num++
}
else
{
num = 12
}
}
start.hidden = true
}
#IBAction func prime(sender: UIButton) {
if temp2 == 0
{
self.performSegueWithIdentifier("primeCorrect", sender: nil)
}
else
{
self.performSegueWithIdentifier("primeIncorrect", sender: nil)
}
}
#IBAction func notPrime(sender: UIButton) {
if temp2 == 0
{
self.performSegueWithIdentifier("primeIncorrect", sender: nil)
}
else
{
self.performSegueWithIdentifier("primeCorrect", sender: nil)
}
}
Debug and watch what happens to your value of temp2. I suspect it never becomes equal to zero, hence your while loop never ends
This is a Tip Calculator Project and It must have a settings view where I select the default tip rate. I have some issues with passing data, when I select a default tip percentage it doesn't change in the View Controller, also I want to make the app remember the default rate when I close the app and reopened. I will really appreciate that some one corrects my code and test it. This is for entering a Computer Science Program in college, I don't have previous experience with any programming language before.
100
TipPercentageLabel.text = "(tipDisplay)%"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupContainer() {
tipSlider.minimumValue = 0
tipSlider.maximumValue = 100
tipSlider.value = 20
tipSlider.addTarget(self, action: "sliderTipChanged:", forControlEvents: .ValueChanged)
personsStepper.minimumValue = 1
personsStepper.maximumValue = 30
personsStepper.value = 1
personsStepper.addTarget(self, action: "sliderPersonChanged:", forControlEvents: .ValueChanged)
amountTextField.text = ""
refreshCalculation()
}
#IBAction func OnEditingFieldBill(sender: AnyObject) {
refreshCalculation()
}
func refreshCalculation() {
numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
if let amount = numberFormatter.numberFromString(amountTextField.text!) as? Double {
let tipAmount = amount * tipPercentage
let totalBill = amount + tipAmount
let billPerPerson = totalBill / Double(numberOfPerson)
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
tipAmountLabel.text = numberFormatter.stringFromNumber(tipAmount)
totalBillLabel.text = numberFormatter.stringFromNumber(totalBill)
billPerPersonLabel.text = numberFormatter.stringFromNumber(billPerPerson)
} else {
tipAmountLabel.text = "-"
totalBillLabel.text = "-"
billPerPersonLabel.text = "-"
}
numberFormatter.numberStyle = NSNumberFormatterStyle.PercentStyle
numberFormatter.minimumFractionDigits = 1
numberFormatter.maximumFractionDigits = 1
TipPercentageLabel.text = self.numberFormatter.stringFromNumber(tipPercentage)
numberOfPersonLabel.text = "\(numberOfPerson)"
}
#IBAction func sliderTipChanged(sender: AnyObject) {
tipPercentage = Double(round(tipSlider.value)) / 100
refreshCalculation()
}
#IBAction func StepperPersonChanged(sender: AnyObject) {
numberOfPerson = Int(round(personsStepper.value))
refreshCalculation()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let id = segue.identifier {
if id == "show settings" {
if let SettingsViewController = segue.destinationViewController as? SettingsViewController {
}
}
}
}
}
SETTINGS VIEW CONTROLLER
import UIKit
class SettingsViewController: UIViewController {
#IBOutlet weak var tipControl: UISegmentedControl!
var tipRates:Double?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func DefaultRate(sender: AnyObject) {
va
if let tip = tipRates {
ViewController.tipPercentage = tip/100
Replace you DefaultRate IBAction with this:
#IBAction func changevalue(sender: UISegmentedControl) {
var tipRate = [5, 10, 15, 20, 25, 30]
tipRates = Double(tipRate[tipControl.selectedSegmentIndex])
delegate?.tipPercentageChanged(tipRates!) print("(tipRates)")
NSUserDefaults.standardUserDefaults().setDouble(tipRates!, forKey: "DefaultTipRate")
NSUserDefaults.standardUserDefaults().synchronize()
}
And set the event as "Value Changed" as shown in the image below
I strongly recommend you go through a few introductory iOS & Swift tutorials online. SO is not the place to get beginner level understanding of programming topics. Some recommended tutorials:
Swift 2 Tutorial
Apple swift tutorials