我正在关注https://github.com/RabbitMC/inbox-replica以制作收件箱副本转换的Objective-C版本,但是我陷入了无法制作Objective-C代码的代码。
这是Swift代码:
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let duration = transitionDuration(transitionContext)
let fromViewController = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey)!
let toViewController = transitionContext.viewControllerForKey(UITransitionContextToViewControllerKey)!
let containerView = transitionContext.containerView()
var foregroundViewController = toViewController
var backgroundViewController = fromViewController
if type == .Dismissing {
foregroundViewController = fromViewController
backgroundViewController = toViewController
}
// get target view
var targetViewController = backgroundViewController
if let navController = targetViewController as? UINavigationController {
targetViewController = navController.topViewController!
}
let targetViewMaybe = (targetViewController as? ExpandingTransitionPresentingViewController)?.expandingTransitionTargetViewForTransition(self)
这是我的Objective-C代码:
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
NSTimeInterval duration = [self transitionDuration:transitionContext];
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
UIViewController *foregroundViewController = toViewController;
UIViewController *backgroundViewController = fromViewController;
if (type == Dismissing) {
foregroundViewController = fromViewController;
backgroundViewController = toViewController;
}
//get the target view
UIViewController *targetViewController = backgroundViewController;
if ([targetViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navController = (UINavigationController *)targetViewController;
targetViewController = navController.topViewController;
}
UIView *targetViewMaybe = ([targetViewController conformsToProtocol:@protocol(ExpandingTransitionPresentingViewController)])?[targetViewController expandingTransitionTargetViewForTransition:self]:assert(targetViewMaybe != nil);
现在问题就出现了,我必须在targetViewController
上调用协议方法,这是一个UIViewController
,但编译器提示我输入错误:
UIViewController没有可见的接口来声明选择器'expandingTransitionTargetViewForTransition'
答案 0 :(得分:1)
问题的原因是targetViewController
属于UIViewController
类型。但是您尝试从某个协议调用方法。解决方案是在检查它实际符合协议后将指针强制转换为适当的类型。
帮自己一个忙,避免冗长,难以阅读,不可能像上一个那样调试行:
UIView *targetViewMaybe = ([targetViewController conformsToProtocol:@protocol(ExpandingTransitionPresentingViewController)])?[targetViewController expandingTransitionTargetViewForTransition:self]:assert(targetViewMaybe != nil);
将其拆分成可管理的东西。以下内容修复了该问题,使代码更易于阅读和调试:
if ([targetViewController conformsToProtocol:@protocol(ExpandingTransitionPresentingViewController)]) {
id<ExpandingTransitionPresentingViewController> controller = (id<ExpandingTransitionPresentingViewController>)targetViewController;
UIView *targetView = [controller expandingTransitionTargetViewForTransition:self];
// Do something with targetView
} else {
// Handle as needed
}