【问题标题】:Objective C to Swift Conversion (Protocol)Objective C 到 Swift 的转换(协议)
【发布时间】:2016-01-22 14:18:35
【问题描述】:

我正在尝试将下面的代码转换为 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&lt;RMPZoomTransitionAnimating&gt;)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;
}

我不知道那叫什么。知道那是什么吗? 我试着投它,但它不起作用

【问题讨论】:

    标签: ios objective-c swift uinavigationcontroller


    【解决方案1】:

    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!) {
    
    }
    }
    

    【讨论】:

      【解决方案2】:

      你可以按如下方式投射:

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

      只有当fromVC同时符合RMPZoomTransitionAnimatingRMPZoomTransitionDelegate这两个协议时,转换才会成功

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

      【讨论】:

        猜你喜欢
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 2016-07-06
        • 1970-01-01
        相关资源
        最近更新 更多