0

Salutations,

Allow me to preface this that my search queries may have probably been incorrectly worded as I am certain others have tried to do, yet, I am not able to find it. I have looked into different ways, but none really worked.

So my current code is as follows:

while true {
   currentPicture += 1; //Yes I know that ; is not needed, i come from Java so I                     find it comforting.
   showPico(); //function which shows an image from my array of images, this function handles out of bounds by reseting currentPic to 0 or 4. 
   sleep(2);
}

Now my problem is that once I hit my auto button which is where my code above is located, my app of course freezes. This is normal as it is my understanding that sleep() calls upon the main thread and simply suspends everything until it is done sleeping. However, I do not see it update the picture at all. Therefore, what is the best way to go about create a sort of slide show.

The array of pictures is defined as follows, for those wondering:

var pictureArray = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"]; //I know, highly original names. 

Side note: My knowledge of swift/iOS is now a grand total of 4 days.

SomeStudent
  • 2,856
  • 1
  • 22
  • 36
  • Depending on what you feel most comfortable with, and what the largest amount of pictures you are expecting in the slideshow, you should look into scrollView/collectionView. CollectionView for example, you make a cell the full screen of the view and assign it an imageView, disable scrolling/userInteraction, then scroll to the next indexPath after a certain amount of time. It would be a lot to get into specifically but setting up a timer and learning how to manually scroll a collectionView will be easy to find on SO – JustinM Jan 15 '17 at 05:48

2 Answers2

1

You probably want something like this in your view controller:

var timer: Timer!

func viewDidLoad() {
    super.viewDidLoad()
    timer = Timer.scheduledTimer(timeInterval: 0.1,
                                 target: self,
                                 selector: #selector(self.updateTime),
                                 userInfo: nil,
                                 repeats: true)
}


func updateTime() {
    print("whatever is here happens every 0.1 seconds")
}

func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    timer.invalidate()
}
Daniel Williams
  • 256
  • 1
  • 12
  • I will give this code a whirl soon, what is the purpose of the viewWillDisappear? I assume it has to do with smooth animation, though i do not see it being called anywhere in the viewDidLoad() method, or am I missing something? – SomeStudent Jan 15 '17 at 06:09
  • These are 'life cycle' methods. They are triggered when certain things happen. In this case, the code in viewDidLoad is called when the view controller is loaded. When the view disappears from our view, we invalidate the timer to ensure that it doesn't continue to run in the background. The #selector(self.updateTime) is telling the Timer to use the updateTime() method every 0.1 seconds, according to the scheduleTimer function. Check this out: https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/index.html#//apple_ref/doc/uid/TP40007457 – Daniel Williams Jan 15 '17 at 06:52
  • Gotcha, makes sense. Works very well, thank you. And just a quick follow up, what would be the best way to halt the execution. Say the user wants to manually go through the slide show on their own using next/prev buttons? Cheers! – SomeStudent Jan 15 '17 at 07:28
  • Nvm, figured it out, timer.invalidate(); – SomeStudent Jan 15 '17 at 07:35
0

The good news is you don't have to even do this, the control UIPagingViewController already supports auto running slide shows.

This question details how: Is it possible to Turn page programmatically in UIPageViewController?.

Community
  • 1
  • 1
Rob
  • 11,446
  • 7
  • 39
  • 57