我有这个 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/