【发布时间】:2016-06-22 10:02:45
【问题描述】:
我已经实现了一个类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 函数时,视图的图层蒙版会被重置。
【问题讨论】:
标签: swift animation uiview transition caanimation