SwiftUI Article View with Expand/Collapse Animation
Here's a complete implementation of an Apple News-like article view with the expand/collapse animation you described. The code is all in a single file with an XCode preview.
import SwiftUI
struct Article: Identifiable {
let id = UUID()
let title: String
let summary: String
let content: String
let color: Color
}
struct ArticleView: View {
@State private var selectedArticle: Article?
@State private var isExpanded = false
let articles = [
Article(
title: "New iPhone Features Announced",
summary: "Apple unveils groundbreaking new camera system and performance improvements.",
content: "At today's event, Apple announced several new features coming to the next iPhone. The new camera system includes improved low-light performance and a periscope zoom lens. The A16 chip promises 20% faster performance while being more energy efficient. The event also showcased new software features like customizable lock screens and improved notifications.",
color: .blue
),
Article(
title: "WWDC 2023 Highlights",
summary: "Key announcements from Apple's developer conference including new MacBooks and iOS 17.",
content: "Apple's Worldwide Developers Conference brought several exciting announcements. The new M2 chip powers updated MacBook Air and Pro models with improved performance and battery life. iOS 17 introduces new communication features like Contact Posters and Live Voicemail. Vision Pro, Apple's new spatial computing platform, was the surprise announcement of the event.",
color: .orange
),
Article(
title: "Apple Watch Series 9 Review",
summary: "Our hands-on experience with Apple's latest smartwatch shows meaningful improvements.",
content: "The Apple Watch Series 9 builds on the success of previous models with a brighter display, faster performance, and new gesture controls. The new S9 chip enables on-device Siri processing for faster responses. Battery life remains at about 18 hours with normal use. While not a revolutionary update, the Series 9 is the most refined Apple Watch yet.",
color: .green
)
]
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 20) {
ForEach(articles) { article in
ArticleTile(article: article, isExpanded: selectedArticle?.id == article.id && isExpanded)
.onTapGesture {
withAnimation(.interactiveSpring(response: 0.5, dampingFraction: 0.8, blendDuration: 0.8)) {
selectedArticle = article
isExpanded = true
}
}
}
}
.padding()
}
.navigationTitle("News")
.overlay {
if isExpanded, let selectedArticle = selectedArticle {
ExpandedArticleView(article: selectedArticle, isExpanded: $isExpanded)
.transition(.asymmetric(
insertion: .identity,
removal: .modifier(
active: ShrinkModifier(origin: articleFrame(selectedArticle), progress: 0),
identity: ShrinkModifier(origin: articleFrame(selectedArticle), progress: 1)
)
))
.zIndex(1)
}
}
}
}
func articleFrame(_ article: Article) -> CGRect {
// This would normally be calculated based on the actual frame of the tile
// For simplicity, we're using a placeholder here
return CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width - 40, height: 120)
}
}
struct ArticleTile: View {
let article: Article
let isExpanded: Bool
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(article.title)
.font(.headline)
.lineLimit(isExpanded ? nil : 1)
Text(article.summary)
.font(.subheadline)
.foregroundColor(.secondary)
.lineLimit(isExpanded ? nil : 2)
}
.padding()
.frame(maxWidth: .infinity, alignment: .leading)
.background(article.color.opacity(0.2))
.cornerRadius(12)
.shadow(color: .black.opacity(0.1), radius: 5, x: 0, y: 2)
}
}
struct ExpandedArticleView: View {
let article: Article
@Binding var isExpanded: Bool
var body: some View {
ZStack(alignment: .topTrailing) {
ScrollView {
VStack(alignment: .leading, spacing: 20) {
Text(article.title)
.font(.title)
.bold()
Text(article.summary)
.font(.title3)
.foregroundColor(.secondary)
Text(article.content)
.font(.body)
.padding(.bottom, 40)
}
.padding()
.padding(.top, 50)
}
.background(article.color.opacity(0.1))
.ignoresSafeArea()
Button(action: {
withAnimation(.interactiveSpring(response: 0.5, dampingFraction: 0.8, blendDuration: 0.8)) {
isExpanded = false
}
}) {
Image(systemName: "xmark.circle.fill")
.font(.system(size: 30))
.foregroundColor(.secondary)
.padding()
}
}
}
}
struct ShrinkModifier: ViewModifier {
let origin: CGRect
let progress: CGFloat
func body(content: Content) -> some View {
content
.frame(width: origin.width + (UIScreen.main.bounds.width - origin.width) * progress,
height: origin.height + (UIScreen.main.bounds.height - origin.height) * progress)
.clipped()
.cornerRadius(12 * (1 - progress) + 0 * progress)
.offset(x: -origin.minX * (1 - progress),
y: -origin.minY * (1 - progress))
.scaleEffect(1 + (0.95 - 1) * (1 - progress))
}
}
struct ArticleView_Previews: PreviewProvider {
static var previews: some View {
ArticleView()
}
}
How It Works
-
Article Structure: The Article struct holds the data for each news item.
-
Main View: ArticleView displays a scrollable list of article tiles.
-
Tile View: ArticleTile shows the compact version of each article (title and summary).
-
Expanded View: ExpandedArticleView shows the full article content when tapped.
-
Animation:
- When you tap a tile, it expands to full screen with a spring animation
- When you dismiss, it shrinks back to its original position
- The animation uses a custom
ShrinkModifier to handle the transition
-
Navigation: The close button in the top-right corner dismisses the expanded view.
The preview should work immediately in Xcode, showing three sample articles with different colors. You can tap them to see the expand/collapse animation.