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(_ 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


Flag Counter

?xgqfrms 2012-2020

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

原创文章,版权所有??xgqfrms, 禁止转载 ???,侵权必究??!