marcosgriselli / ViewAnimator

ViewAnimator brings your UI to life with just one line

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

self?.transform = CGAffineTransform.identity

tteabag opened this issue · comments

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?()
        })
    }
}

}

Added on version 1.0.2