I'm wondering why my
matchedGeometryEffect
isn't working as expected. Did I do something wrong? Because this guy (at 3:15) did the same and it looks much smoother. Im running the code on a M1 MacBook Air that means the speed is probably not the problem. Here is my Code:struct ContentView: View { @Namespace var namespace @State var bool = true var body: some View { HStack{ if bool { Rectangle() .frame(width: 100, height: 100) .foregroundColor(Color.red) .padding() .onTapGesture { withAnimation(.spring(response: 0.5, dampingFraction: 0.7)){ bool.toggle() } } .matchedGeometryEffect(id: "id", in: namespace) } else { Rectangle() .frame(width: 200, height: 200) .foregroundColor(Color.red) .matchedGeometryEffect(id: "id", in: namespace) } } } }
Thanks for your help, Boothosh
score:3
In SwiftUI, the modifier order matters. This is the same with matchedGeometryEffect(id:in:properties:anchor:isSource:)
.
If you want the rectangles to expand in the center, you need the geometries to align in the center. To do this, we will have Rectangle
s of the same size. This means that both Rectangle
s will have the matchedGeometryEffect
modifier attached, and then styled after.
Code:
HStack {
if bool {
Rectangle()
.matchedGeometryEffect(id: "id", in: namespace) // <- Here
.frame(width: 100, height: 100)
.foregroundColor(Color.red)
.padding()
.onTapGesture {
withAnimation(.spring(response: 0.5, dampingFraction: 0.7)) {
bool.toggle()
}
}
} else {
Rectangle()
.matchedGeometryEffect(id: "id", in: namespace) // <- Here
.frame(width: 200, height: 200)
.foregroundColor(Color.red)
}
}
Credit To: stackoverflow.com
More questions
- Matched Geometry Effect in SwiftUI is not working
- Transition animation not working in SwiftUI
- Color rgb initializer not working as background in SwiftUI
- SwiftUI NavigationLink for iOS 14.5 not working
- Tap Action not working when Color is clear SwiftUI
- Navigation Bar hide is not working in SwiftUI
- SwiftUI View Property willSet & didSet property observers not working
- Custom SceneKit Geometry in Swift on iOS not working but equivalent Objective C code does
- SwiftUI Text alignment with maxWidth not working
- SwiftUI TapGesture and LongPressGesture in ScrollView with tap indication not working
- Navigation View not working properly in SwiftUI on iPad
- SwiftUI swizzling disabled by default, phone auth not working
- SwiftUI custom list with ForEach delete animation not working
- UIImageView contentMode not working after blur effect application
- SwiftUI custom accentColor not working on physical device(iOS 13) but works everywhere else(iOS 14.0+)
- SwiftUI navigationBarBackButtonHidden not working as expected
- NavigationLink buttons on tvOS with SwiftUI not working
- SwiftUI animation within ScrollView not working
- navigationBarHidden() Not working in iOS 15 Beta SwiftUI
- Ternary conditional not working in SwiftUI
- SwiftUI Picker item .foregroundColor() not working
- SwiftUI TextField Simple Example not working
- Alignment not working in ios14, xcode12 swiftUI
- SwiftUI .onDelete is not working in TabView because View is always changing
- Animation in SwiftUI not working properly
- Firebase Analytics - logging screen_view not working in SwiftUI
- SwiftUI ForEach not working after recent update
- SwiftUI navigation link back button working in preview but not in simulator or device
- SwiftUI Universal Links not working for NFC
- NavigationLink in SwiftUI not working anymore
More questions with similar tag
- How to create an empty array in Swift?
- UIActivityIndicator stopAnimating() not working
- Cache image for use offline SDWebImage
- Combine - Subscriber gets cancelled silently when subscribing a second time
- Static Library and Swift
- How to create a range of CGPoints (SWIFT)
- Unit test, to test if a view controller has presented another view controller
- Unable to get an image to fit inside the bounds of a UITextField
- Why does @unknown default (Swift 5) cause compile error?
- How can I tell if my intent is running in the Shortcuts app?
- Bind NSButton state
- Why "Verify your bundle identifier 'org.cocoapods.Alamofire' is correct."?
- Segue all the data of a TableView to a CollectionView swift parse
- Authentication with WKWebView in Swift
- Swift delegate as a function parameter