Hello,
I'm attempting to create a basic showMore/showLess control at the bottom of a large block of text. However it is currently animating in a suboptimal way. When I tap to expand my text block:
- the text block expands right away
- the text label changes from 'More' to 'Less' right away
- the chevron animates both the rotationEffect and its change in position
The problem is animating the change of position is odd as it is moving 'over' the expanded text block.
My fallback position is to remove the rotationEffect and have the Image(systemName:) choose either "chevron.up" or "chevron.down" depending on is expanded. But is there a way to either:
- animate the rotationEffect, but have the position change instantly -or-
- have the more/less label and the chevron both animate the position change together?
thanks in advance for any help
struct ContentView: View {
@State private var isExpanded = false
let text = "That you have wronged me doth appear in this: You have condemned and noted Lucius Pella For taking bribes here of the Sardians, wherein my letters, praying on his side because I knew the man, were slighted off."
var body: some View {
VStack(alignment: .leading) {
Text(text)
.frame(maxWidth: 300)
.lineLimit(isExpanded ? nil : 2)
footer
}
.padding()
}
@ViewBuilder
private var footer: some View {
HStack {
Text(isExpanded ? "Less" : "More")
Button {
isExpanded.toggle()
} label: {
Image(systemName: "chevron.up")
.rotationEffect(isExpanded ? .degrees(180) : .zero)
}
}
}
}