繁体   English   中英

目标C到Swift的转换(协议)

[英]Objective C to Swift Conversion (Protocol)

我正在尝试将下面的代码转换为swift

- (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                   animationControllerForOperation:(UINavigationControllerOperation)operation
                                                fromViewController:(UIViewController *)fromVC
                                                  toViewController:(UIViewController *)toVC
{
    // minimum implementation for example
    RMPZoomTransitionAnimator *animator = [[RMPZoomTransitionAnimator alloc] init];
    animator.goingForward = (operation == UINavigationControllerOperationPush);
    animator.sourceTransition = (id<RMPZoomTransitionAnimating>)fromVC;
    animator.destinationTransition = (id<RMPZoomTransitionAnimating>)toVC;
    return animator;
}

到目前为止,我已经成功进行了转换,但是我想知道如何从(id<RMPZoomTransitionAnimating>)fromVC转换(id<RMPZoomTransitionAnimating>)fromVC

func navigationController(navigationController: UINavigationController, animationControllerForOperation operation: UINavigationControllerOperation, fromViewController fromVC: UIViewController, toViewController toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    let animator: RMPZoomTransitionAnimator = RMPZoomTransitionAnimator();

    animator.goingForward = (operation == UINavigationControllerOperation.Push);
    animator.sourceTransition = fromVC as! RMPZoomTransitionAnimating;//DOESN'T COMPILE
    animator.destinationTransition = toVC as! RMPZoomTransitionAnimating;//DOESN'T COMPILE
    return animator;
}

我不知道那叫什么。 知道那是什么吗? 我尝试投射它,但是没有用

屏幕截图

RMPZoomTransitionAnimator swift3.0扩展

extension ViewController: UINavigationControllerDelegate {

func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
    if fromVC is RMPZoomTransitionAnimating && toVC is RMPZoomTransitionAnimating {
        let animator = RMPZoomTransitionAnimator()
        animator.goingForward = (operation == .push)
        animator.sourceTransition = fromVC as? RMPZoomTransitionAnimating & RMPZoomTransitionDelegate
        animator.destinationTransition = toVC as? RMPZoomTransitionAnimating & RMPZoomTransitionDelegate
        return animator
    } else {
        return nil
    }
  }
}

extension ViewController: RMPZoomTransitionAnimating, RMPZoomTransitionDelegate {

func imageViewFrame() -> CGRect {
    if let collectionView = self.collectionView(),
        let indexPath = self.selectedIndexPath,
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as? NewsCollectionViewCell,
        let imageView = cell.fgImageView {
        let frame = imageView.convertRect(imageView.frame, toView: self.view.window)
        return frame
    }

    return CGRect.zero
}

func transitionSourceImageView() -> UIImageView! {
    let imageView = UIImageView()
    imageView.clipsToBounds = true
    imageView.isUserInteractionEnabled = false
    imageView.contentMode = .scaleAspectFill
    imageView.frame = imageViewFrame()
    imageView.image = self.selectedViewCell?.fgImageView!.image
    return imageView
}

func transitionSourceBackgroundColor() -> UIColor! {
    return UIColor.white
}

func transitionDestinationImageViewFrame() -> CGRect {
    return imageViewFrame()
}

func zoomTransitionAnimator(_ animator: RMPZoomTransitionAnimator!, didCompleteTransition didComplete: Bool, animatingSourceImageView imageView: UIImageView!) {

}
}

您可以按以下方式进行投射:

animator.sourceTransition = fromVC as? protocol<RMPZoomTransitionAnimating, RMPZoomTransitionDelegate>

仅当fromVC符合两个协议RMPZoomTransitionAnimatingRMPZoomTransitionDelegate时,转换才能成功

P / S:在编写Swift时,您应该忘记semi-colon

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM