How to animate preferredContentSize when using UIPopoverPresentationController?

夙愿已清 提交于 2019-12-22 08:47:06

问题


I am able to successfully change the frame of a popover I present by simply setting preferredContentSize. I would now like to animate the change in the content size but I haven't been successful. It just instantly updates, in fact the delay I have set isn't even respected. How could one animate the change in content size?

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

回答1:


I was able to reproduce your problem, then figured out a way to make it work. The animation delay just isn't working here, so set that to 0 and do a different type of delay before doing the animation. I use a function I found here on SO:

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

Just put that at the top of any swift file, outside of the class definition.

Then change your code to look like this:

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


来源:https://stackoverflow.com/questions/29860816/how-to-animate-preferredcontentsize-when-using-uipopoverpresentationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!