{"id":752,"date":"2020-08-15T20:26:18","date_gmt":"2020-08-15T12:26:18","guid":{"rendered":"http:\/\/123.57.164.21\/?p=752"},"modified":"2020-08-15T20:39:31","modified_gmt":"2020-08-15T12:39:31","slug":"swiftui%e5%8a%a8%e7%94%bb3-animatablemodifier","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=752","title":{"rendered":"SwiftUI\u52a8\u753b(3) AnimatableModifier"},"content":{"rendered":"\n<p>\u5728\u524d\u4e24\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528<code>Animatable<\/code>\u548c<code>GeometryEffect<\/code>\u6765\u5b9e\u73b0\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u52a8\u753b\uff0c\u5176\u57fa\u672c\u539f\u7406\uff0c\u662f\u6839\u636e<code>animatableData<\/code>\u6765\u81ea\u7531\u63a7\u5236\u5f62\u53d8\u3002<\/p>\n\n\n\n<p>\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5e26\u6765\u66f4\u4e3a\u5f3a\u5927\u7684\u4e00\u4e2a\u5de5\u5177AnimatableModifier,\u5b83\u4e4b\u6240\u4ee5\u5f3a\u5927\uff0c\u662f\u56e0\u4e3a\u5b83\u4e0d\u4ec5\u5b9e\u73b0\u4e86Animatable\u534f\u8bae\uff0c\u8fd8\u5b9e\u73b0\u4e86ViewModifier\u534f\u8bae\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u80fd\u591f\u5229\u7528\u8fd9\u4e24\u4e2a\u534f\u8bae\u7684\u4f18\u52bf\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e<code>ViewModifier<\/code>\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de<code>some View<\/code>,\u8fd9\u8ba9\u6211\u4eec\u80fd\u591f\u4e0d\u65ad\u7684\u5f80\u539f\u6765\u7684view\u4e0a\u589e\u52a0\u65b0\u7684view\u3002\u4ee3\u7801\u64cd\u4f5c\u8d77\u6765\u4e5f\u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">1. Animating Text<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"259\" height=\"224\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-84c6744035e5bb1b.gif\" alt=\"\" class=\"wp-image-753\"\/><\/figure><\/div>\n\n\n\n<p>\u5c0f\u8bd5\u725b\u5200\uff0c\u6211\u4eec\u770b\u4e0a\u8fb9\u7684gif\u56fe\uff0c\u8fd9\u4ecd\u7136\u662f\u4e00\u4e2apercent\u52a8\u753b\uff0c\u6839\u636epercent(0~1)\uff0c\u6211\u4eec\u753b\u4e00\u4e2a\u8def\u5f84\uff0c\u5f53\u7136\uff0c\u8be5\u8def\u5f84\u6211\u4eec\u4f7f\u7528\u4e86<code>path.strokedPath(.init(lineWidth: 10, dash: [6, 3], dashPhase: 20))<\/code>.<\/p>\n\n\n\n<p>\u5176\u4e2d<code>dashPhase<\/code>\u8868\u793a\u865a\u7ebf\u5f00\u59cb\u7684\u70b9\uff0c\u8fd9\u91cc\u4e0d\u591a\u505a\u89e3\u91ca\uff0c\u4e00\u4e2a\u5c0f\u6280\u5de7\uff0c\u5229\u7528\u8fd9\u4e2a\u503c\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e9b\u6709\u610f\u601d\u7684\u52a8\u753b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct ContentView: View {\n    @State private var phase: CGFloat = 0\n\n    var body: some View {\n        Rectangle()\n            .strokeBorder(style: StrokeStyle(lineWidth: 4, dash: [10], dashPhase: phase))\n            .frame(width: 200, height: 200)\n            .onAppear { self.phase -= 20 }\n            .animation(Animation.linear.repeatForever(autoreverses: false))\n    }\n}<\/pre>\n\n\n\n<p>\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"281\" height=\"289\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-7b7b3ba38286f226.gif\" alt=\"\" class=\"wp-image-755\"\/><\/figure><\/div>\n\n\n\n<p>\u56de\u5230\u6b63\u6587\uff0c\u5927\u5bb6\u4ed4\u7ec6\u601d\u8003\uff0c\u6211\u4eec\u5982\u679c\u7528<code>GeometryEffect<\/code>\u4e5f\u80fd\u5b9e\u73b0\u6700\u4e0a\u8fb9\u8fdb\u5ea6\u7684\u95ee\u9898\uff0c\u4f46\u662f\u6211\u4eec\u65e0\u6cd5\u663e\u793a30%\uff0c40%\u8fd9\u6837\u7684\u95ee\u9898\u3002\u770b\u770b\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct Example10: View {\n    @State private var pct: CGFloat = 0\n    \n    var body: some View {\n        VStack {\n            Spacer()\n            \n            Indicator(pct: pct)\n            \n            Spacer()\n            \n            HStack(spacing: 10) {\n                MyButton(label: \"20%\", font: .subheadline) {\n                    withAnimation(.easeInOut(duration: 1.0)) {\n                        self.pct = 0.2\n                    }\n                }\n                MyButton(label: \"60%\", font: .subheadline) {\n                    withAnimation(.easeInOut(duration: 1.0)) {\n                        self.pct = 0.6\n                    }\n                }\n                MyButton(label: \"100%\", font: .subheadline) {\n                    withAnimation(.easeInOut(duration: 1.0)) {\n                        self.pct = 1.0\n                    }\n                }\n            }\n            \n            Spacer()\n        }\n        \n    }\n}\n\nstruct Indicator: View {\n    var pct: CGFloat\n    \n    var body: some View {\n        Circle()\n            .fill(LinearGradient(gradient: .init(colors: [.green, .orange]), startPoint: .topLeading, endPoint: .bottomTrailing))\n            .frame(width: 150, height: 150)\n        .modifier(IndicatorModirier(pct: pct))\n    }\n}\n\nstruct IndicatorModirier: AnimatableModifier {\n    var pct: CGFloat = 0\n    \n    var animatableData: CGFloat {\n        get {\n            pct\n        }\n        set {\n            pct = newValue\n        }\n    }\n    \n    func body(content: Content) -> some View {\n        content\n            .overlay(ArcShape(pct: pct).foregroundColor(.orange))\n            .overlay(LabelView(pct: pct))\n    }\n    \n    struct ArcShape: Shape {\n        var pct: CGFloat\n        \n        func path(in rect: CGRect) -> Path {\n            var path = Path()\n            \n            path.addArc(center: CGPoint(x: rect.width \/ 2.0, y: rect.height \/ 2.0),\n                        radius: rect.height \/ 2.0 + 5.0,\n                        startAngle: .degrees(0),\n                        endAngle: .degrees(Double(pct) * 360),\n                        clockwise: false)\n            return path.strokedPath(.init(lineWidth: 10, dash: [6, 3, 20, 3], dashPhase: 0))\n        }\n    }\n    \n    struct LabelView: View {\n        var pct: CGFloat\n        \n        var body: some View {\n            Text(\"\\(pct * 100, specifier: \"%.0f\") %\")\n                .font(.largeTitle)\n                .bold()\n                .foregroundColor(.white)\n        }\n    }\n}<\/pre>\n\n\n\n<p>\u5176\u4e2d\uff0c\u6700\u6838\u5fc3\u7684\u4ee3\u7801\u662f\u4e0b\u8fb9\u8fd9\u51e0\u884c\uff0c\u6709\u4e86content\uff0c\u4f60\u5c31\u53ef\u4ee5\u505a\u51fa\u4f60\u60f3\u8981\u7684\u4efb\u4f55\u52a8\u753b\u6837\u5f0f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    func body(content: Content) -> some View {\n        content\n            .overlay(ArcShape(pct: pct).foregroundColor(.orange))\n            .overlay(LabelView(pct: pct))\n    }<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">2. Animating Gradients<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-86.png\" alt=\"\" class=\"wp-image-782\" width=\"291\" height=\"297\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-86.png 624w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-86-294x300.png 294w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-86-230x234.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-86-350x357.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-86-480x489.png 480w\" sizes=\"(max-width: 291px) 100vw, 291px\" \/><\/figure><\/div>\n\n\n\n<p>\u5f53\u6211\u4eec\u5e73\u65f6\u9700\u8981\u5bf9Gradient\u8fdb\u884c\u52a8\u753b\u65f6\u4f1a\u6709\u4e00\u4e9b\u9650\u5236\uff0c\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9start point\u548cend point\u8fdb\u884c\u52a8\u753b\uff0c\u4f46\u662f\u5374\u65e0\u6cd5\u5bf9\u989c\u8272\u8fdb\u884c\u52a8\u753b\u3002<\/p>\n\n\n\n<p>\u4f46\u6709\u4e86AnimatableModifier\uff0c\u5c31\u53d8\u5f97\u7b80\u5355\u5f88\u591a\uff0c\u6211\u4eec\u5148\u770b\u628a\u6700\u7ec8\u6548\u679c\u505a\u8fdb\u4e00\u6b65\u7684\u62c6\u5206\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"392\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-79-1024x392.png\" alt=\"\" class=\"wp-image-757\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-1024x392.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-300x115.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-768x294.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-830x318.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-230x88.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-350x134.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79-480x184.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-79.png 1322w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u5b9a\u4e49\u8fdb\u5ea6\uff0c\u6211\u4eec\u4ecd\u7136\u4f7f\u7528percent\uff0c\u5b83\u7684\u503c\u4e3a0\uff0c0.1\uff0c 0.2 &#8230; 1\uff0c\u8fd9\u4e2a\u503c\u662f\u7cfb\u7edf\u6839\u636e\u52a8\u753b\u9700\u8981\u81ea\u52a8\u8ba1\u7b97\u7684\u3002<\/p>\n\n\n\n<p>\u5927\u5bb6\u60f3\u4e00\u60f3\uff0c\u8fd9\u4e2a\u77e9\u5f62\u533a\u57df\u4e0a\u6709\u5f88\u591a\u50cf\u7d20\uff0c\u6211\u4eec\u4e0d\u53ef\u80fd\u628a\u6bcf\u4e00\u4e2a\u50cf\u7d20\u5bf9\u5e94\u4e8epercent\u7684\u53d8\u5316\u90fd\u8ba1\u7b97\u51fa\u6765\uff0c\u6bcf\u4e00\u6b21percent\u53d8\u5316\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5e94\u7528\u4e00\u4e2a<code>LinearGradient<\/code>\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    func body(content: Content) -> some View {\n        var gColors = [Color]()\n        \n        for i in 0..&lt;from.count {\n            gColors.append(colorMixer(c1: from[i], c2: to[i], pct: pct))\n        }\n        \n        return RoundedRectangle(cornerRadius: 15)\n            .fill(LinearGradient(gradient: .init(colors: gColors), startPoint: .topLeading, endPoint: .bottomTrailing))\n            .frame(width: 300, height: 300)\n    }<\/pre>\n\n\n\n<p>\u4ece\u4e0a\u8fb9\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u6211\u4eec\u4fee\u6539\u4e86<code>gColors<\/code>,\u800c\u8fd9\u4e2a<code>gColors<\/code>\u662f\u901a\u8fc7\u4e0b\u8fb9\u7684\u4ee3\u7801\u8ba1\u7b97\u51fa\u6765\u7684\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    func colorMixer(c1: UIColor, c2: UIColor, pct: CGFloat) -> Color {\n        let rgbSpace = CGColorSpaceCreateDeviceRGB()\n        guard let cc1 = c1.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else {\n            return Color(c1)\n        }\n        guard let cc2 = c2.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else {\n            return Color(c2)\n        }\n\n        let r = cc1[0] + (cc2[0] - cc1[0]) * pct\n        let g = cc1[1] + (cc2[1] - cc1[1]) * pct\n        let b = cc1[2] + (cc2[2] - cc1[2]) * pct\n\n        return Color(red: Double(r), green: Double(g), blue: Double(b))\n    }<\/pre>\n\n\n\n<p><strong>\u6ce8\u610f\uff0c\u7c7b\u4f3c\u4e8eblack\uff0cwhite\u8fd9\u6837\u7684\u989c\u8272\uff0c\u5b83\u7684rgb\u989c\u8272\u7a7a\u95f4\u53ea\u6709\u4e24\u4e2a\u503c\uff0c\u5206\u522b\u8868\u793a\u57fa\u4e8ewhite\u7684\u503c\u548c\u900f\u660e\u5ea6\uff0c\u5176\u4ed6\u7684\u989c\u8272\u7684rgb\u7a7a\u95f4\u67094\u4e2a\u503c\u3002<\/strong><\/p>\n\n\n\n<p>\u8bf4\u767d\u4e86\uff0c\u5c31\u662f\u6839\u636epercent\u6df7\u5408from\u548cto\u7684\u989c\u8272\uff0c\u5728\u672c\u4f8b\u4e2d\uff0cfrom\u6df7\u5408\u7684\u989c\u8272\u662f<code>green<\/code>\u548c<code>yellow<\/code>\uff0cto\u6df7\u5408\u7684\u989c\u8272\u662f<code>blue<\/code>\u548c<code>red<\/code>\u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct Example11: View {\n    @State private var animate = false\n    \n    var body: some View {\n        let gradient1: [UIColor] = [.green, .blue]\n        let gradient2: [UIColor] = [.yellow, .red]\n        \n        return VStack {\n            Spacer()\n            \n            RoundedRectangle(cornerRadius: 15)\n                .frame(width: 300, height: 300)\n                .modifier(GradientAnimatabelModifier(from: gradient1, to: gradient2, pct: animate ? 1 : 0))\n            \n            Spacer()\n            \n            Button(\"\u989c\u8272\u8fc7\u6e21\") {\n                withAnimation(.easeInOut(duration: 1.0)) {\n                    self.animate.toggle()\n                }\n            }\n            \n            Spacer()\n        }\n    }\n}\n\nstruct GradientAnimatabelModifier: AnimatableModifier {\n    let from: [UIColor]\n    let to: [UIColor]\n    var pct: CGFloat\n    \n    var animatableData: CGFloat {\n        get {\n            pct\n        }\n        set {\n            pct = newValue\n        }\n    }\n    \n    func body(content: Content) -> some View {\n        var gColors = [Color]()\n        \n        for i in 0..&lt;from.count {\n            gColors.append(colorMixer(c1: from[i], c2: to[i], pct: pct))\n        }\n        \n        return RoundedRectangle(cornerRadius: 15)\n            .fill(LinearGradient(gradient: .init(colors: gColors), startPoint: .topLeading, endPoint: .bottomTrailing))\n            .frame(width: 300, height: 300)\n    }\n\n    func colorMixer(c1: UIColor, c2: UIColor, pct: CGFloat) -> Color {\n        let rgbSpace = CGColorSpaceCreateDeviceRGB()\n        guard let cc1 = c1.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else {\n            return Color(c1)\n        }\n        guard let cc2 = c2.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else {\n            return Color(c2)\n        }\n\n        let r = cc1[0] + (cc2[0] - cc1[0]) * pct\n        let g = cc1[1] + (cc2[1] - cc1[1]) * pct\n        let b = cc1[2] + (cc2[2] - cc1[2]) * pct\n\n        return Color(red: Double(r), green: Double(g), blue: Double(b))\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">3. More Text Animation<\/h4>\n\n\n\n<p>\u8fd9\u4e00\u5c0f\u8282\uff0c\u4e3b\u8981\u8bb2\u89e3\u5982\u4f55\u5b9e\u73b0\u4e0b\u8fb9\u7684\u52a8\u753b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"377\" height=\"238\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-00a477d74422e0da-1-1.gif\" alt=\"\" class=\"wp-image-760\"\/><\/figure><\/div>\n\n\n\n<p>\u5f88\u660e\u663e\uff0c\u52a8\u753b\u662f\u9488\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684\u5b57\u7b26\u6765\u6267\u884c\u7684\uff0c<strong>\u57fa\u672c\u539f\u7406\u662f\u968f\u7740percent\u7684\u53d8\u5316\uff0c\u5b57\u7b26\u7684scale\u968f\u4e4b\u53d8\u5316\u3002<\/strong><\/p>\n\n\n\n<p>\u56e0\u6b64\u6211\u4eec\u7684\u95ee\u9898\u53d8\u4e3a\u5982\u4f55\u6839\u636epercent\u8ba1\u7b97\u76f8\u5e94\u4f4d\u7f6e\u7684\u5b57\u7b26\u7684scale\uff1f<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-87-1024x503.png\" alt=\"\" class=\"wp-image-784\" width=\"545\" height=\"267\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-1024x503.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-300x147.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-768x378.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-830x408.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-230x113.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-350x172.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87-480x236.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-87.png 1078w\" sizes=\"(max-width: 545px) 100vw, 545px\" \/><\/figure><\/div>\n\n\n\n<p>\u6211\u4eec\u8bbe\u7f6e\u4e00\u4e2a\u5177\u6709\u4e00\u5b9a\u5bbd\u5ea6\u7684\u533a\u57df\uff0c\u901a\u8fc7\u8ba1\u7b97\u8be5\u533a\u57df\u7684\u5b57\u7b26\u7684\u4f4d\u7f6e\u6765\u8ba1\u7b97\u76f8\u5e94\u7684scale\uff0c\u6838\u5fc3\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">        func valueInCurve(pct: Double, total: Double, x: Double, waveWidth: Double) -> Double {\n            let chunk = waveWidth \/ total\n            let m = 1 \/ chunk\n            let offset = (chunk - (1 \/ total)) * pct\n            let lowerLimit = (pct - chunk) + offset\n            let upperLimit = (pct) + offset\n\n            guard x >= lowerLimit &amp;&amp; x &lt; upperLimit else { return 0 }\n            \n            let angle = ((x - pct - offset) * m)*360-90\n            \n            return (sin(angle.rad) + 1) \/ 2\n        }<\/pre>\n\n\n\n<p>\u5173\u4e8e\u4e0a\u8fb9\u7684\u4ee3\u7801\uff0c\u505a\u51e0\u70b9\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul><li><code>chunk<\/code>\u8868\u793ascale\u8303\u56f4\u7684\u5927\u5c0f\uff0c\u5176\u4e2d<code>waveWidth<\/code>\u8868\u793ascale\u7684\u5b57\u7b26\u6570\uff0c\u5728\u672c\u4ee3\u7801\u4e2d\u7684\u503c\u4e3a6<\/li><li>pct\u7684\u53d6\u503c\u8303\u56f4\u662f0~1\uff0cx\u7684\u503c\u901a\u8fc7<code>n\/total<\/code>\u8ba1\u7b97\u51fa\u6765\uff0c\u56e0\u6b64x\u7684\u53d6\u503c\u8303\u56f4\u4e3a<code>0..&lt;1<\/code><\/li><li><code>offset<\/code>\u6307\u7684\u662fpct\u70b9\u53f3\u4fbf\u7684\u8ddd\u79bb\uff0c\u5b83\u52a8\u6001\u53d8\u5316<\/li><li><code>lowerLimit<\/code>\u548c<code>upperLimit<\/code>\u662fscale\u8303\u56f4\u7684\u4e0a\u4e0b\u9650<\/li><li><code>angle<\/code>\u8868\u793a\u89d2\u5ea6\uff0c\u5b83\u7684\u53d6\u503c\u8303\u56f4\u662f-90~-450\uff0c\u56e0\u6b64<code>(sin(angle.rad) + 1) \/ 2<\/code>\u8ba1\u7b97\u7684\u7ed3\u679c\u8303\u56f4\u662f0~1<\/li><\/ul>\n\n\n\n<p>scale\u8303\u56f4\u8ba1\u7b97\u7684\u793a\u610f\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-80.png\" alt=\"\" class=\"wp-image-765\" width=\"532\" height=\"403\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-80.png 766w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-80-300x227.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-80-230x174.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-80-350x265.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-80-480x363.png 480w\" sizes=\"(max-width: 532px) 100vw, 532px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-81-1024x625.png\" alt=\"\" class=\"wp-image-766\" width=\"496\" height=\"303\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-1024x625.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-300x183.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-768x469.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-830x506.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-230x140.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-350x214.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81-480x293.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-81.png 1180w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/figure><\/div>\n\n\n\n<p>\u53cd\u6620\u5230\u4e0a\u56fe\u4e2d<code>angle<\/code>\u7684\u53d6\u503c\u8303\u56f4\u4e3aB\u70b9\u5230A\u70b9\uff0c\u56e0\u6b64\u8ba1\u7b97<code>sin(angle.rad)<\/code>\u7684\u53d6\u503c\u8303\u56f4\u6b63\u597d\u662f-1~1\uff0c\u8fd9\u91cc\u8fb9\u6bd4\u8f83\u5de7\u5999\u7684\u662fB-&gt;C\u662f\u4e0a\u5347\u8fc7\u7a0b\uff0cC-&gt;A\u662f\u4e0b\u964d\u8fc7\u7a0b\uff0c\u6b63\u597d\u7b26\u5408\u6ce2\u5f62\u3002<\/p>\n\n\n\n<p>\u5927\u5bb6\u4ed4\u7ec6\u601d\u8003\u5c31\u80fd\u591f\u60f3\u660e\u767d\u5176\u4e2d\u5965\u5999\uff0c\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">extension Double {\n    var rad: Double { return self * .pi \/ 180 }\n    var deg: Double { return self * 180 \/ .pi }\n}\n\nstruct Example12: View {\n    @State private var flag = false\n    \n    var body: some View {\n        VStack {\n            Spacer()\n            Color.clear.overlay(WaveText(\"The SwiftUI Lab\", waveWidth: 6, pct: flag ? 1.0 : 0.0).foregroundColor(.blue)).frame(height: 40)\n            Color.clear.overlay(WaveText(\"swiftui-lab.com\", waveWidth: 6, pct: flag ? 0.0 : 1.0, size: 18).foregroundColor(.green)).frame(height: 30)\n            Spacer()\n        }.onAppear {\n            withAnimation(Animation.easeInOut(duration: 2.0).repeatForever()) {\n                self.flag.toggle()\n            }\n        }.navigationBarTitle(\"Example 12\")\n    }\n}\n\nstruct WaveText: View {\n    let text: String\n    let pct: Double\n    let waveWidth: Int\n    var size: CGFloat\n    \n    init(_ text: String, waveWidth: Int, pct: Double, size: CGFloat = 34) {\n        self.text = text\n        self.waveWidth = waveWidth\n        self.pct = pct\n        self.size = size\n    }\n    \n    var body: some View {\n        Text(text).foregroundColor(Color.clear).modifier(WaveTextModifier(text: text, waveWidth: waveWidth, pct: pct, size: size))\n    }\n    \n    struct WaveTextModifier: AnimatableModifier {\n        let text: String\n        let waveWidth: Int\n        var pct: Double\n        var size: CGFloat\n        \n        var animatableData: Double {\n            get { pct }\n            set { pct = newValue }\n        }\n        \n        func body(content: Content) -> some View {\n            \n            HStack(spacing: 0) {\n                ForEach(Array(text.enumerated()), id: \\.0) { (n, ch) in\n                    Text(String(ch))\n                        .font(Font.custom(\"Menlo\", size: self.size).bold())\n                        .scaleEffect(self.effect(self.pct, n, self.text.count, Double(self.waveWidth)))\n                }\n            }\n        }\n        \n        func effect(_ pct: Double, _ n: Int, _ total: Int, _ waveWidth: Double) -> CGFloat {\n            let n = Double(n)\n            let total = Double(total)\n            \n            return CGFloat(1 + valueInCurve(pct: pct, total: total, x: n\/total, waveWidth: waveWidth))\n        }\n        \n        func valueInCurve(pct: Double, total: Double, x: Double, waveWidth: Double) -> Double {\n            let chunk = waveWidth \/ total\n            let m = 1 \/ chunk\n            let offset = (chunk - (1 \/ total)) * pct\n            let lowerLimit = (pct - chunk) + offset\n            let upperLimit = (pct) + offset\n\n            guard x >= lowerLimit &amp;&amp; x &lt; upperLimit else { return 0 }\n            \n            let angle = ((x - pct - offset) * m)*360-90\n            \n            return (sin(angle.rad) + 1) \/ 2\n        }\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">4. Getting Creative<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"218\" height=\"174\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-12703d25ffc09f44.gif\" alt=\"\" class=\"wp-image-768\"\/><\/figure><\/div>\n\n\n\n<p>\u7c7b\u4f3c\u4e8e\u8fd9\u6837\u7684counter\uff0c\u770b\u4e0a\u53bb\u5b9e\u73b0\u8d77\u6765\u4f1a\u975e\u5e38\u9ebb\u70e6\uff0c\u4f46\u662f\u7528<code>AnimatableModifier<\/code>\u5b9e\u73b0\u8d77\u6765\u5c31\u975e\u5e38easy\u3002\u8bb0\u4f4f\u4e00\u70b9\uff0c<code>AnimatableModifier<\/code>\u6700\u725b\u903c\u7684\u5730\u65b9\u5728\u4e8e\uff0c\u80fd\u591f\u8ba9\u6211\u4eec\u5904\u7406\u67d0\u4e00\u4e2a\u65f6\u95f4\u70b9\u7684\u72b6\u6001\uff0c\u5c31\u597d\u8c61\u628a\u4e00\u7cfb\u5217\u7684\u53d8\u5316\u5b9a\u683c\u5728\u67d0\u4e00\u65f6\u523b\uff0c\u6211\u4eec\u53ea\u5173\u5fc3\u90a3\u4e00\u65f6\u523b\u7684\u6837\u5f0f\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2aCounter\u6700\u6838\u5fc3\u7684\u60f3\u6cd5\u5c31\u662f\u5206\u522b\u8ba1\u7b97\u5341\u4f4d\u6570\u548c\u4e2a\u4f4d\u6570\u5728\u67d0\u4e2a\u6570\u503c\u65f6\u7684offset\u3002<\/p>\n\n\n\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u5f53\u6570\u5b57\u4e3a21\u65f6\uff0c\u4ed6\u7684offset\u4e3a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-82.png\" alt=\"\" class=\"wp-image-770\" width=\"427\" height=\"323\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-82.png 710w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-82-300x227.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-82-230x174.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-82-350x265.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-82-480x364.png 480w\" sizes=\"(max-width: 427px) 100vw, 427px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u80fd\u5927\u5bb6\u4e0d\u7406\u89e3\uff0c\u6309\u7406\u8bf421\u6b63\u597d\u662f\u6574\u6570\uff0coffset\u5e94\u8be5\u4e3a0\u554a\u3002\u5176\u5b9e\u4f60\u60f3\u7684\u4e5f\u6ca1\u9519\uff0c\u4f46\u662f\u4f5c\u8005\u8fd9\u91cc\u7684\u4ee3\u7801\u662f\u4ee5n+1\u4e3a\u57fa\u51c6\u7684\uff0c21 + 1 \u662f22\uff0c \u6b63\u597doffset\u4e3a1.<\/p>\n\n\n\n<p>\u518d\u770b\u4e00\u4e2a21.3\u7684\u4f8b\u5b50<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-83.png\" alt=\"\" class=\"wp-image-771\" width=\"347\" height=\"327\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-83.png 564w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-83-300x283.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-83-230x217.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-83-350x330.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-83-480x453.png 480w\" sizes=\"(max-width: 347px) 100vw, 347px\" \/><\/figure><\/div>\n\n\n\n<p>21 + 1 = 22 \uff0c\u663e\u793a\u4e8622\u7684offset\u4e3a0.7\uff0c\u518d\u770b\u4e00\u4e2a21.8\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/image-84.png\" alt=\"\" class=\"wp-image-772\" width=\"318\" height=\"311\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-84.png 454w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-84-300x293.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-84-230x225.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2020\/08\/image-84-350x342.png 350w\" sizes=\"(max-width: 318px) 100vw, 318px\" \/><\/figure><\/div>\n\n\n\n<p>\u76f8\u4fe1\u5927\u5bb6\u5df2\u7ecf\u660e\u767d\u8fd9\u4e2aoffset\u662f\u4ec0\u4e48\u610f\u601d\u4e86\uff0c\u4e0a\u8fb9\u6f14\u793a\u7684\u662f\u4e2a\u4f4d\u6570\u7684offset\uff0c\u5341\u4f4d\u6570\u7684offset\u540c\u7406\u3002\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct Example13: View {\n    @State private var number: Double = 21\n    \n    var body: some View {\n        VStack {\n            Spacer()\n            \n            MovingCounter(number: number)\n            \n            Spacer()\n            \n            HStack {\n                MyButton(label: \"35\", font: .headline) {\n                    withAnimation(Animation.interpolatingSpring(mass: 0.1, stiffness: 1, damping: 0.4, initialVelocity: 0.8)) {\n                        self.number = 35\n                    }\n                }\n                \n                MyButton(label: \"44\", font: .headline) {\n                    withAnimation(Animation.interpolatingSpring(mass: 0.1, stiffness: 1, damping: 0.4, initialVelocity: 0.8)) {\n                        self.number = 44\n                    }\n                }\n                \n                MyButton(label: \"87\", font: .headline) {\n                    withAnimation(Animation.interpolatingSpring(mass: 0.1, stiffness: 1, damping: 0.4, initialVelocity: 0.8)) {\n                        self.number = 87\n                    }\n                }\n            }\n            \n            Spacer()\n        }\n    }\n}\n\nstruct MovingCounter: View {\n    var number: Double\n    \n    var body: some View {\n        Text(\"00\")\n        .modifier(CounterAnimatableModifier(number: number))\n    }\n    \n    struct CounterAnimatableModifier: AnimatableModifier {\n        var number: Double\n        \n        var animatableData: Double {\n            get {\n                number\n            }\n            set {\n                number = newValue\n            }\n        }\n        \n        func body(content: Content) -> some View {\n            let n = self.number + 1\n            \n            let uoffset = getOffsetForUnitDigit(n)\n            let toffset = getOffsetForTenDigit(n)\n            \n            let u = [n - 2, n - 1, n, n + 1, n + 2].map{ getUnitDigit($0) }\n            let x = getTenDigit(n)\n            var t = [abs(x - 2), abs(x - 1), abs(x), abs(x + 1), abs(x + 2)]\n            t = t.map{ getUnitDigit(Double($0)) }\n            \n            let font = Font.custom(\"Menlo\", size: 34).bold()\n            \n            return HStack(alignment: .top, spacing: 0) {\n                VStack {\n                    Text(\"\\(t[0])\").font(font)\n                    Text(\"\\(t[1])\").font(font)\n                    Text(\"\\(t[2])\").font(font)\n                    Text(\"\\(t[3])\").font(font)\n                    Text(\"\\(t[4])\").font(font)\n                }\n                .foregroundColor(.green)\n                .modifier(ShiftEffect(pct: toffset))\n                \n                VStack {\n                    Text(\"\\(u[0])\").font(font)\n                    Text(\"\\(u[1])\").font(font)\n                    Text(\"\\(u[2])\").font(font)\n                    Text(\"\\(u[3])\").font(font)\n                    Text(\"\\(u[4])\").font(font)\n                }\n                .foregroundColor(.green)\n                .modifier(ShiftEffect(pct: uoffset))\n            }\n            .clipShape(CounterShap())\n            .overlay(CounterBorder())\n            .background(CounterBackground())\n        }\n        \n        func getUnitDigit(_ number: Double) -> Int {\n            return abs(Int(number) - (Int(number) \/ 10) * 10)\n        }\n        \n        func getTenDigit(_ number: Double) -> Int {\n            return abs(Int(number) \/ 10)\n        }\n        \n        func getOffsetForUnitDigit(_ number: Double) -> CGFloat {\n            return 1 - CGFloat(number - Double(Int(number)))\n        }\n        \n        func getOffsetForTenDigit(_ number: Double) -> CGFloat {\n            if getUnitDigit(number) == 0 {\n              return 1 - CGFloat(number - Double(Int(number)))\n            }\n            return 0\n        }\n    }\n    \n    struct ShiftEffect : GeometryEffect {\n        var pct: CGFloat = 1.0\n        \n        func effectValue(size: CGSize) -> ProjectionTransform {\n            ProjectionTransform(CGAffineTransform(translationX: 0, y: size.height \/ 5.0 * pct))\n        }\n    }\n    \n    struct CounterShap: Shape {\n        func path(in rect: CGRect) -> Path {\n            var path = Path()\n            \n            let h = rect.height \/ 5.0 + 30\n            let r = CGRect(x: 0, y: (rect.height - h) * 0.5, width: rect.width, height: h)\n            \n            path.addRoundedRect(in: r, cornerSize: CGSize(width: 5.0, height: 5.0))\n            \n            return path\n        }\n    }\n    \n    struct CounterBorder: View {\n        var body: some View {\n            GeometryReader { proxy in\n                RoundedRectangle(cornerRadius: 5.0)\n                    .stroke(lineWidth: 5)\n                    .foregroundColor(.blue)\n                    .frame(width: 80, height: proxy.size.height \/ 5.0 + 30)\n            }\n        }\n    }\n    \n    struct CounterBackground: View {\n        var body: some View {\n            GeometryReader { proxy in\n                RoundedRectangle(cornerRadius: 5.0)\n                    .fill(Color.black)\n                    .frame(width: 80, height: proxy.size.height \/ 5.0 + 30)\n            }\n        }\n    }\n}\n<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">5. Animating Text Color<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"238\" height=\"123\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2020\/08\/1914952-340acf911063a007.gif\" alt=\"\" class=\"wp-image-774\"\/><\/figure><\/div>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u5bf9\u67d0\u4e2aView\u7684<code>foregroundColor<\/code>\u6267\u884c\u989c\u8272\u7684\u6e10\u53d8\u52a8\u753b\uff0c\u4f46\u662f\uff0c\u5f53\u628a\u8fd9\u4e2a\u52a8\u753b\u653e\u5927\u6587\u672c\u4e0a\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u597d\u4f7f\u4e86\uff0c\u5229\u7528<code>AnimatableModifier<\/code>\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u52a8\u753b\u7684\u5b9e\u73b0\u5b9e\u5728\u662f\u592a\u7b80\u5355\u4e86\uff0c\u6211\u4eec\u5c31\u4e0d\u505a\u66f4\u591a\u7684\u89e3\u91ca\u4e86\uff0c\u76f4\u63a5\u4e0a\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">struct Example15: View {\n    @State private var flag = false\n    \n    var body: some View {\n        VStack {\n            AnimatableColorText(from: .systemRed, to: .systemBlue, pct: flag ? 1 : 0) {\n                Text(\"\u6211\u662f\u4e00\u4e2a\u597d\u4eba\").font(.largeTitle).bold()\n            }\n        }\n        .onTapGesture {\n            withAnimation(.easeInOut(duration: 1.0)) {\n                self.flag.toggle()\n            }\n        }\n    }\n}\n\nstruct AnimatableColorText: View {\n    let from: UIColor\n    let to: UIColor\n    let pct: CGFloat\n    let text: () -> Text\n    \n    var body: some View {\n        let textView = text()\n        return textView.foregroundColor(.clear)\n        .modifier(AnimatableColorTextModifier(from: from, to: to, pct: pct, text: textView))\n    }\n}\n\nstruct AnimatableColorTextModifier: AnimatableModifier {\n    let from: UIColor\n    let to: UIColor\n    var pct: CGFloat\n    let text: Text\n    \n    var animatableData: CGFloat {\n        get {\n            pct\n        }\n        set {\n            pct = newValue\n        }\n    }\n    \n    func body(content: Content) -> some View {\n        return text.foregroundColor(colorMixer(c1: from, c2: to, pct: pct))\n    }\n    \n    func colorMixer(c1: UIColor, c2: UIColor, pct: CGFloat) -> Color {\n        let rgbSpace = CGColorSpaceCreateDeviceRGB()\n        guard let cc1 = c1.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else {\n            return Color(c1)\n        }\n        guard let cc2 = c2.cgColor.converted(to: rgbSpace, intent: .defaultIntent, options: nil)?.components else {\n            return Color(c2)\n        }\n\n        let r = cc1[0] + (cc2[0] - cc1[0]) * pct\n        let g = cc1[1] + (cc2[1] - cc1[1]) * pct\n        let b = cc1[2] + (cc2[2] - cc1[2]) * pct\n\n        return Color(red: Double(r), green: Double(g), blue: Double(b))\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">\u603b\u7ed3<\/h4>\n\n\n\n<p><code>AnimatableModifier<\/code>\u7684\u5f3a\u5927\u4e4b\u5904\u5728\u4e8e\u4ed6\u5373\u9075\u5b88\u4e86<code>Animatable<\/code>\u534f\u8bae\uff0c\u53c8\u662f\u4e00\u4e2a<code>ViewModifier<\/code>\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u6839\u636e<code>animatableData<\/code>\u6765\u8fd4\u56de\u4e00\u4e2aView\u3002\u8fd9\u5c31\u50cf\u628a\u4e00\u6bb5\u8fde\u7eed\u7684\u52a8\u753b\uff0c\u6253\u6563\u6210\u4e00\u5f20\u5f20\u7684\u56fe\u7247\u3002<\/p>\n\n\n\n<p><em>\u6ce8\uff1a\u4e0a\u8fb9\u7684\u5185\u5bb9\u53c2\u8003\u4e86\u7f51\u7ad9<a rel=\"noreferrer noopener\" href=\"https:\/\/links.jianshu.com\/go?to=https%3A%2F%2Fswiftui-lab.com%2Fswiftui-animations-part3%2F\" target=\"_blank\">https:\/\/swiftui-lab.com\/swiftui-animations-part3\/<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u524d\u4e24\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Animatable\u548cGeometryEffect\u6765\u5b9e\u73b0\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u52a8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/752"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=752"}],"version-history":[{"count":16,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions"}],"predecessor-version":[{"id":785,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/752\/revisions\/785"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}