如何在动画结束时实例化 viewController?

how to instantiate a viewController at the end of an animation?

我有这个 UIGestureRecognizer,它在其函数中嵌套了一个动画,在该函数的末尾,我想实例化一个 viewController.. 完成此操作的正确方法是什么?这是我的代码:

func longPress(gesture:UILongPressGestureRecognizer)
    {
        if gesture.state == UIGestureRecognizerState.Began
        {
            oldbounds = self.imageView.bounds

            let bounds = self.imageView.bounds
            UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
                self.imageView.bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: bounds.size.width + 40, height: bounds.size.height + 40)
            }, completion: nil)

            println("user pressed on image")
        }
        else if gesture.state == UIGestureRecognizerState.Changed
        {
            gesture.state == UIGestureRecognizerState.Began
        }
        else
        {
            let bounds = self.imageView.bounds
            UIView.animateWithDuration(0.2, animations: {
                self.imageView.bounds = self.oldbounds
            })

            println("user release on image")
        }
    }

我猜代码应该在第一个 gesture.state 条件下动画的完成部分,但是我不知道这样做的正确代码实现。

UIView.animateWithDuration 有一个 completion block。这是在动画完成后将代码放入 运行 的地方;例如,您启动 viewController 的代码会放在那里。

例子

    UIView.animateWithDuration(0.3, animations: {
    //Do animation stuff here 
}, completion: { (complete: Bool) in
   //Create your view controller here and show on screen
})

在默认 UIView 动画块之后实例化 VC 的 objective C 代码如下

    [UIView animateWithDuration:1.0 animations:^{
    //animation code here
} completion:^(BOOL finished) {
    UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"storyboardIdentifier"]; // storyboard identifier is set for a VC in the storyboard in its attribute inspector
    [self.navigationController pushViewController:vc animated:YES];//pushing the VC in the navigation controller.
}];

Swift

  UIView.animateWithDuration(0.7, delay: 1.0, options: .CurveEaseOut, animations: {
// your animation code here
  }, completion: { finished in
    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
  })

我猜是这样的