在协议中使用 UIView.animateWithDuration

Use UIView.animateWithDuration inside protocol

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

Ambiguous reference to member 'animateWithDuration(_:delay:options:animations:completion:)'

如何获取对 UIView 的引用(据我所知,animateWithDuration 是静态方法)?

由于您无法在协议声明本身内提供任何实现,因此您应该在默认实现中引用 UIView class。我希望这三个模板案例中的一个是您需要的:

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)
    }
}