1

I have an app that draws names randomly and then displays them in groups of 3 or 4. All is fine, but I would like to delay the output (increase suspense) We currently get "Group 1 consists of Fred, Dave, Steve, John"

We want to see "Group 1 consists of .(pause).....Fred...(pause)......Dave..(pause)......Steve....(pause).....John"

I have tried Sleep(x) but this seems to be ignored.

Here is my simple loop:

The players are stored in array playing. G1 can be anything from 1 to 4. Gr1Text is a textbox.

for i in 0...G1-1 {
    G1Text = (G1Text ?? "") + playing[i] + " , "
    self.Gr1Text.text = G1Text}
}

Just need a pause between displaying the names

thanks

So I have created some code to drop the names using animation

func dropText(_ sender: String)
{
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = sender
    label.font = UIFont.systemFont(ofSize: 32, weight: .bold)
    label.textColor = UIColor.white
    view.addSubview(label)

    var yAnchor = label.centerYAnchor.constraint(equalTo: view.topAnchor, constant: -200)
    yAnchor.isActive = true

    label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
        yAnchor.isActive = false
        yAnchor = label.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 200)
        yAnchor.isActive = true
        UIView.animate(withDuration: 1, delay: 1, usingSpringWithDamping: 0.5, initialSpringVelocity:5, options: .curveEaseIn, animations: {

            self.view.layoutIfNeeded()
        })
}

But I still do not know how to pause between the names. if I use a for .. loop to call this function it just runs them all at the same time?

Thanks

Mahmut Acar
  • 713
  • 2
  • 7
  • 26
Lynxbci
  • 71
  • 7

3 Answers3

0

Just need a pause between displaying the names

It'd probably be easier to select the groups first, and then worry about displaying them however you like. Once you have the groups, you can use a timer to schedule the display of each name. If you don't want to do it that way, then you should still use a timer, and just select the next name and display it in one step, then start another timer to select and display the next one, and so on.

Using a for loop with a delay in the loop is a poor plan because it means that your program can't do anything else, at least on that thread, until the whole process is complete. Apps usually have a lot going on in the background, so blocking the main thread that way is Very Bad Thing.

Makes sense, but when you say use a timer... what timer method?

Whatever works for you. For example, you could use scheduledTimer(withTimeInterval:repeats:block:), and set it up to repeat every second until it's done. Or you could have it just fire once, but start a new timer when the current one fires -- that'd let you use different time intervals, so the user doesn't know exactly when the next name will appear.

Another approach would be to use Core Animation and animate the names onto the screen -- it could be a single long animation that displays all the names with delays between, or a series of animations scheduled for different times. Using animation would let you display the names in an interesting fashion... they could fade in, or slide in from off screen, or appear to fall onto the screen with a little bounce, or whatever.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • Makes sense, but when you say use a timer... what timer method? – Lynxbci Dec 16 '19 at 23:16
  • Core Animation sounds like the thing I need. I only started Xcode/Swift4 on Monday last week, and have a working App, and now trying to enhance, so this is all completely new to me. Thanks I will try and investigate core animation. – Lynxbci Dec 16 '19 at 23:33
0

I have found a solution using

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {}

This works for delaying ideally.

Lynxbci
  • 71
  • 7
0

You can use same label and modify the text within the label. For the intermediate pause, you can use a timer.

func addNamesWithDelay(){
        let friendsName = ["aaa","bbb","ccc","ddd","eee"]
        var nameCnt = 0
        var finalStr = "Group 1 has "
        Timer.scheduledTimer(withTimeInterval: 2, repeats: true) { (timer) in
            let randomNumber = Int.random(in: 0...4) // total 5 players
            if nameCnt == 2 { // assuming one group has 3 players
                timer.invalidate()
            }
            finalStr = finalStr + friendsName[randomNumber] + (nameCnt == 3 ? "" : " ,")
            self.lbl.text = finalStr
            nameCnt += 1
        }
    }
Neethu M
  • 644
  • 6
  • 11