Swift UIView自定义转换在完成时捕捉回来
我已经实现了一个类Swift UIView自定义转换在完成时捕捉回来,swift,animation,uiview,transition,caanimation,Swift,Animation,Uiview,Transition,Caanimation,我已经实现了一个类BubbleAnimator,它应该在视图之间创建一个类似于气泡的转换,并通过UIViewControllerTransitioningDelegate-协议添加了它。到目前为止,演示动画效果很好(这就是为什么我没有为这一部分添加所有代码) 但是在关闭视图时,“fromViewController”会在动画的最后闪烁。在这个很短的闪烁之后,正确的toViewController再次显示,但是这个小故障非常恼人。 以下是相关的animateTransition-方法: /
BubbleAnimator
,它应该在视图之间创建一个类似于气泡的转换,并通过UIViewControllerTransitioningDelegate
-协议添加了它。到目前为止,演示动画效果很好(这就是为什么我没有为这一部分添加所有代码)
但是在关闭视图时,“fromViewController”会在动画的最后闪烁。在这个很短的闪烁之后,正确的toViewController
再次显示,但是这个小故障非常恼人。
以下是相关的animateTransition
-方法:
//Get all the necessary views from the context
let containerView = transitionContext.containerView()
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
//Presenting
if self.reverse == false {
//Add the destinationvc as subview
containerView!.addSubview(fromViewController!.view)
containerView!.addSubview(toViewController!.view)
/*...Animating the layer goes here... */
//Dismissing
} else {
containerView!.addSubview(toViewController!.view)
containerView!.addSubview(fromViewController!.view)
//Init the paths
let circleMaskPathInitial = UIBezierPath(ovalInRect: self.originFrame)
let extremePoint = CGPoint(x: originFrame.origin.x , y: originFrame.origin.y - CGRectGetHeight(toViewController!.view.bounds) )
let radius = sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y))
let circleMaskPathFinal = UIBezierPath(ovalInRect: CGRectInset(originFrame, -radius, -radius))
//Create a layer
let maskLayer = CAShapeLayer()
maskLayer.path = circleMaskPathFinal.CGPath
fromViewController!.view.layer.mask = maskLayer
//Create and add the animation
let animation = CABasicAnimation(keyPath: "path")
animation.toValue = circleMaskPathInitial.CGPath
animation.fromValue = circleMaskPathFinal.CGPath
animation.duration = self.transitionDuration(transitionContext)
animation.delegate = self
maskLayer.addAnimation(animation, forKey: "path")
}
清理在委托方法中进行:
override public func animationDidStop(anim: CAAnimation, finished flag: Bool) {
self.transitionContext?.completeTransition(!(self.transitionContext?.transitionWasCancelled())!)
}
我想,我在将视图添加到containerView时犯了一些错误,但我无法理解。另一种可能性是,当调用函数completeTransition
时,视图的层掩码被重置。多亏了我终于能够解决这个问题。简要说明:
CAAnimation仅操作视图的表示层,但不更改模型层。动画现在完成后,其值将捕捉回模型层的原始值和未更改的值
简短而简单的解决方法:
animation.fillMode = kCAFillModeForwards
animation.removedOnCompletion = false
更好的解决方案是在动画开始之前手动设置层位置的最终值,因为它不会阻止动画被删除。这样,将为模型图层指定正确的值:
maskLayer.path = circleMaskPathInitial.CGPath
//Create and add the animation below..