自定义segue动画 - 闪烁

时间:2014-07-01 10:57:20

标签: ios swift

我的自定义segue动画效果很好,但有时我会在动画过程中看到白色闪烁/闪烁。有关如何避免这种情况的任何提示或建议?

这是我的代码:

import UIKit

@objc(InsetBlurModalSeque) class InsetBlurModalSeque: UIStoryboardSegue {

    override func perform() {
        let sourceViewController = self.sourceViewController as UIViewController
        let destinationViewController = self.destinationViewController as UIViewController

        // Make sure the background is ransparent
        destinationViewController.view.backgroundColor = UIColor.clearColor()

        // Take screenshot from source VC
        UIGraphicsBeginImageContext(sourceViewController.view.bounds.size)
        sourceViewController.view.drawViewHierarchyInRect(sourceViewController.view.frame, afterScreenUpdates:true)
        var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        // Blur screenshot
        var blurredImage:UIImage = image.applyBlurWithRadius(5, tintColor: UIColor(white: 1.0, alpha: 0.0), saturationDeltaFactor: 1.3, maskImage: nil)

        // Crop screenshot, add to view and send to back
        let blurredBackgroundImageView : UIImageView = UIImageView(image:blurredImage)
        blurredBackgroundImageView.clipsToBounds = true;
        blurredBackgroundImageView.contentMode = UIViewContentMode.Center
        let insets:UIEdgeInsets = UIEdgeInsetsMake(20, 20, 20, 20);
        blurredBackgroundImageView.frame = UIEdgeInsetsInsetRect(blurredBackgroundImageView.frame, insets)

        destinationViewController.view.addSubview(blurredBackgroundImageView)
        destinationViewController.view.sendSubviewToBack(blurredBackgroundImageView)

        // Add original screenshot behind blurred image
        let backgroundImageView : UIImageView = UIImageView(image:image)
        destinationViewController.view.addSubview(backgroundImageView)
        destinationViewController.view.sendSubviewToBack(backgroundImageView)

        // Add the destination view as a subview, temporarily – we need this do to the animation
        sourceViewController.view.addSubview(destinationViewController.view)

        // Set initial state of animation
        destinationViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
        blurredBackgroundImageView.alpha = 0.0;
        backgroundImageView.alpha = 0.0;

        // Animate
        UIView.animateWithDuration(0.5,
            delay: 0.0,
            usingSpringWithDamping: 0.6,
            initialSpringVelocity: 0.0,
            options: UIViewAnimationOptions.CurveLinear,
            animations: {
                destinationViewController.view.transform = CGAffineTransformIdentity
                blurredBackgroundImageView.alpha = 1.0
                backgroundImageView.alpha = 1.0;

            },
            completion: { (fininshed: Bool) -> () in
                // Remove from temp super view
                destinationViewController.view.removeFromSuperview()

                sourceViewController.presentViewController(destinationViewController, animated: false, completion: nil)
            }
        )

    }

}

感谢。

0 个答案:

没有答案