2015-09-09 35 views
0

我試圖切換視圖控制器在迅速編程與動畫:iOS的 - 在切換視圖控制器的自定義動畫編程

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」,因爲我無法找到一些褪色out動畫

那麼有沒有辦法使用自定義動畫?

回答

1

是的,你可以爲過渡設置動畫,但是你想使用自定義的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刪除,否則可能會造成內存泄漏。您在示例代碼中轉換的方式將爲您處理此步驟。

相關問題