I'm trying to create an extension to View to easily apply my custom view modifier. Below is the code for my custom viewModifier.
This gives an error
But AnimatableFontModifier does confirm to the AnimatableModifier protocol, so where am I going wrong?
Code Block swift import SwiftUI struct AnimatableFontModifier: AnimatableModifier{ var fontSize: CGFloat var animatableData: CGFloat{ get{ fontSize } set{ fontSize = newValue } } func body(content: Content) -> some View { content.font(Font.system(size: fontSize)) } } extension View { func AnimatableFontModifier(fontSize: CGFloat) -> some View{ self.modifier(AnimatableFontModifier(fontSize: fontSize)) } }
This gives an error
Code Block error Return type of instance method 'AnimatableFontModifier(fontSize:)' requires that 'some View' conform to 'ViewModifier'
But AnimatableFontModifier does confirm to the AnimatableModifier protocol, so where am I going wrong?
You should better avoid method names having the same name with types.
Line 19 of your code AnimatableFontModifier(fontSize:) is calling the extension method, not the initializer of AnimatableModifier.
Code Block extension View { func animatableFont(ofSize fontSize: CGFloat) -> some View{ self.modifier(AnimatableFontModifier(fontSize: fontSize)) } }
Line 19 of your code AnimatableFontModifier(fontSize:) is calling the extension method, not the initializer of AnimatableModifier.