2016-09-07 151 views
0

如何在Swift協議中使用UIView.animateWithDuration?當我嘗試使用它,我總是得到:在協議中使用UIView.animateWithDuration

曖昧參考成員 'animateWithDuration(_:延遲:選項:動畫:完成:)'

我怎樣才能獲得參考的UIView(據我所知,animateWithDuration是靜態方法)?

+0

請給我們看你的代碼 – Hamish

回答

1

由於您無法在協議聲明本身內提供任何實現,因此您應該在默認實現中引用UIView類。我希望這三個模板案例之一是你需要的:

import UIKit 

protocol SomeProtocol { 
    static func animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations:() -> Void, completion: ((Bool) -> Void)?) 
    func animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations:() -> Void, completion: ((Bool) -> Void)?) 
    func someCustomFuncForAnimate() 
} 

extension SomeProtocol { 
    static func animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations:() -> Void, completion: ((Bool) -> Void)?) { 
     UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations, completion: completion) 
    } 
    func animateWithDuration(duration: NSTimeInterval, delay: NSTimeInterval, options: UIViewAnimationOptions, animations:() -> Void, completion: ((Bool) -> Void)?) { 
     UIView.animateWithDuration(duration, delay: delay, options: options, animations: animations, completion: completion) 
    } 
    func someCustomFuncForAnimate() { 
     UIView.animateWithDuration(0.2, delay: 1, options: .TransitionCrossDissolve, animations: {/*...*/}, completion: nil) 
    } 
}