首页 文章

无法符合Swift中的obj-c协议

提问于
浏览
1

我正在用Swift创建一个iOS应用程序 . 我发现了一个我想在我的表视图中实现的动画,但代码是在Objective-C中 .

存储库:https://github.com/recruit-mp/RMPZoomTransitionAnimator

我已成功将Obj-C代码桥接到Swift,但似乎无法符合所需的协议 .

协议:

@protocol RMPZoomTransitionAnimating <NSObject>

@required

- (UIImageView *)transitionSourceImageView;
- (UIColor *)transitionSourceBackgroundColor;
- (CGRect)transitionDestinationImageViewFrame;

@end

我的Swift实现:

实现协议的第一类:

class ChallengeViewController: UIViewController, RMPZoomTransitionAnimating
   func transitionSourceImageView() -> UIImageView {
        return imageView
    }

    func transitionSourceBackgroundColor() -> UIColor {
        return UIColor.whiteColor()
    }

    func transitionDestinationImageViewFrame() -> CGRect {
        return imageView.frame
    }

二等:

class ChallengeTableViewController: UITableViewController, RMPZoomTransitionAnimating
    func transitionSourceImageView() -> UIImageView {
        return imageForTransition!
    }

    func transitionSourceBackgroundColor() -> UIColor {
        return UIColor.whiteColor()
    }

    func transitionDestinationImageViewFrame() -> CGRect {
        return imageFrame!
    }

在动画播放之前发生的此检查始终失败:

Protocol *animating = @protocol(RMPZoomTransitionAnimating);
    BOOL doesNotConfirmProtocol = ![self.sourceTransition conformsToProtocol:animating] || ![self.destinationTransition conformsToProtocol:animating];

我已经阅读了这个主题How to create class methods that conform to a protocol shared between Swift and Objective-C?但没有找到任何帮助

任何线索都会非常感激

1 回答

  • 0

    Swift类本身不是(默认情况下)Objective-C兼容 .

    您可以通过继承 NSObject 或在类前面添加 @objc 来获得兼容性 . 我怀疑这是"may"是你的问题 - 但遗憾的是我现在无法测试它 .

    您可能还需要添加一些初始化程序,例如在您的情况下从 NSCoder 添加一些 - 我现在无法访问Xcode .

    https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

    尝试:

    @objc
    class ChallengeViewController: UIViewController, RMPZoomTransitionAnimating
       func transitionSourceImageView() -> UIImageView {
            return imageView
        }
    
        func transitionSourceBackgroundColor() -> UIColor {
            return UIColor.whiteColor()
        }
    
        func transitionDestinationImageViewFrame() -> CGRect {
            return imageView.frame
        }
    

    这将告诉编译器您的类是Objective-c兼容的

相关问题