I like the Apple News app. It has a cool interaction pattern where if you see an article…
- User
I like the Apple News app. It has a cool interaction pattern where if you see an article summary tile and tap on it, the full article grows out of the tile. And when you dismiss the full article, it shrinks back down into the original tile.
I'd like to make a SwiftUI app that does the same thing. Can you give me the code to do that, putting everything into a single file with a base view of ArticleView. Include an Xcode preview.
Assistant
Here's a SwiftUI implementation that recreates that Apple News-style expand/collapse animation:
import SwiftUI struct Article: Identifiable { let id = UUID() let title: String let summary: String let content: String let imageName: String } struct ArticleView: View { @State private var selectedArticle: Article? @Namespace private var animationNamespace let articles = [ Article( title: "SwiftUI Advances", summary: "Apple announces major updates to SwiftUI framework", content: "Apple has unveiled significant enhancements to SwiftUI, its declarative framework for building user interfaces across all Apple platforms. The updates include improved performance, new animation APIs, and better integration with UIKit.\n\nThe new features promise to make app development faster and more intuitive, with particular emphasis on complex animations and transitions. Developers can now create more sophisticated user experiences with less code.\n\nThese improvements come as part of Apple's continued investment in SwiftUI as the future of app development on its platforms.", imageName: "swift" ), Article( title: "iOS 18 Features", summary: "Exploring the latest capabilities in iOS 18", content: "iOS 18 brings a host of new features designed to enhance user productivity and privacy. The update includes redesigned widgets, improved multitasking capabilities, and new privacy controls.\n\nOne of the standout features is the enhanced Focus modes, which now offer more granular control over notifications and app access. The Photos app has also received a major overhaul with new AI-powered organization tools.\n\nBattery life improvements and performance optimizations round out this significant update to Apple's mobile operating system.", imageName: "iphone" ), Article( title: "Mac Performance", summary: "M3 chips deliver unprecedented speed", content: "Apple's M3 chip family represents a major leap forward in Mac performance. Built on a 3-nanometer process, these chips offer up to 50% faster CPU performance and 65% faster GPU performance compared to the previous generation.\n\nThe M3 Pro and M3 Max variants bring even more power to professional workflows, with support for up to 128GB of unified memory and advanced machine learning capabilities.\n\nReal-world testing shows dramatic improvements in video editing, 3D rendering, and software development tasks, making these the most powerful chips Apple has ever created.", imageName: "macbook.and.iphone" ) ] var body: some View { ZStack { ScrollView { VStack(spacing: 16) { ForEach(articles) { article in if selectedArticle?.id != article.id { ArticleTile( article: article, namespace: animationNamespace, onTap: { withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) { selectedArticle = article } } ) } else { Color.clear .frame(height: 200) } } } .padding() } if let article = selectedArticle { FullArticleView( article: article, namespace: animationNamespace, onDismiss: { withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) { selectedArticle = nil } } ) } } } } struct ArticleTile: View { let article: Article let namespace: Namespace.ID let onTap: () -> Void var body: some View { VStack(alignment: .leading, spacing: 12) { Image(systemName: article.imageName) .font(.system(size: 40)) .foregroundColor(.blue) .frame(maxWidth: .infinity, alignment: .center) .frame(height: 80) .background(Color.blue.opacity(0.1)) .matchedGeometryEffect(id: "image-\(article.id)", in: namespace) VStack(alignment: .leading, spacing: 8) { Text(article.title) .font(.headline) .foregroundColor(.primary) .matchedGeometryEffect(id: "title-\(article.id)", in: namespace) Text(article.summary) .font(.subheadline) .foregroundColor(.secondary) .lineLimit(2) .matchedGeometryEffect(id: "summary-\(article.id)", in: namespace) } .padding(.horizontal, 16) .padding(.bottom, 16) } .background(Color(UIColor.secondarySystemBackground)) .matchedGeometryEffect(id: "background-\(article.id)", in: namespace) .cornerRadius(12) .shadow(radius: 2) .onTapGesture { onTap() } } } struct FullArticleView: View { let article: Article let namespace: Namespace.ID let onDismiss: () -> Void var body: some View { ScrollView { VStack(alignment: .leading, spacing: 20) { // Header with image ZStack(alignment: .topTrailing) { Image(systemName: article.imageName) .font(.system(size: 60)) .foregroundColor(.blue) .frame(maxWidth: .infinity) .frame(height: 200) .background(Color.blue.opacity(0.1)) .matchedGeometryEffect(id: "image-\(article.id)", in: namespace) Button(action: onDismiss) { Image(systemName: "xmark.circle.fill") .font(.title) .foregroundColor(.gray) .background(Color.white.opacity(0.8)) .clipShape(Circle()) } .padding() } VStack(alignment: .leading, spacing: 16) { Text(article.title) .font(.largeTitle) .fontWeight(.bold) .matchedGeometryEffect(id: "title-\(article.id)", in: namespace) Text(article.summary) .font(.headline) .foregroundColor(.secondary) .matchedGeometryEffect(id: "summary-\(article.id)", in: namespace) Text(article.content) .font(.body) .lineSpacing(8) .transition(.opacity.combined(with: .scale(scale: 0.95))) } .padding(.horizontal) .padding(.bottom, 40) } } .background(Color(UIColor.systemBackground)) .matchedGeometryEffect(id: "background-\(article.id)", in: namespace) .cornerRadius(12) .ignoresSafeArea() .transition(.asymmetric( insertion: .opacity.animation(.easeInOut(duration: 0.2)), removal: .opacity.animation(.easeInOut(duration: 0.2)) )) } } // MARK: - Preview struct ArticleView_Previews: PreviewProvider { static var previews: some View { ArticleView() } }This implementation features:
- Article tiles that display a summary view with an image, title, and brief description
- Smooth expansion animation using
matchedGeometryEffectto transition the tile elements to their full-article positions - Full article view that expands to fill the screen with scrollable content
- Dismiss functionality with an X button that animates the article back to its tile form
- Spring animations that feel natural and responsive
The key to this effect is the
@NamespaceandmatchedGeometryEffectmodifiers, which create smooth transitions between the tile and full article states. The animation tracks the image, title, summary, and background between both views, creating the illusion that the tile is expanding into the full article.You can customize the animation timing by adjusting the spring parameters in the
withAnimationcalls, or switch to different animation types if you prefer a different feel.- User
When the article opens, it expands up from the tile. But when the article is dismissed, it's not shrinking back down to the tile. Also, you should go back to the main list with a left arrow or swipe from the left edge, not a close button.
Assistant
You're right! Let me fix those issues. Here's the updated code with proper shrinking animation and navigation-style dismissal:
import SwiftUI struct Article: Identifiable { let id = UUID() let title: String let summary: String let content: String let imageName: String } struct ArticleView: View { @State private var selectedArticle: Article? @Namespace private var animationNamespace let articles = [ Article( title: "SwiftUI Advances", summary: "Apple announces major updates to SwiftUI framework", content: "Apple has unveiled significant enhancements to SwiftUI, its declarative framework for building user interfaces across all Apple platforms. The updates include improved performance, new animation APIs, and better integration with UIKit.\n\nThe new features promise to make app development faster and more intuitive, with particular emphasis on complex animations and transitions. Developers can now create more sophisticated user experiences with less code.\n\nThese improvements come as part of Apple's continued investment in SwiftUI as the future of app development on its platforms.", imageName: "swift" ), Article( title: "iOS 18 Features", summary: "Exploring the latest capabilities in iOS 18", content: "iOS 18 brings a host of new features designed to enhance user productivity and privacy. The update includes redesigned widgets, improved multitasking capabilities, and new privacy controls.\n\nOne of the standout features is the enhanced Focus modes, which now offer more granular control over notifications and app access. The Photos app has also received a major overhaul with new AI-powered organization tools.\n\nBattery life improvements and performance optimizations round out this significant update to Apple's mobile operating system.", imageName: "iphone" ), Article( title: "Mac Performance", summary: "M3 chips deliver unprecedented speed", content: "Apple's M3 chip family represents a major leap forward in Mac performance. Built on a 3-nanometer process, these chips offer up to 50% faster CPU performance and 65% faster GPU performance compared to the previous generation.\n\nThe M3 Pro and M3 Max variants bring even more power to professional workflows, with support for up to 128GB of unified memory and advanced machine learning capabilities.\n\nReal-world testing shows dramatic improvements in video editing, 3D rendering, and software development tasks, making these the most powerful chips Apple has ever created.", imageName: "macbook.and.iphone" ) ] var body: some View { ZStack { ScrollView { VStack(spacing: 16) { ForEach(articles) { article in ArticleTile( article: article, namespace: animationNamespace, isHidden: selectedArticle?.id == article.id, onTap: { withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) { selectedArticle = article } } ) } } .padding() } if let article = selectedArticle { FullArticleView( article: article, namespace: animationNamespace, onDismiss: { withAnimation(.spring(response: 0.6, dampingFraction: 0.8)) { selectedArticle = nil } } ) .zIndex(1) } } } } struct ArticleTile: View { let article: Article let namespace: Namespace.ID let isHidden: Bool let onTap: () -> Void var body: some View { VStack(alignment: .leading, spacing: 12) { Image(systemName: article.imageName) .font(.system(size: 40)) .foregroundColor(.blue) .frame(maxWidth: .infinity, alignment: .center) .frame(height: 80) .background(Color.blue.opacity(0.1)) .matchedGeometryEffect(id: "image-\(article.id)", in: namespace) VStack(alignment: .leading, spacing: 8) { Text(article.title) .font(.headline) .foregroundColor(.primary) .matchedGeometryEffect(id: "title-\(article.id)", in: namespace) Text(article.summary) .font(.subheadline) .foregroundColor(.secondary) .lineLimit(2) .matchedGeometryEffect(id: "summary-\(article.id)", in: namespace) } .padding(.horizontal, 16) .padding(.bottom, 16) } .background( Color(UIColor.secondarySystemBackground) .matchedGeometryEffect(id: "background-\(article.id)", in: namespace) ) .cornerRadius(12) .shadow(radius: 2) .onTapGesture { onTap() } .opacity(isHidden ? 0 : 1) } } struct FullArticleView: View { let article: Article let namespace: Namespace.ID let onDismiss: () -> Void @State private var dragAmount = CGSize.zero @State private var showContent = false var body: some View { GeometryReader { geometry in ScrollView { VStack(alignment: .leading, spacing: 0) { // Navigation Bar HStack { Button(action: onDismiss) { HStack(spacing: 4) { Image(systemName: "chevron.left") .font(.system(size: 20, weight: .semibold)) Text("Articles") .font(.body) } .foregroundColor(.blue) } Spacer() } .padding(.horizontal) .padding(.vertical, 12) .background(Color(UIColor.systemBackground)) // Header with image Image(systemName: article.imageName) .font(.system(size: 60)) .foregroundColor(.blue) .frame(maxWidth: .infinity) .frame(height: 200) .background(Color.blue.opacity(0.1)) .matchedGeometryEffect(id: "image-\(article.id)", in: namespace) VStack(alignment: .leading, spacing: 16) { Text(article.title) .font(.largeTitle) .fontWeight(.bold) .matchedGeometryEffect(id: "title-\(article.id)", in: namespace) Text(article.summary) .font(.headline) .foregroundColor(.secondary) .matchedGeometryEffect(id: "summary-\(article.id)", in: namespace) if showContent { Text(article.content) .font(.body) .lineSpacing(8) .transition(.opacity.combined(with: .move(edge: .bottom))) } } .padding(.horizontal) .padding(.bottom, 40) } } .background( Color(UIColor.systemBackground) .matchedGeometryEffect(id: "background-\(article.id)", in: namespace) ) .cornerRadius(12) .offset(x: dragAmount.width) .gesture( DragGesture() .onChanged { value in if value.translation.width > 0 { dragAmount = value.translation } } .onEnded { value in if value.translation.width > 100 { onDismiss() } else { withAnimation(.spring()) { dragAmount = .zero } } } ) } .ignoresSafeArea() .onAppear { withAnimation(.easeInOut(duration: 0.3).delay(0.2)) { showContent = true } } .onDisappear { showContent = false } } } // MARK: - Preview struct ArticleView_Previews: PreviewProvider { static var previews: some View { ArticleView() } }Key improvements:
-
Fixed shrinking animation: The tile now stays in place with
opacityinstead of being replaced with aColor.clearspacer. This ensures thematchedGeometryEffectcan properly animate back to the original position. -
Navigation-style dismissal:
- Added a back button with chevron and "Articles" text in the navigation area
- Implemented swipe-to-dismiss gesture that tracks horizontal drag
- The view slides with your finger and dismisses if you swipe far enough (>100 points)
-
Better animation flow:
- The article content (full text) appears with a delayed animation after the expansion
- The content disappears immediately when dismissing to keep the shrinking animation clean
-
Proper z-ordering: Added
.zIndex(1)to the full article view to ensure it renders above the tiles during animation
The animation now properly expands from and shrinks back to the original tile position, just like in the Apple News app!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.