UIViewControllerAnimatedTransitioning:旋转更改后的黑屏碎片

时间:2016-01-25 13:25:24

标签: ios swift uiview

我创建了一个Viewcontrollertransition,只要我不改变设备方向,一切正常。

图1显示了应该的屏幕。然后我切换到下一个viewcontroller,在那里我改变了方向。现在我回到第一个viewcontroller并再次切换方向。然后我在图像2中看到结果。出现黑色边框。请不要介意屏幕中央的白框。

下面你会找到我动画的代码。你能看出出了什么问题吗?

import Foundation

import UIKit

class ZoomOutCircleViewTransition: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    var hideDelayed = false
    var transitionContext: UIViewControllerContextTransitioning?

    init(hideDelayed : Bool = true) {
        self.hideDelayed = hideDelayed
        super.init()
    }


    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.6
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext

        guard let toViewController: UIViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else {
            return
        }

        guard let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) else {
            return
        }

        guard let toViewTransitionFromView = toViewController as? TransitionFromViewProtocol else {
            return
        }

        let containerView = transitionContext.containerView()

        let imageViewSnapshot = toViewTransitionFromView.getViewForTransition()
        imageViewSnapshot.alpha = 0.0
        let imageViewSnapshotOriginalFrame = imageViewSnapshot.frame

        let startFrame = CGRectMake(-CGRectGetWidth(toViewController.view.frame)/2, -CGRectGetHeight(toViewController.view.frame)/2, CGRectGetWidth(toViewController.view.frame)*2, CGRectGetHeight(toViewController.view.frame)*2)

        let quadraticStartFrame = CGRect(x: startFrame.origin.x - (startFrame.height - startFrame.width)/2, y: startFrame.origin.y, width: startFrame.height, height: startFrame.height)

        containerView!.insertSubview(toViewController.view, atIndex: 0)
        containerView!.addSubview(imageViewSnapshot)


        // UIViewController circle shrink animation
        let bigCirclePath = UIBezierPath(ovalInRect: quadraticStartFrame)

        let smallCirclePath = UIBezierPath(ovalInRect: imageViewSnapshot.frame)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = toViewController.view.frame
        maskLayer.path = bigCirclePath.CGPath//maskPath.CGPath
        fromViewController.view.layer.mask = maskLayer

        let pathAnimation = CABasicAnimation(keyPath: "path")
        pathAnimation.delegate = self
        pathAnimation.fromValue = bigCirclePath.CGPath
        pathAnimation.toValue = smallCirclePath.CGPath
        pathAnimation.duration = transitionDuration(transitionContext)
        maskLayer.path = smallCirclePath.CGPath
        maskLayer.addAnimation(pathAnimation, forKey: "pathAnimation")

        //        pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        imageViewSnapshot.frame = quadraticStartFrame

        // Make imageView visible with animation
        let showImageViewAnimation =  {
            imageViewSnapshot.alpha = 1.0
        }
        let showImageViewDelay = 0.3
        UIView.animateWithDuration(transitionDuration(transitionContext) - showImageViewDelay , delay: showImageViewDelay, options: UIViewAnimationOptions.CurveLinear, animations: showImageViewAnimation) { (completed) -> Void in
        }

        // Shrink the imageView to the original size
        let scaleImageViewAnimation = {
            imageViewSnapshot.frame = imageViewSnapshotOriginalFrame
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: scaleImageViewAnimation) { (completed) -> Void in
            // After the complete animations have endet

            // Hide ImageView after it is completely resized. Added some animation delay to not abruptly change the contactImage
            if self.hideDelayed {
                let hideImageViewAnimation =  {
                    imageViewSnapshot.alpha = 0.0
                }
                UIView.animateWithDuration(0.2 , delay: 0.2, options: UIViewAnimationOptions.CurveLinear, animations: hideImageViewAnimation) { (completed) -> Void in
                    imageViewSnapshot.removeFromSuperview()
                }
            }
            else {
                imageViewSnapshot.removeFromSuperview()
            }
        }
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        if let transitionContext = self.transitionContext {
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    }

    // MARK: UIViewControllerTransitioningDelegate protocol methods

    // return the animataor when presenting a viewcontroller
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    // return the animator used when dismissing from a viewcontroller
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

}

2 个答案:

答案 0 :(得分:2)

您需要将新帧设置为toViewController.view。这会将视图更新为当前方向。

toViewController.view.frame = fromViewController.view.frame

使用我更新的代码:

class ZoomOutCircleViewTransition: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    var hideDelayed = false
    var transitionContext: UIViewControllerContextTransitioning?

    init(hideDelayed : Bool = true) {
        self.hideDelayed = hideDelayed
        super.init()
    }


    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.6
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext

        guard let toViewController: UIViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else {
            return
        }

        guard let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) else {
            return
        }

        guard let toViewTransitionFromView = toViewController as? TransitionFromViewProtocol else {
            return
        }

        toViewController.view.frame = fromViewController.view.frame

        let containerView = transitionContext.containerView()

        let imageViewSnapshot = toViewTransitionFromView.getViewForTransition()
        imageViewSnapshot.alpha = 0.0
        let imageViewSnapshotOriginalFrame = imageViewSnapshot.frame

        let startFrame = CGRectMake(-CGRectGetWidth(fromViewController.view.frame)/2, -CGRectGetHeight(fromViewController.view.frame)/2, CGRectGetWidth(toViewController.view.frame)*2, CGRectGetHeight(toViewController.view.frame)*2)

        let quadraticStartFrame = CGRect(x: startFrame.origin.x - (startFrame.height - startFrame.width)/2, y: startFrame.origin.y, width: startFrame.height, height: startFrame.height)

        containerView!.insertSubview(toViewController.view, atIndex: 0)
        containerView!.addSubview(imageViewSnapshot)


        // UIViewController circle shrink animation
        let bigCirclePath = UIBezierPath(ovalInRect: quadraticStartFrame)

        let smallCirclePath = UIBezierPath(ovalInRect: imageViewSnapshot.frame)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = toViewController.view.frame
        maskLayer.path = bigCirclePath.CGPath//maskPath.CGPath
        fromViewController.view.layer.mask = maskLayer

        let pathAnimation = CABasicAnimation(keyPath: "path")
        pathAnimation.delegate = self
        pathAnimation.fromValue = bigCirclePath.CGPath
        pathAnimation.toValue = smallCirclePath.CGPath
        pathAnimation.duration = transitionDuration(transitionContext)
        maskLayer.path = smallCirclePath.CGPath
        maskLayer.addAnimation(pathAnimation, forKey: "pathAnimation")

        //        pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        imageViewSnapshot.frame = quadraticStartFrame

        // Make imageView visible with animation
        let showImageViewAnimation =  {
            imageViewSnapshot.alpha = 1.0
        }
        let showImageViewDelay = 0.3
        UIView.animateWithDuration(transitionDuration(transitionContext) - showImageViewDelay , delay: showImageViewDelay, options: UIViewAnimationOptions.CurveLinear, animations: showImageViewAnimation) { (completed) -> Void in
        }

        // Shrink the imageView to the original size
        let scaleImageViewAnimation = {
            imageViewSnapshot.frame = imageViewSnapshotOriginalFrame
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: scaleImageViewAnimation) { (completed) -> Void in
            // After the complete animations have endet

            // Hide ImageView after it is completely resized. Added some animation delay to not abruptly change the contactImage
            if self.hideDelayed {
                let hideImageViewAnimation =  {
                    imageViewSnapshot.alpha = 0.0
                }
                UIView.animateWithDuration(0.2 , delay: 0.2, options: UIViewAnimationOptions.CurveLinear, animations: hideImageViewAnimation) { (completed) -> Void in
                    imageViewSnapshot.removeFromSuperview()
                }
            }
            else {
                imageViewSnapshot.removeFromSuperview()
            }
            toViewController.view.layer.mask = nil
        }
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        if let transitionContext = self.transitionContext {
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    }

    // MARK: UIViewControllerTransitioningDelegate protocol methods

    // return the animataor when presenting a viewcontroller
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    // return the animator used when dismissing from a viewcontroller
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

}

class ZoomOutCircleViewTransition: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {

    var hideDelayed = false
    var transitionContext: UIViewControllerContextTransitioning?

    init(hideDelayed : Bool = true) {
        self.hideDelayed = hideDelayed
        super.init()
    }


    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.6
    }

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        self.transitionContext = transitionContext

        guard let toViewController: UIViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey) else {
            return
        }

        guard let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) else {
            return
        }

        guard let toViewTransitionFromView = toViewController as? TransitionFromViewProtocol else {
            return
        }

        toViewController.view.frame = fromViewController.view.frame

        let containerView = transitionContext.containerView()

        let imageViewSnapshot = toViewTransitionFromView.getViewForTransition()
        imageViewSnapshot.alpha = 0.0
        let imageViewSnapshotOriginalFrame = imageViewSnapshot.frame

        let startFrame = CGRectMake(-CGRectGetWidth(fromViewController.view.frame)/2, -CGRectGetHeight(fromViewController.view.frame)/2, CGRectGetWidth(toViewController.view.frame)*2, CGRectGetHeight(toViewController.view.frame)*2)

        let quadraticStartFrame = CGRect(x: startFrame.origin.x - (startFrame.height - startFrame.width)/2, y: startFrame.origin.y, width: startFrame.height, height: startFrame.height)

        containerView!.insertSubview(toViewController.view, atIndex: 0)
        containerView!.addSubview(imageViewSnapshot)


        // UIViewController circle shrink animation
        let bigCirclePath = UIBezierPath(ovalInRect: quadraticStartFrame)

        let smallCirclePath = UIBezierPath(ovalInRect: imageViewSnapshot.frame)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = toViewController.view.frame
        maskLayer.path = bigCirclePath.CGPath//maskPath.CGPath
        fromViewController.view.layer.mask = maskLayer

        let pathAnimation = CABasicAnimation(keyPath: "path")
        pathAnimation.delegate = self
        pathAnimation.fromValue = bigCirclePath.CGPath
        pathAnimation.toValue = smallCirclePath.CGPath
        pathAnimation.duration = transitionDuration(transitionContext)
        maskLayer.path = smallCirclePath.CGPath
        maskLayer.addAnimation(pathAnimation, forKey: "pathAnimation")

        //        pathAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

        imageViewSnapshot.frame = quadraticStartFrame

        // Make imageView visible with animation
        let showImageViewAnimation =  {
            imageViewSnapshot.alpha = 1.0
        }
        let showImageViewDelay = 0.3
        UIView.animateWithDuration(transitionDuration(transitionContext) - showImageViewDelay , delay: showImageViewDelay, options: UIViewAnimationOptions.CurveLinear, animations: showImageViewAnimation) { (completed) -> Void in
        }

        // Shrink the imageView to the original size
        let scaleImageViewAnimation = {
            imageViewSnapshot.frame = imageViewSnapshotOriginalFrame
        }
        UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0.0, options: UIViewAnimationOptions.CurveLinear, animations: scaleImageViewAnimation) { (completed) -> Void in
            // After the complete animations have endet

            // Hide ImageView after it is completely resized. Added some animation delay to not abruptly change the contactImage
            if self.hideDelayed {
                let hideImageViewAnimation =  {
                    imageViewSnapshot.alpha = 0.0
                }
                UIView.animateWithDuration(0.2 , delay: 0.2, options: UIViewAnimationOptions.CurveLinear, animations: hideImageViewAnimation) { (completed) -> Void in
                    imageViewSnapshot.removeFromSuperview()
                }
            }
            else {
                imageViewSnapshot.removeFromSuperview()
            }
            toViewController.view.layer.mask = nil
        }
    }

    override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
        if let transitionContext = self.transitionContext {
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled())
        }
    }

    // MARK: UIViewControllerTransitioningDelegate protocol methods

    // return the animataor when presenting a viewcontroller
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }

    // return the animator used when dismissing from a viewcontroller
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return self
    }
}

答案 1 :(得分:0)

我遇到了同样的问题并尝试了@Arun提出的解决方案,但它没有用。对我来说,这是:

    }) { finished in
        toViewController.view.frame = toFrame
        transitionContext.completeTransition(finished)
    }

并在完成动画块中:

var map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.MapQuest({
        layer: 'sat'
      })
    })
  ],
  view: new ol.View({
    center: ol.proj.transform([37.41, 8.82], 'EPSG:4326', 'EPSG:3857'),
    zoom: 4
  })
});
var source = new ol.source.Vector();
var layer = new ol.layer.Vector({
  source: source,
  name: 'layer'
});
map.addLayer(layer);
var drawFeaturesCollection = new ol.Collection();
var drawInteraction = new ol.interaction.Draw({
  'type': 'Polygon',
  'source': source,
  'geometryFunction': onDrawPolygon,
  'features': drawFeaturesCollection
});
drawInteraction.on('drawend', function(e) {
  currentDrawingGeom = undefined;
});
map.addInteraction(drawInteraction);
var currentDrawingGeom;
var addCoordinate = false;
var coord1 = [2690583.395638204, 585201.8885513092];
var coord2 = [2270583.395638204, 543201.8885513092];
var added1 = false;

function onDrawPolygon(coordinates, geometry) {
  if (geometry) {
    if (addCoordinate) {
      coordinates[0].splice(coordinates.length - 2, 0, added1 ? coord2 : coord1);
      added1 = !added1;
      addCoordinate = false;
    }
    geometry.setCoordinates(coordinates);
  } else {
    geometry = new ol.geom.Polygon(coordinates);
  }
  currentDrawingGeom = geometry;
  return geometry;
}

function addCoord() {
  addCoordinate = true;
}

function clearLayer() {
  layer.getSource().clear();
}

function addCoordColl() {
  var coords = currentDrawingGeom.getCoordinates();
  coords[0].splice(coords.length - 2, 0, coord1);
  currentDrawingGeom.setCoordinates(coords);
}

希望有所帮助!

相关问题