Swift UIView.animate between many different strings

uiview animation from bottom to top swift
uiview popup animation swift
swift constraint animation not working
types of animation in swift
uiview animation from right to left swift
uiview.animate completion
uiview animation move from left to right swift
uiview zoom in/out animation swift

I'm looking to recreate the animation on the Welcome screen present on the iPhone once booted up from a fresh device - the greetings you see in various different languages seen here - https://www.youtube.com/watch?v=YZDbGyvAK7o

My issue is that I have about 20+ strings I want to fade in/out between but I'm not convinced that creating 20+ different UILabels is the most efficient method to approach this. There must be another way to do this rather than hard coding it.

Here's what I have so far with keyframes

//textC UIlabel declaration and initialising its' properties.   
UIView.animateKeyframes(withDuration: total, delay: 0, options: [.calculationModeLinear, .repeat], animations: {

    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1/total, animations: {
                let attributedStrD = NSMutableAttributedString(string: countryArr[0], attributes: resultAttributes as [NSAttributedStringKey : Any])
                print(attributedStrD.string)
                textC.attributedText = attributedStrD

            })

    UIView.addKeyframe(withRelativeStartTime: 1/total, relativeDuration: 1/total, animations: {
                let attributedStrD = NSMutableAttributedString(string: countryArr[0], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.alpha = 0
                textC.attributedText = attributedStrD

            })
    UIView.addKeyframe(withRelativeStartTime: 2/total, relativeDuration: 1/total, animations: {
                textC.alpha = 1
                let attributedStrD = NSMutableAttributedString(string: countryArr[1], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.attributedText = attributedStrD
                print("2")
            })
    UIView.addKeyframe(withRelativeStartTime: 3/total, relativeDuration: 1/total, animations: {
                textC.alpha = 0

                let attributedStrD = NSMutableAttributedString(string: countryArr[1], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.attributedText = attributedStrD
            })

    UIView.addKeyframe(withRelativeStartTime: 4/total, relativeDuration: 1/total, animations: {
                textC.alpha = 1

                let attributedStrD = NSMutableAttributedString(string: countryArr[2], attributes: resultAttributes as [NSAttributedStringKey : Any])
                textC.attributedText = attributedStrD
            })

    UIView.addKeyframe(withRelativeStartTime: 5/total, relativeDuration: 1/total, animations: {
                textC.alpha = 0

                let attributedStrD = NSMutableAttributedString(string: countryArr[2], attributes: resultAttributes as [NSAttributedStringKey : Any])
        textC.attributedText = attributedStrD
            })
        }) { (_) in
             print("done animation")
        }

This also doesn't quite work, as the text doesn't update, instead the last string in the array is seen when built and run on the simulator.

Update: Though I wanted to avoid timers it seems that's the only method that works for me. So I made a UIView extension that fades the label using a CATransitionView, you can find an example of it on stackoverflow. Then I created a Timer that would run the fade animation and cycle through the strings using one label

You can use UIView.transition to cross fade the text in a single UILabel, using a Timer to initiate the next animation:

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

    self.textLabel.text = self.strings[stringIndex]

    let _ = Timer.scheduledTimer(withTimeInterval: 3.0, repeats: true) { (timer) in
        self.stringIndex = (self.stringIndex + 1) % self.strings.count

        UIView.transition(with: self.textLabel, duration: 0.8, options: .transitionCrossDissolve, animations: {
            self.textLabel.text = ""
        }, completion: { (Void) in
            UIView.transition(with: self.textLabel, duration: 0.8, options: .transitionCrossDissolve, animations: {
                self.textLabel.text = self.strings[self.stringIndex]
            }, completion: nil)
        })

    }
}

Animations. Animations provide fluid visual transitions between different states of your user interface. In iOS, animations are used extensively to  The UIView class is a key subclassing point for visual content that also requires user interactions. Although there are many good reasons to subclass UIView, it is recommended that you do so only when the basic UIView class or the standard system views do not provide the capabilities that you need. Subclassing requires more work on your part to

You can achieve it using this:

    Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true) { (timer) in
        UIView.animate(withDuration: 1.0, animations: {
            self.label.alpha = 0
        }, completion: { (finished) in
            UIView.animate(withDuration: 1.0, animations: {
                self.label.alpha = 1.0

                let text = self.quotes.remove(at: 0)
                self.label.text = text
                self.quotes.insert(text, at: self.quotes.count-1)
            })
        })

    }

iOS 2.0–13.0Deprecated; Mac Catalyst 13.0–13.0Deprecated; tvOS 9.0–13.0​Deprecated This method does nothing if called from outside of an animation block. class func beginAnimations(String?, context: UnsafeMutableRawPointer​?) In the session they claim that the animations will all finish at the same time but this is not the case in my testing (see the GIF in the question). The workaround suggested in the session is to keep a counter going for how many times the animation started vs. completed and perform the post-animation actions when the counter reaches zero.

@bigubosu This should work:

var strings = ["1", "2", "3", "4", "5", "6"]

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    animate()
}

func animate() {
    guard strings.count > 0 else { return }

    let alpha:CGFloat = textLabel.alpha == 0 ? 1 : 0

    if alpha == 1 {
        textLabel.text = strings.remove(at: 0)
    }

    UIView.animate(withDuration: 2.0, animations: { [weak self] in
        self?.textLabel.alpha = alpha
    }, completion: { [weak self]  (finished) in
        self?.animate()
    })
}

Learn to use Auto Layout's constraint animations on iOS in Xcode with Swift. You have likely seen a bunch of animations if you own an Apple device, from third-party be animating view constraints, we should at the very least know what (Don't worry too much about the other two options which handle  A string is a series of characters, such as "Swift", that forms a collection. Strings in Swift are Unicode correct and locale insensitive, and are designed to be efficient. The String type bridges with the Objective-C class NSString and offers interoperability with C functions that works with strings.

Animation in iOS is done by starting an animation block, then telling iOS If you want to remove the view from its superview once the fade has  Operating on substrings is fast and efficient because a substring shares its storage with the original string. The Substring type presents the same interface as String, so you can avoid or defer any copying of the string’s contents. The following example creates a greeting string, and then finds the substring of the first sentence:

Having looked at FPS, let's now delve into the different core iOS frameworks trigger the animation of the button's frame changing from current frame: you can define multiple animations with different timings and transitions. These include the UTF-8 encoding form (which encodes a string as 8-bit code units), the UTF-16 encoding form (which encodes a string as 16-bit code units), and the UTF-32 encoding form (which encodes a string as 32-bit code units). Swift provides several different ways to access Unicode representations of strings.

Let's learn how to chain animations together in today's lesson to achieve some stunning Duration: 17:21 Posted: Mar 29, 2018 By default, most types in the standard library are hashable, including strings, numeric and Boolean types, enumeration cases without associated values, and even sets themselves. Swift makes it as easy to create a new set as to create a new array. Simply assign an array literal to a variable or constant with the Set type specified.

Comments
  • You only need one label - See the answer here (Not the accepted answer, the highest voted one) - It is Objective C, but you should be able to figure out how to use transitionWithView in Swift pretty easily
  • Thanks. Saw the answer you were referring to. But I don't see how using the superivew as the argument would be the approach to use lots of different strings/labels
  • No, I have no idea what that comment is about. You only need one UILabel; you just animate the new piece of text into it
  • Just one label would suffice. All you need to do is alter the animate the alpha of that label and when alpha is 1 (meaning it is not visible) update the text of the label.