如何使用UIPopoverPresentationControlleranimationpreferredContentSize?

我能够通过简单地设置preferredContentSize成功地改变我提供的popover框架。 我现在想animation内容大小的变化,但我没有成功。 它只是即时更新,实际上我设置的延迟甚至没有被尊重。 内容大小如何变化?

 //In viewDidAppear: UIView.animateWithDuration(5, delay: 3, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in self.preferredContentSize = CGSizeMake(320, 400) }, completion: nil) 

我能够重现你的问题,然后想出一个办法使其工作。 animation延迟在这里不起作用,所以将其设置为0,并在做animation之前做一个不同types的延迟。 我使用了我在这里find的一个函数:

 func delay(delay: Double, closure:()->()) { dispatch_after( dispatch_time( DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)) ), dispatch_get_main_queue(), closure) } 

只要把它放在类定义之外的任何swift文件的顶部。

然后改变你的代码看起来像这样:

 delay(3, { () -> () in UIView.animateWithDuration(5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.25, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in self.preferredContentSize = CGSizeMake(320, 400) }, completion: nil) })