在协议内使用UIView.animateWithDuration

时间:2016-09-07 15:53:01

标签: swift swift-protocols

如何在Swift协议中使用UIView.animateWithDuration?当我尝试使用它时,我总是得到:

  

对成员的模棱两可的提及   'animateWithDuration(_:延迟:选择:动画:完成:)'

我如何能够引用UIView(animateWithDuration是我所理解的静态方法?)

1 个答案:

答案 0 :(得分: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)
    }
}