marcosgriselli / ViewAnimator

ViewAnimator brings your UI to life with just one line
MIT License
7.28k stars 485 forks source link

self?.transform = CGAffineTransform.identity #8

Closed tteabag closed 6 years ago

tteabag commented 6 years ago

Expected Behavior

Actual Behavior

if view has a tranform before animation,the tranform will become CGAffineTransform.identity when animation finish

Steps to Reproduce the Problem

import Foundation import UIKit

/// Animations completion block public typealias CompletionBlock = (()->()) private var kViewAnimationPreTransformKey = "kViewAnimationPreTransformKey"

// MARK: - UIView extension with animations. public extension UIView {

var preTransform: CGAffineTransform? {
    get {
        return objc_getAssociatedObject(self, &kViewAnimationPreTransformKey) as? CGAffineTransform
    }
    set {
        objc_setAssociatedObject(self, &kViewAnimationPreTransformKey, newValue, .OBJC_ASSOCIATION_RETAIN)
    }
}

/// Performs the animation.
///
/// - Parameters:
///   - animations: Array of Animations to perform on the animation block.
///   - initialAlpha: Initial alpha of the view prior to the animation.
///   - finalAlpha: View's alpha after the animation.
///   - delay: Time Delay before the animation.
///   - duration: TimeInterval the animation takes to complete.
///   - completion: CompletionBlock after the animation finishes.
public func animate(animations: [Animation],
                    initialAlpha: CGFloat = 0.0,
                    finalAlpha: CGFloat = 1.0,
                    delay: Double = 0,
                    duration: TimeInterval = ViewAnimatorConfig.duration,
                    completion: CompletionBlock? = nil) {

    // Apply initial transform and alpha
    animations.forEach {
        preTransform = transform
        transform = transform.concatenating($0.initialTransform)
    }
    alpha = initialAlpha

    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        UIView.animate(withDuration: duration, animations: { [weak self] in
            self?.transform = self?.preTransform ?? CGAffineTransform.identity
            self?.alpha = finalAlpha
            }, completion: { _ in
                completion?()
        })
    }
}

}

marcosgriselli commented 6 years ago

Added on version 1.0.2