iOS - 以编程方式切换视图控制器的自定义动画
iOS - Custom animations in switching view controllers programmatically
我正在尝试通过动画以编程方式切换 swift 中的视图控制器:
var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var appDelegateTemp : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var view : UIView? = appDelegateTemp.window!.rootViewController?.view
destinationViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as? UIViewController
UIView.transitionFromView(view!, toView: destinationViewController!.view, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromBottom,
completion: { (completed : Bool) -> Void in
var application = UIApplication.sharedApplication()
var appDelegateTemp : AppDelegate = application.delegate as! AppDelegate
appDelegateTemp.window!.rootViewController = self.destinationViewController
}
)
我将动画选项设置为 "TransitionFlipFromBottom",因为我找不到一些淡出动画
那么有没有办法使用自定义动画?
是的,您可以根据需要使用自定义 UIView
动画来制作过渡动画。例如,新视图从左侧滑入,旧视图从右侧滑出的基本幻灯片过渡:
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect toViewStartFrame = toView.frame;
CGRect toViewEndFrame = fromView.frame;
CGRect fromViewEndFrame = fromView.frame;
toViewStartFrame.origin.x = -screenSize.width;
fromViewEndFrame.origin.x = screenSize.width;
[fromView.superview addSubview:toView];
toView.frame = toViewStartFrame;
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
toView.frame = toViewEndFrame;
fromView.frame = fromViewEndFrame;
} completion:^(BOOL finished) {
[fromView removeFromSuperview];
fromView = nil;
}];
基本前提是设置toView
的结束帧和fromView
的结束帧,然后使用UIView
动画。只需确保从超级视图中删除 fromView
然后再将其删除 nil
,否则可能会造成内存泄漏。您在示例代码中的转换方式会为您处理这一步。
我正在尝试通过动画以编程方式切换 swift 中的视图控制器:
var storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
var appDelegateTemp : AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var view : UIView? = appDelegateTemp.window!.rootViewController?.view
destinationViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController") as? UIViewController
UIView.transitionFromView(view!, toView: destinationViewController!.view, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromBottom,
completion: { (completed : Bool) -> Void in
var application = UIApplication.sharedApplication()
var appDelegateTemp : AppDelegate = application.delegate as! AppDelegate
appDelegateTemp.window!.rootViewController = self.destinationViewController
}
)
我将动画选项设置为 "TransitionFlipFromBottom",因为我找不到一些淡出动画
那么有没有办法使用自定义动画?
是的,您可以根据需要使用自定义 UIView
动画来制作过渡动画。例如,新视图从左侧滑入,旧视图从右侧滑出的基本幻灯片过渡:
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect toViewStartFrame = toView.frame;
CGRect toViewEndFrame = fromView.frame;
CGRect fromViewEndFrame = fromView.frame;
toViewStartFrame.origin.x = -screenSize.width;
fromViewEndFrame.origin.x = screenSize.width;
[fromView.superview addSubview:toView];
toView.frame = toViewStartFrame;
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:0 animations:^{
toView.frame = toViewEndFrame;
fromView.frame = fromViewEndFrame;
} completion:^(BOOL finished) {
[fromView removeFromSuperview];
fromView = nil;
}];
基本前提是设置toView
的结束帧和fromView
的结束帧,然后使用UIView
动画。只需确保从超级视图中删除 fromView
然后再将其删除 nil
,否则可能会造成内存泄漏。您在示例代码中的转换方式会为您处理这一步。