iOS: 自定义 transitioningDelegate 不起作用
iOS: Custom transitioningDelegate doesn't work
这是我的自定义 transitioningDelegate:
enum CameraState {
case On
case Off
}
class CameraTransitioning: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
var state: CameraState
init(state: CameraState) {
self.state = state
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
var toViewInitialFrame = transitionContext.initialFrameForViewController(toVC!)
var fromViewFinalFrame = transitionContext.finalFrameForViewController(fromVC!)
switch self.state {
case .On:
toViewInitialFrame.origin.y = containerView!.frame.height
case .Off:
fromViewFinalFrame.origin.y = -containerView!.frame.height
}
containerView?.addSubview(toView!)
toView?.frame = toViewInitialFrame
let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(duration, animations: {
fromView?.frame = fromViewFinalFrame
}) {
finished in
transitionContext.completeTransition(true)
}
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 10
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
}
这就是我的使用方式:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Home -> Camera" {
let cameraVC = segue.destinationViewController as! CameraViewController
cameraVC.delegate = self
cameraVC.transitioningDelegate = CameraTransitioning(state: .On)
}
}
如您所见,我使用此过渡是因为我不喜欢默认的 UIViewAnimationCurveEaseInOut,并且我尝试将持续时间设置为 10 以使此更改更加清晰。但这不起作用。问题出在哪里?
transitioningDelegate
属性 是弱引用,您没有创建对它的其他强引用。其他东西需要拥有该对象才能使其停留足够长的时间以用于动画转换。
这是我的自定义 transitioningDelegate:
enum CameraState {
case On
case Off
}
class CameraTransitioning: NSObject, UIViewControllerAnimatedTransitioning, UIViewControllerTransitioningDelegate {
var state: CameraState
init(state: CameraState) {
self.state = state
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()
let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)
let toVC = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
var toViewInitialFrame = transitionContext.initialFrameForViewController(toVC!)
var fromViewFinalFrame = transitionContext.finalFrameForViewController(fromVC!)
switch self.state {
case .On:
toViewInitialFrame.origin.y = containerView!.frame.height
case .Off:
fromViewFinalFrame.origin.y = -containerView!.frame.height
}
containerView?.addSubview(toView!)
toView?.frame = toViewInitialFrame
let duration = self.transitionDuration(transitionContext)
UIView.animateWithDuration(duration, animations: {
fromView?.frame = fromViewFinalFrame
}) {
finished in
transitionContext.completeTransition(true)
}
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 10
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
}
这就是我的使用方式:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Home -> Camera" {
let cameraVC = segue.destinationViewController as! CameraViewController
cameraVC.delegate = self
cameraVC.transitioningDelegate = CameraTransitioning(state: .On)
}
}
如您所见,我使用此过渡是因为我不喜欢默认的 UIViewAnimationCurveEaseInOut,并且我尝试将持续时间设置为 10 以使此更改更加清晰。但这不起作用。问题出在哪里?
transitioningDelegate
属性 是弱引用,您没有创建对它的其他强引用。其他东西需要拥有该对象才能使其停留足够长的时间以用于动画转换。