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

标签 ios xcode swift animation uigesturerecognizer

我有这个 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 动画 block 之后实例化 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)
  })

我猜是这样的

关于ios - 如何在动画结束时实例化 viewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32515474/

相关文章:

ios - iBeacon 案例 - 触发具有距离限制的 didEnterRegion

ios - 如何解决像 'NULL cString' 这样的崩溃?

ios - 根据indexPath.section和indexPath.row判断多维结构的cell类型

ios - PDFKit 的 PDFDocument init(url : URL) does not work with HTTPS

ios - MKMapView 不改变区域

ios - 绘制遵循道路几何形状的多段线

ios - 找不到目标的伞头

ios - 运行带有分发配置文件的 xcode 项目

ios - 从 Testflight 下载应用程序时,我的 JSON 方法不起作用

ios - 无法构造“任务”,因为它没有可访问的初始值设定项