AnyTransition.combined()

// 1. AnyTransition([.scale, .opacity, ...])
// 2. .combined(.scale, .opacity, ...)
// 3. AnyTransition(.scale, .opacity, ...)
extension AnyTransition {
    
    // โญ๏ธ 1. combining transitions
    // AnyTransition([.scale, .opacity, ...]) ........ (1)
    public init(_ transitions: [AnyTransition]) {
        let intialValue = AnyTransition.identity
        // โญ๏ธ ไฝฟ็”จ `reduce(_:_)`
        self = transitions.reduce(intialValue) { (result, transition) in
            result.combined(with: transition)
        }
    }
    
    // โญ๏ธ 2. when AnyTransition can be inferred, use๏ผš
    // .combined(.move, .scale, ...)
    public static func combined(_ transitions: AnyTransition...) -> AnyTransition {
        .init(transitions)
    }
}

extension AnyTransition: ExpressibleByArrayLiteral {
    // โญ๏ธ 3. convenience init
    // AnyTransition(.move, .scale, .opacity, ...)
    public init(arrayLiteral elements: AnyTransition...) {
        self.init(elements)    // ไฝฟ็”จ (1)
    }
}

Last updated