首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >目标C到Swift转换(协议)

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

Stack Overflow用户
提问于 2015-10-23 13:53:17
回答 2查看 426关注 0票数 0

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

代码语言:javascript
复制
- (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

代码语言:javascript
复制
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;
}

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

EN

回答 2

Stack Overflow用户

发布于 2017-03-14 14:05:13

RMPZoomTransitionAnimator swift3.0 3.0 extension

代码语言:javascript
复制
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!) {

}
}
票数 1
EN

Stack Overflow用户

发布于 2015-10-23 15:30:50

您可以进行如下转换:

代码语言:javascript
复制
animator.sourceTransition = fromVC as? protocol<RMPZoomTransitionAnimating, RMPZoomTransitionDelegate>

仅当fromVC同时符合RMPZoomTransitionAnimatingRMPZoomTransitionDelegate协议时,转换才会成功

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33295668

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档