2015-10-24 21 views
1

我正在用Swift創建iOS應用程序。我發現了一個我想在我的表格視圖中實現的動畫,但代碼位於Objective-C中。無法符合Swift中的obj-c協議

庫:https://github.com/recruit-mp/RMPZoomTransitionAnimator

我已經成功過渡的OBJ-C代碼來斯威夫特,但似乎不符合所需的協議。

協議:

@protocol RMPZoomTransitionAnimating <NSObject> 

@required 

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

@end 

我迅速實施:

實現該協議第一類:

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

有一個'sourceTransition'和'destinationTransition'。他們是兩個不同的對象。他們必須_符合RMPZoomTransitionAnimating。但是您只顯示符合RMPZoomTransitionAnimating的_one_對象。另一個呢? – matt

+0

[如何創建符合在Swift和Objective-C之間共享的協議的類方法]可能的重複(http://stackoverflow.com/questions/29399871/how-to-create-class-methods-that-c​​onform -i-protocol-shared-between-swift-and) – Jeef

+0

@Jeef我現在來看看它 – Mindaugas

回答

0

雨燕類不是(默認情況下)Objective-C兼容。

您可以通過繼承NSObject或在班級前添加@objc來獲得兼容性。我懷疑這個「可能」是你的問題 - 但我不幸目前無法測試它。

您可能還需要添加一些初始化工具,如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的兼容