Skip to content

Commit

Permalink
Fixed the first text to be displayed as the default text.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasudev committed Feb 9, 2022
1 parent d32827d commit b3ac660
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions Sources/AnimateText/AnimateText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public struct AnimateText<E: ATTextAnimateEffect>: View {
/// Used to re-create the view.
@State private var toggle: Bool = false

/// The first text is exposed as the default text.
@State private var isChanged: Bool = false

/// The size of the Text view.
@State private var size: CGSize = .zero

Expand All @@ -63,13 +66,12 @@ public struct AnimateText<E: ATTextAnimateEffect>: View {
}

public var body: some View {
ZStack {
ZStack(alignment: .leading) {
if size.width == 0 {
Text(text)
.lineLimit(1)
.takeSize($size)
}
ZStack(alignment: .leading) {
if !isChanged {
Text(text)
.lineLimit(1)
.takeSize($size)
}else {
HStack(spacing: 0) {
ForEach(Array(elements.enumerated()), id: \.offset) { index, element in
let data = ATElementData(element: element,
Expand All @@ -89,6 +91,7 @@ public struct AnimateText<E: ATTextAnimateEffect>: View {
}
}
.onChange(of: text) { _ in
self.isChanged = true
withAnimation {
value = 0
getText(text)
Expand All @@ -98,13 +101,6 @@ public struct AnimateText<E: ATTextAnimateEffect>: View {
value = 1
}
}
.onAppear{
let text = self.text
self.text = ""
DispatchQueue.main.async {
self.text = text
}
}
}

private func getText(_ text: String) {
Expand Down

0 comments on commit b3ac660

Please sign in to comment.