xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

SwiftUI animation All In One

SwiftUI animation All In One

Command + Click, 跳转到源码

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension View {

    /// Applies the given animation to this view when the specified value
    /// changes.
    ///
    /// - Parameters:
    ///   - animation: The animation to apply. If `animation` is `nil`, the view
    ///     doesn't animate.
    ///   - value: A value to monitor for changes.
    ///
    /// - Returns: A view that applies `animation` to this view whenever `value`
    ///   changes.
    @inlinable public func animation<V>(_ animation: Animation?, value: V) -> some View where V : Equatable

}

extension View where Self : Equatable {

    /// Applies the given animation to this view when this view changes.
    ///
    /// - Parameters:
    ///   - animation: The animation to apply. If `animation` is `nil`, the view
    ///     doesn't animate.
    ///
    /// - Returns: A view that applies `animation` to this view whenever it
    ///   changes.
    @available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
    @inlinable public func animation(_ animation: Animation?) -> some View

}

demo

//
//  BeiJingView.swift
//  BeiJingTourth
//
//  Created by xgqfrms on 2022/5/13.
//

import SwiftUI

//let titleFont = .system(size: 42, weight: .bold, design: .serif);
let titleFont = Font.system(size: 42, weight: Font.Weight.bold, design: Font.Design.serif);



struct BeiJingView: View {
    // private let titleFont = Font.system(size: 42, weight: Font.Weight.bold, design: Font.Design.serif);
    // @State private var animation: Bool = false;
    @State private var animation = false;
    // var animationState = animation ? 1.0 : 0.7;
    // Cannot use instance member 'animation' within property initializer; property initializers run before 'self' is available
  func getAnimationState (_ animation: Bool) -> Double {
      return animation ? 1.0 : 0.7;
    }
    var body: some View {
      VStack {
        Spacer()
        Image("Beijing-Logo")
          .resizable()
          .scaledToFit()
          .frame(width: 240, height: 240, alignment: .center)
          .shadow(color: Color("ColorDark"), radius: 12, x: 0, y: 8)
          .scaleEffect(animation ? 1.0 : 0.7)
          .opacity(animation ? 1.0 : 0.7)
          // .scaleEffect(getAnimationState(self.animation))
          // .opacity(getAnimationState(self.animation))
          // ❌
          // .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true), value: true)
          // .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true), value: 1)
          // value: UUID ✅
          .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true), value: UUID())
          // .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true))
          // ⚠️ 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
          // .withAnimation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true)) ❌
        VStack {
          Text("北京旅游")
            .padding()
            .foregroundColor(.white)
            .font(titleFont)
            // .font(.system(size: 42, weight: .bold, design: .serif))
            .shadow(color: Color("ColorDark"), radius: 4, x: 0, y: 4)
          Text("🇨🇳 北京,是一座朴实亲切而又大气磅礴的城市既能海纳百川,又有着自己独特的风姿,既能独树一帜,又不孤芳自赏。")
            .lineLimit(nil)
            .font(.headline)
            // 前面的覆盖后面属性 ❓逆序, 先进后出,栈
            .foregroundColor(Color("ColorBrown"))
            .foregroundColor(Color.green)
            // 前面的覆盖后面属性 ❓逆序, 先进后出,栈
            .multilineTextAlignment(.center)
            .multilineTextAlignment(.leading)
            .lineSpacing(8)
            // .frame(minWidth: 640,  minHeight: 120)
            .frame(maxWidth: 640, minHeight: 120)
            .padding(.horizontal, 20)
        }
        .padding()
        Spacer()
      }
      .background(
        Image("background")
          .resizable()
          .scaledToFill()
      )
      .edgesIgnoringSafeArea(.all)
      .onAppear {
        self.animation.toggle();
      }
    }
}

'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.

UUID

          // value: UUID ✅
          .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true), value: UUID())
          // .animation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true))
          // ⚠️ 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
          // .withAnimation(Animation.easeInOut(duration: 1.5).repeatForever(autoreverses: true)) ❌
extension View {
	func withoutAnimation() -> some View {
		self.animation(nil, value: UUID())
	}
}

https://developer.apple.com/forums/thread/688947

https://developer.apple.com/documentation/swiftui/view/animation(_:)-1hc0p

https://developer.apple.com/documentation/swiftui/view/animation(_:value:)

https://developer.apple.com/documentation/swiftui/withanimation(:😃

refs



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(167)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2021-05-13 how to delete all localStorage data in js All In One
2021-05-13 js Proxy in Action
2020-05-13 HTTP cache in depth All In One
2020-05-13 Make one your own Online Video Recorder by using WebRTC & vanilla javascript
2019-05-13 How to write a Node.js cli tools step by step tutorials All In One
2019-05-13 svn conflict & svn cleanup
2019-05-13 node.js & read argv
点击右上角即可分享
微信分享提示